summaryrefslogtreecommitdiff
path: root/lib/libc/string
AgeCommit message (Collapse)Author
6 daysswab: Correctly treat the data as misalignedJohn Baldwin
The __aligned attribute in the previous version applied to the location of the pointers, not the data the pointers pointed to. While this could be fixed by applying the attribute to a local typedef of uint16_t, just using memcpy() for the unaligned access is simpler and ISO C. This fixes the build on CHERI architectures which do not support misaligned pointers and were thus failing with: lib/libc/string/swab.c:12:18: error: alignment (1) of 'const uint16_t *' (aka 'const unsigned short *') is less than the required capability alignment (16) [-Werror,-Wcheri-capability-misuse] 12 | const uint16_t *f __aligned(1) = from; | Co-authored by: Jessica Clarke <jrtc27@FreeBSD.org> Fixes: 02ebbc781f08 ("swab: Fix implementation to support overlapping copies") Sponsored by: AFRL, DARPA Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D54399
2025-12-09libc/string: add strdupa(3) and strndupa(3)Konstantin Belousov
Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54066
2025-12-09libc/string: put source files list one item per lineKonstantin Belousov
Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54066
2025-11-30man/man3: add cross references to stdbit(3)Robert Clausecker
Add cross references to relevant stdbit man pages Approved by: markj (mentor) MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D53661
2025-10-25ffs.3: Add a STANDARDS section to the manual pageGordon Bergling
The ffs() function conforms to IEEE Std 1003.1-2008 ("POSIX.1"). The ffsl() and ffsll() functions conform to IEEE Std 1003.1-2024 ("POSIX.1"). Reviewed by: ziaee MFC after: 5 days Differential Revision: https://reviews.freebsd.org/D53352
2025-07-25memchr.3: Operates on objects, not stringsEd Maste
Suggested by: avg Reviewed by: avg Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D51523
2025-07-22memchr.3: mention the len argument in the functions descriptionKonstantin Belousov
Otherwise, its use is only specified in the error case, which is somewhat obscure. Also properly typeset NULL. Reviewed by: pauamma_gundo.com Discussed with: emaste Sponsored by: The FreeBSD Foundation MFC after: 3 days Differential revision: https://reviews.freebsd.org/D51424
2025-06-12manuals: Align our tree with upstream C23 macroAlexander Ziaee
Switch all instances of the -isoC-2024 macro to -isoC-2023 selected by upstream. Keep -isoC-2024 defined, but deprecated, for backwards compat. MFC after: 3 days (I will resolve merge conflicts with cdefs)
2025-01-06swab: Fix implementation to support overlapping copiesWarner Losh
A number of image processing packages assume that swab() can handle to and from being the same. However, POSIX.1 states that overlapping buffers produces undefined results. Our old implementation would produce coherent results, but the recent change to the musl-inspired code does not. Since there's complaints in the forums for these image processing packages for musl and now FreeBSD, update the algorithm to just read a word at a time and bswap16 the results. All FreeBSD's architecutres support unaligned access in userland, and swab is not used in the kernel (g_part_apm has its own copy), so opt for even simpler code that's easier to understand. This makes the overlapping behavior match i386 again, since its assembler routine for swab handles overlapping correctly. PR: 283698 Sponsored by: Netflix Reviewed by: nwhitehorn Differential Revision: https://reviews.freebsd.org/D48259
2024-11-20memcmp.3: Clarify return valueEd Maste
The return value is not required to be the difference between the differing bytes, only less than zero, zero, or greater than zero. Reviewed by: fuz Event: Kitchener-Waterloo Hackathon 202406 Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47683
2024-11-14libc: indicate existing functions that are POSIX 2024Ed Maste
Reviewed by: brooks, imp Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47581
2024-11-14lib/libc/string: apply SSP hardening and tests to memset_explicitRobert Clausecker
Reviewed by: emaste, kevans Differential Revision: https://reviews.freebsd.org/D47286
2024-11-14lib/libc/string: add memset_explicit() for compliance with C23Robert Clausecker
Patterned after explicit_bzero, visible from C23 onwards. Reviewed by: emaste, kevans Differential Revision: https://reviews.freebsd.org/D47286
2024-10-26contrib/mandoc: add -isoC-2024 and -svid1 to mdoc(7)Robert Clausecker
C23 (ISO/IEC 9899:2024) is not out yet, but will be shortly. SVID1 is needed for the history section of memccpy(3). Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D47284
2024-10-15manuals: Fix "unusual .Xr" warnings with a scriptGraham Percival
These were reported by `mandoc -T lint ...` as warnings: - unusual Xr order - unusual Xr punctuation Fixes made by script in https://github.com/Tarsnap/freebsd-doc-scripts Signed-off-by: Graham Percival <gperciva@tarsnap.com> Reviewed by: mhorne, Alexander Ziaee <concussious.bugzilla@runbox.com> Sponsored by: Tarsnap Backup Inc. Pull Request: https://github.com/freebsd/freebsd-src/pull/1464
2024-07-16include: ssp: don't shadow the mempcpy builtinKyle Evans
GCC emits a warning about shadowing a builtin with our mempcpy declaration, so switch it to using the same model as memcpy() and use the apparently-existing __builtin___mempcpy_chk(). Reviewed by: kib (earlier version), markj Sponsored by: Klara, Inc. Sponsored by: Stormshield Differential Revision: https://reviews.freebsd.org/D45976
2024-07-15Remove residual blank line at start of MakefileWarner Losh
This is a residual of the $FreeBSD$ removal. MFC After: 3 days (though I'll just run the command on the branches) Sponsored by: Netflix
2024-07-13include: ssp: fortify <wchar.h>Kyle Evans
This includes all of the w*() equivalents to str*()/mem*() implemented in more or less the same way. For these ones, we'll just use header-only implementations from the start to stop further cluttering the libc symbol table. Reviewed by: markj Sponsored by: Klara, Inc. Sponsored by: Stormshield Differential Revision: https://reviews.freebsd.org/D45682
2024-07-13include: ssp: round out fortification of current set of headersKyle Evans
ssp/ssp.h needed some improvements: - `len` isn't always a size_t, it may need casted - In some cases we may want to use a len that isn't specified as a parameter (e.g., L_ctermid), so __ssp_redirect() should be more flexible. - In other cases we may want additional checking, so pull all of the declaration bits out of __ssp_redirect_raw() so that some functions can implement the body themselves. strlcat/strlcpy should be the last of the fortified functions that get their own __*_chk symbols, and these cases are only done to be consistent with the rest of the str*() set. Reviewed by: markj Sponsored by: Klara, Inc. Sponsored by: Stormshield Differential Revision: https://reviews.freebsd.org/D45679
2024-05-13Prepare the system for _FORTIFY_SOURCEKyle Evans
Notably: - libc needs to #undef some of the macros from ssp/* for underlying implementations - ssp/* wants a __RENAME() macro (snatched more or less from NetBSD) There's some extra hinkiness included for read(), since libc spells it as "_read" while the rest of the world spells it "read." Reviewed by: imp, ngie Sponsored by: Stormshield Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D32307
2024-04-23libc: make strerror_rl() usable for libcKonstantin Belousov
Reviewed by: imp Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D44916
2024-04-22swab.c(libc): use a simplified version of byte swappingrilysh
This version of swab function simplifies the logic of swapping adjacent bytes. Previous version of swab() used an arbitrary unrolling, which was relevant back in the day but unnecessary for modern compilers, as if the input size is known at compile time, they can do it automatically. This version of swab() is inspired by musl. A similar version can be found at: https://github.com/openbsd/src/blob/master/lib/libc/string/swab.c Signed-off-by: rilysh <nightquick@proton.me> Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/1086
2024-03-21wmemchr(3): fix prototypes for wcpcpy() and wcpncpy()Konstantin Belousov
PR: 277855 Reported by: Paul Floyd <pjfloyd@wanadoo.fr> Sponsored by: The FreeBSD Foundation MFC after: 3 days
2024-03-15strnlen(3): add HISTORYDavid E. O'Brien
Complete 4b7f35db's HISTORY's train of thought
2024-01-05timingsafe_bcmp(3) was MFC'ed to 11.1David E. O'Brien
2023-12-25lib/libc/string: document restrict qualification of memccpy() argumentsRobert Clausecker
POSIX.1-2004 and the upcoming C23 agree that memccpy()'s arguments are restrict qualified and must not overlap. In 2002, restrict qualifiers were added to <string.h>'s declaration of the function. Make things official and document that the arguments must not overlap. See also: 61b60edfd3fff20f884419f8097870c7045315c9 Approved by: kib MFC after: 1 month MFC to: stable/14
2023-11-26lib: Automated cleanup of cdefs and other formattingWarner Losh
Apply the following automated changes to try to eliminate no-longer-needed sys/cdefs.h includes as well as now-empty blank lines in a row. Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/ Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/ Remove /\n+#if.*\n#endif.*\n+/ Remove /^#if.*\n#endif.*\n/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/ Sponsored by: Netflix
2023-11-26sccs: Manual changesWarner Losh
For the uncommon items: Go through the tree and remove sccs tags that didn't fit any nice pattern. If in the neighborhood, other SCM tags were removed when they were detritis of long-ago CVS somehow in the early mists of the project. Some adjacent copyrights stringswere removed (they duplicated the copyright notices in the file). This also removed non-standard formations of omission of SCCS tags (usually by adding an extra #if 0 somewhere. After this commit, a number of strings tagged with the 'what' @(#) prefix remain, but they are primarily copyright notices. Sponsored by: Netflix
2023-11-26lib: Remove ancient SCCS tags.Warner Losh
Remove ancient SCCS tags from the tree, automated scripting, with two minor fixup to keep things compiling. All the common forms in the tree were removed with a perl script. Sponsored by: Netflix
2023-11-18Add a HISTORY section for memcpy(3) and mempcpy(3)Gordon Bergling
The memcpy() function first appeared in AT&T System V UNIX and was reimplemented for 4.3BSD-Tahoe. The mempcpy() function first appeared in FreeBSD 13.1. PR: 272227 MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D42630
2023-11-15libc: Remove empty comments in Symbol.mapBrooks Davis
These were left over from $FreeBSD$ removal. Reviewed by: emaste Differential Revision: https://reviews.freebsd.org/D42612
2023-11-01libc: Purge unneeded cdefs.hWarner Losh
These sys/cdefs.h are not needed. Purge them. They are mostly left-over from the $FreeBSD$ removal. A few in libc are still required for macros that cdefs.h defines. Keep those. Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D42385
2023-10-27strlcpy/strlcat: Remove references to snprintfWarner Losh
While strlcpy and snprintf are somewhat similar, there's big differences between strlcat and snprintf which leads to confusion. Remove the comparison, since it's ultimately not that useful: the snprintf man page has similar language to strlcpy, so it doesn't provide a better reference. The two implementations are otherwise unrelated. Reviewed by: bcr Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D27228
2023-09-05*/string.3,arch.7,environ.7: add cross-references to simd(7)Robert Clausecker
With the new simd-dispatch framework documented in simd(7), add cross-references to the new man pages to appropriate places. Sponsored by: The FreeBSD Foundation Approved by: emaste MFC to: stable/14 MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D41697
2023-08-31libc: fix history for strverscmp(3) and versionsort(3)Mina Galić
strverscmp(3) and versionsort(3) where first released in 13.2 PR: 273401 Reviewed by: kib MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D41617
2023-08-21Document strnstr()'s history.David E. O'Brien
2023-08-16Remove $FreeBSD$: one-line nroff patternWarner Losh
Remove /^\.\\"\s*\$FreeBSD\$$\n/
2023-08-16Remove $FreeBSD$: two-line nroff patternWarner Losh
Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/
2023-08-16Remove $FreeBSD$: one-line sh patternWarner Losh
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
2023-08-16Remove $FreeBSD$: one-line .c patternWarner Losh
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16Remove $FreeBSD$: one-line .h patternWarner Losh
Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
2023-08-16Remove $FreeBSD$: two-line .h patternWarner Losh
Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
2023-07-16lib/libc/string/bcmp.c: fix integer overflow bugRobert Clausecker
bcmp() returned the number of remaining bytes when the main loop exits. In case of a match, this is zero, else a positive integer. On systems where SIZE_MAX > INT_MAX, the implicit conversion from size_t to int in the return value may cause the number of remaining bytes to overflow, becoming zero and falsely indicating a successful comparison. Fix the bug by always returning 0 on equality, 1 otherwise. PR: 272474 Approved by: emaste Reviewed by: imp MFC After: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D41011
2023-07-14lib/libc/string/ffs*.c: work around gcc warningRobert Clausecker
Gcc warns of infinite recursion if we use __builtin_ffs*() to implement ffs*(). This is because gcc uses ffs() to implement these on some platforms. Sidestep the warning by using __builtin_ctz*() for these. Sponsored by: FreeBSD Foundation Reported by: jlduran@gmail.com, jhb Fixes: ee8b0c43 (D40730) Reviewed by: jhb, mhorne Approved by: jhb MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D40966
2023-07-03lib/libc/string: replace ffs/fls implementations with clang builtinsRobert Clausecker
Most architectures we support (except for riscv64) have instructions to compute these functions very quickly. Replace old code with the ffs and clz builtin functions, allowing clang to generate good code for all architectures. As a consequence, toss out arm and i386 ffs() implementations. Sponsored by: FreeBSD Foundation Approved by: mhorne MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D40730
2023-06-29Correctly spell illumos (all lowercase) in man pagesYuri Pankov
From illumos FAQ: "illumos (pronounced i-llu-MOS and written in lowercase)"
2023-06-09Correct man page URI formatting.David E. O'Brien
Don't format URL's embedding a ',' where one doesn't exist; so that one may copy-n-paste the displayed URL into a browser.
2023-05-12spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSDWarner Losh
The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch up to that fact and revert to their recommended match of BSD-2-Clause. Discussed with: pfg MFC After: 3 days Sponsored by: Netflix
2023-03-30memmem: add a note about other systems which have memmemEd Maste
memmem started as a GNU extension but is now widely available. Reviewed by: mhorne (slightly earlier version) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D39337
2023-02-04Fix truncation when ssize_t is larger than MAX_INTAlfonso
Casting to int truncates size on some platforms, resulting swab not copying all the data. Cast len to size_t to avoid right shifting a signed value: we know here it's > 0, so we can safely cast it w/o losing precision. In addition, be more careful with signedness of char pointers and temporaries. Downgrade tmp from unsigned long to unsigned char since we're only reading and writing characters. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/516