relaxed/relaxed; d=freebsd.org; s=dkim; t=1765861097; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=oIQmfkLvMkaYO2/UDlEeGjqrAfVfyVsVcW+ECnDrz6c=; b=uI8gP9rnQqwcOokEShvpfFYtH79aEG6urPGkgQTR2euOvxj5ZaIE9rRwonjshYQFsGo5GH yPXDGKf+RQI+mZnCD0uXRDV2uIRxMmxJ1tdl8urgLEcnm4GNd7NspKioLqWlTib0AVtjgr IQexeLFSZXOAP+0KjbVcnmVKJ0fcwrfk0z/70rCPnBMs+69iIVhMZf5WparOEuwgLTd/UO VvOegi0aysURQXb7eTIzWWH2ojrNmZpbpaxi2PgI66YEEFIYVqeXYk4NvRnhF4wlER0joK O5Qrc2XYqkjj1LjmDWbK5MTl2hV5zU0GimD/blhWzKHzmkFpBHUD3xAh8tmATQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765861097; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=oIQmfkLvMkaYO2/UDlEeGjqrAfVfyVsVcW+ECnDrz6c=; b=vy7hVUeg219S2nW8hqF9nlab5snj1wwSpwpjQVB6sJ6ZQLyJOgpxHwxFbot/WfW8sGATJY BmqIlQSoET+dk0mlqHmFfTL5Y1RedO006zTqpkMgpDihMX+Fjor9Fh4WSv8OwM14H0iaLf dqsFBNsrpgdJlmpBKuc8nAXZTO4FpgiYL3VVP5+QBJLk7EAbnOWs6WImPS8aNtXF9SG6BY u378mm8FIz2xE198CRow/upk4djuvXgtRW7iOl284Rnl+7czFfjHeq61V3gPKdi04uQxfI erU1l5CxR1EmRlE63WbA9IjCSqcnWiW2CS0xvyzjF5LNOfBdMuhAKg167xaC/Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1765861097; a=rsa-sha256; cv=none; b=cmM9c8ME4wrlFG6G+xLPM6dfLFVIiHqXmrMwq+0Ms6fXDj5XwDWOMb1kg5e8uWEO2GRpVh QclylX7a62z7d4nXg8wpuqYpgC5XEw6aYlVFtLNoc4uDIYhSp4zXOS+DSxg52NqC2zass/ TvxANNgeJ1tYLdaNGuo8JXbaoP0bGO0aAmV3sYJlWhSzGvDSCG1sh5EwK0SLIcuv8aPx93 O8xPknmjHsRMCY0WPSTD0u1aerMKS3pSXJh3ijLPvBZQBzS7N6+B0STKTTYSj2YOcaDpsq agmkGbfoV5InL+NrxQX4Kt4hc9TBTyJ01lN3upLxKBVmEqz+xR1e4ZLCvnR1ag== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4dVl7F2fkQzrJQ for ; Tue, 16 Dec 2025 04:58:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id c217 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 16 Dec 2025 04:58:17 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 242f545e4e31 - stable/14 - libc/string: add strdupa(3) and strndupa(3) List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 242f545e4e31b7fd49f4afac99531adebde55fce Auto-Submitted: auto-generated Date: Tue, 16 Dec 2025 04:58:17 +0000 Message-Id: <6940e6e9.c217.466ecde6@gitrepo.freebsd.org> The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=242f545e4e31b7fd49f4afac99531adebde55fce commit 242f545e4e31b7fd49f4afac99531adebde55fce Author: Konstantin Belousov AuthorDate: 2025-12-03 23:56:26 +0000 Commit: Konstantin Belousov CommitDate: 2025-12-16 04:57:52 +0000 libc/string: add strdupa(3) and strndupa(3) (cherry picked from commit a98e5d78500193dc7aa352e1f60ac2c6529e2c38) --- include/string.h | 28 ++++++++++++++++++++++++++++ lib/libc/string/Makefile.inc | 4 +++- lib/libc/string/strdup.3 | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/include/string.h b/include/string.h index a362dc9e0f3f..e7fd3db0f0fd 100644 --- a/include/string.h +++ b/include/string.h @@ -149,6 +149,34 @@ void swab(const void * __restrict, void * __restrict, ssize_t); int timingsafe_bcmp(const void *, const void *, size_t); int timingsafe_memcmp(const void *, const void *, size_t); + +#if __has_builtin(__builtin_alloca) +#define strdupa(_Str) (__extension__({ \ + const char *_Str1; \ + size_t _Len; \ + char *_Copy; \ + \ + _Str1 = (_Str); \ + _Len = strlen(_Str1) + 1; \ + _Copy = (char *)__builtin_alloca(_Len); \ + memcpy(_Copy, _Str1, _Len); \ + _Copy; \ +})) + +#define strndupa(_Str, _Maxlen) (__extension__({ \ + const char *_Str1; \ + char *_Copy; \ + size_t _Len; \ + \ + _Str1 = (_Str); \ + _Len = strnlen((_Str1), (_Maxlen)); \ + _Copy = __builtin_alloca(_Len + 1); \ + (void)memcpy(_Copy, _Str1, _Len); \ + _Copy[_Len] = '\0'; \ + _Copy; \ +})) +#endif + #endif /* __BSD_VISIBLE */ #if __POSIX_VISIBLE >= 200112 || defined(_XLOCALE_H_) diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index a18eb62fddb8..689af376ec07 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -76,7 +76,9 @@ MLINKS+=strcoll.3 strcoll_l.3 MLINKS+=strcpy.3 stpcpy.3 \ strcpy.3 stpncpy.3 \ strcpy.3 strncpy.3 -MLINKS+=strdup.3 strndup.3 +MLINKS+=strdup.3 strndup.3 \ + strdup.3 strdupa.3 \ + strdup.3 strndupa.3 MLINKS+=strerror.3 perror.3 \ strerror.3 strerror_l.3 \ strerror.3 strerror_r.3 \ diff --git a/lib/libc/string/strdup.3 b/lib/libc/string/strdup.3 index a19a00f5430b..b48d88d4bac2 100644 --- a/lib/libc/string/strdup.3 +++ b/lib/libc/string/strdup.3 @@ -32,7 +32,9 @@ .Os .Sh NAME .Nm strdup , -.Nm strndup +.Nm strdupa , +.Nm strndup , +.Nm strndupa .Nd save a copy of a string .Sh LIBRARY .Lb libc @@ -41,7 +43,11 @@ .Ft char * .Fn strdup "const char *str" .Ft char * +.Fn strdupa "const char *str" +.Ft char * .Fn strndup "const char *str" "size_t len" +.Ft char * +.Fn strndupa "const char *str" "size_t len" .Sh DESCRIPTION The .Fn strdup @@ -65,6 +71,19 @@ characters from the string always .Dv NUL terminating the copied string. +.Pp +The +.Fn strdupa +function is identical to +.Fn strdup +but allocates the memory with +.Xr alloca 3 . +Similarly, the +.Fn strndupa +function is identical to +.Fn strndup , +but allocates the memory with +.Xr alloca 3 . .Sh RETURN VALUES If insufficient memory is available, NULL is returned and .Va errno @@ -74,6 +93,7 @@ Otherwise, the .Fn strdup family of functions return a pointer to the copied string. .Sh SEE ALSO +.Xr alloca 3 , .Xr free 3 , .Xr malloc 3 , .Xr wcsdup 3 @@ -86,6 +106,12 @@ The .Fn strndup function is specified by .St -p1003.1-2008 . +The +.Fn strdupa +and +.Fn strndupa +functions are extensions, +taken from glibc. .Sh HISTORY The .Fn strdup @@ -95,3 +121,9 @@ The .Fn strndup function was added in .Fx 7.2 . +The +.Fn strdupa +and +.Fn strndupa +functions were added in +.Fx 15.1 .