From c2ad0ba78f115818195a144f66e8b8cc37c0309b Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 19 Jan 2025 16:01:45 +0100 Subject: [PATCH 1/2] lib/string/strdup/strndupa.h: strndupa(3): Add macro musl doesn't provide strndupa(3). Signed-off-by: Alejandro Colomar --- lib/string/strdup/strndupa.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/string/strdup/strndupa.h b/lib/string/strdup/strndupa.h index cefc298205..c8d53f30b2 100644 --- a/lib/string/strdup/strndupa.h +++ b/lib/string/strdup/strndupa.h @@ -15,6 +15,12 @@ #include "string/strcpy/strncat.h" +// string n-bounded-read duplicate using-alloca(3) +#ifndef strndupa +# define strndupa(s, n) strncat(strcpy(alloca(n + 1), ""), s, n) +#endif + + // Similar to strndupa(3), but ensure that 's' is an array. #define STRNDUPA(s) \ ( \ From 9dcf865ea8d2dde9a512167022ee80b34327adc7 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 19 Jan 2025 15:32:12 +0100 Subject: [PATCH 2/2] lib/string/strdup/strndupa.h: STRNDUPA(): Simplify implementation Signed-off-by: Alejandro Colomar --- lib/string/strdup/strndupa.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/string/strdup/strndupa.h b/lib/string/strdup/strndupa.h index c8d53f30b2..80b8c3dcdb 100644 --- a/lib/string/strdup/strndupa.h +++ b/lib/string/strdup/strndupa.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar // SPDX-License-Identifier: BSD-3-Clause @@ -12,7 +12,6 @@ #include #include "sizeof.h" -#include "string/strcpy/strncat.h" // string n-bounded-read duplicate using-alloca(3) @@ -21,11 +20,7 @@ #endif -// Similar to strndupa(3), but ensure that 's' is an array. -#define STRNDUPA(s) \ -( \ - STRNCAT(strcpy(alloca(strnlen(s, NITEMS(s)) + 1), ""), s) \ -) +#define STRNDUPA(s) strndupa(s, NITEMS(s)) #endif // include guard