Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/string/strdup/memdup.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include "string/strdup/memdup.h"

#include <stddef.h>


extern inline void *memdup(const void *p, size_t size);
40 changes: 29 additions & 11 deletions lib/string/strdup/memdup.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


Expand All @@ -9,21 +9,39 @@
#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "alloc/malloc.h"
#include "attr.h"
#include "sizeof.h"


// memdup_T - memory duplicate type-safe
#define memdup_T(p, T) memdup_T_(p, typeas(T))
#define memdup_T_(p, T) \
({ \
T *new_; \
\
new_ = malloc_T(1, T); \
if (new_ != NULL) \
*new_ = *(p); \
new_; \
})
#define memdup_T(p, n, T) memdup_T_(p, n, typeas(T))
#define memdup_T_(p, n, T) \
( \
_Generic(p, T *: (void)0, const T *: (void)0), \
(T *){memdup(p, (n) * sizeof(T))} \
)


ATTR_MALLOC(free)
inline void *memdup(const void *p, size_t size);


// memdup - memory duplicate
inline void *
memdup(const void *p, size_t size)
{
void *dup;

dup = malloc(size);
if (dup == NULL)
return NULL;

return memcpy(dup, p, size);
}


#endif // include guard
2 changes: 1 addition & 1 deletion lib/utmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ get_current_utmp(pid_t main_pid)
ut = ut_by_pid ?: ut_by_line;

if (NULL != ut)
ut = memdup_T(ut, struct utmpx);
ut = memdup_T(ut, 1, struct utmpx);

free(ut_by_line);
free(ut_by_pid);
Expand Down
Loading