From nobody Tue Dec 9 00:21:03 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4dQKJb6YMkz6Jcbd for ; Tue, 09 Dec 2025 00:21:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dQKJb5Htjz3Vtc for ; Tue, 09 Dec 2025 00:21:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765239663; 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=/2ZBQSmVZZnngGeDKLIx56TCYNtnDqUgxPH01wQtNcw=; b=L6RocsAjnpilYtPZQGKxVqzMqPV7yqXH5uBDeYZXHYT4wM1ArRoBF51SmDDVW0EwIeUzxd MdFzR83f0mTKtDssJ6H2kpA9NsA3jJjQgnigSV+CXmznk/rn4BZE5O1DxwSj7U5sqvuBNg YkcUlaa1L7U7EK5lD54b4PiyaMR0+QAxSTS3plsiUfZAheNrDtZgS4iXzSZfhldQPuUYWC 7e67iR0jhRP2M/OdAk+/jx8TTli1S+gH8MtBedQ4Dk6QqLSK27huT5pLaW07uWXI8VQU3r KG1T66usEBtYBqeZERHjGIkfhuL+jwruiNNxpkUbxt9N4heOuorWrsNJmYNI4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1765239663; 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=/2ZBQSmVZZnngGeDKLIx56TCYNtnDqUgxPH01wQtNcw=; b=VMM/3HoMSVqOsxqJGAH+7YlfGsHsij1PsfOkj+2t5E1Dsqah5jCcgzWGBGmtekwk5mSQeD ohFE3Wx7jp9GPYB/ChVYrKs1/xNdc4qLBiIrtWqWBChvOSNw0nB+w+3Ze99gykGsisQrZ5 +ePxKOamc3LNedqKPqCZsJhK6f5qFhDXZShjVjMw8yc2U6+W4wDhGBSLNh5A/kynuQCIPD ECoQzIfMjxz8hFaP15ipG6NAEqKrNMEpXJTnZJMzXx+JthtZnwNHpOtyTiTHLx0m60nLqv m6WkcKbTLaxTg/8sGPMbOgqgLKO/LUkeamcwvpbQv3PfnjvI9tPekNIDGb2ReA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1765239663; a=rsa-sha256; cv=none; b=L/HE22MfSVROsvDp1MvRBWfIZBt3nFxr8hwEtJdpu4WAJIz9Mf9CJ4D2EaeL/iP01oNEvI tt6DIog6f+tV3Qk5RSrX2Dx9yAhMueVzr90WQ4mWtdtEZe4NjKRf3GcgjSUjZVYTc7Hb/2 Hoh+id//U0aEAbbRZ5xdAnUxNLzX7INZtYYUn1gF/xh7+gvz5ltgJKhgN6rtvFTdWZR8SF u0Hr6rSSs0Qs7EP2nuLKpTBp6/JvDFoxg04I5odbtQO79ni0LtkylEDKIcc/kZdiblOB5r u5HGeAIi006EIZtUiYDfi4QfgLDG8D+G0lDq/HxcJ/opyQaghhyLteI0gDDBsw== 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 4dQKJb4s4lzb9N for ; Tue, 09 Dec 2025 00:21:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id cea3 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 09 Dec 2025 00:21:03 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: a98e5d785001 - main - libc/string: add strdupa(3) and strndupa(3) List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: a98e5d78500193dc7aa352e1f60ac2c6529e2c38 Auto-Submitted: auto-generated Date: Tue, 09 Dec 2025 00:21:03 +0000 Message-Id: <69376b6f.cea3.4e475826@gitrepo.freebsd.org> The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=a98e5d78500193dc7aa352e1f60ac2c6529e2c38 commit a98e5d78500193dc7aa352e1f60ac2c6529e2c38 Author: Konstantin Belousov AuthorDate: 2025-12-03 23:56:26 +0000 Commit: Konstantin Belousov CommitDate: 2025-12-08 22:34:57 +0000 libc/string: add strdupa(3) and strndupa(3) Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54066 --- 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 a3fa96ebb449..e9aa1b55281a 100644 --- a/include/string.h +++ b/include/string.h @@ -154,6 +154,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 13cff22b9a32..dfe4b4b55bec 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -174,7 +174,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 576fa122b8d6..5968289f5002 100644 --- a/lib/libc/string/strdup.3 +++ b/lib/libc/string/strdup.3 @@ -30,7 +30,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 @@ -39,7 +41,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 @@ -63,6 +69,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 @@ -72,6 +91,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 @@ -84,6 +104,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 @@ -93,3 +119,9 @@ The .Fn strndup function was added in .Fx 7.2 . +The +.Fn strdupa +and +.Fn strndupa +functions were added in +.Fx 15.1 .