aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/string
Commit message (Collapse)AuthorAgeFilesLines
* libc: Use C23 pseudo-types in qualifier-preserving man page synopsesFaraz Vahedi14 days8-42/+42
| | | | | | | Reviewed by: fuz Approved by: fuz (mentor) MFC after: 1 month Pull Request: https://github.com/freebsd/freebsd-src/pull/2288
* libc: Implement qualifier-preserving standard library functionsFaraz Vahedi14 days8-31/+109
| | | | | | | | | | | | | | | | | | | | | | | | | | Several standard library functions are specified to return an unqualified pointer while accepting a pointer to a potentially const-qualified object. N3020 addresses this behaviour, discarding qualifiers due to incompatible pointer types, by introducing qualifier-preserving macros for the affected set of standard library functions. Add `__qualsel()` helper to `<sys/cdefs.h>`, implemented using the generic selection, and define qualifier-preserving macros for that set of functions in `<string.h>`, `<wchar.h>`, and `<stdlib.h>`. Macros are gated on `_STDC_VERSION__ >= 202311L && !__cplusplus`, therefore there is no behavioural change for earlier C modes or C++ translation units. The kernel is likewise unaffected, as it does not include userland headers. As function-like macros, they are transparent except at a call site where the address-of operator is applied, the macro is suppressed via `#undef`, or the identifier appears in parenthesised form; all of which cause the underlying function designator to be used instead. Reviewed by: fuz Approved by: fuz (mentor) MFC after: 1 month Pull Request: https://github.com/freebsd/freebsd-src/pull/2288
* mem*.3: stop refering to stringsBrooks Davis2026-06-217-30/+30
| | | | | | | | | | | POSIX and the C standard now refer to objects and sequences rather than confusingly revering to strings. Also update bcopy(3) and bzero(3). Reviewed by: emaste Sponsored by: Innovate UK Differential Revision: https://reviews.freebsd.org/D57680
* libc/fls*.c: use *_WIDTH macros over CHAR_BIT * sizeof(type)Robert Clausecker2026-04-223-3/+3
| | | | | | Makes the code easier to understand. MFC after: 1 week
* bcmp(3): update manpage to following the Posix StandardPouria Mousavizadeh Tehrani2026-02-021-1/+9
| | | | | | Reviewed by: glebius Approved by: glebius (mentor) Differential Revision: https://reviews.freebsd.org/D52980
* swab: Correctly treat the data as misalignedJohn Baldwin2026-01-141-3/+10
| | | | | | | | | | | | | | | | | | | | | 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
* libc/string: add strdupa(3) and strndupa(3)Konstantin Belousov2025-12-082-2/+36
| | | | | | | Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54066
* libc/string: put source files list one item per lineKonstantin Belousov2025-12-081-23/+121
| | | | | | | Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54066
* man/man3: add cross references to stdbit(3)Robert Clausecker2025-11-301-0/+2
| | | | | | | | Add cross references to relevant stdbit man pages Approved by: markj (mentor) MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D53661
* ffs.3: Add a STANDARDS section to the manual pageGordon Bergling2025-10-251-1/+12
| | | | | | | | | 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
* memchr.3: Operates on objects, not stringsEd Maste2025-07-251-3/+3
| | | | | | | Suggested by: avg Reviewed by: avg Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D51523
* memchr.3: mention the len argument in the functions descriptionKonstantin Belousov2025-07-221-5/+11
| | | | | | | | | | | 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
* manuals: Align our tree with upstream C23 macroAlexander Ziaee2025-06-122-2/+2
| | | | | | | 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)
* swab: Fix implementation to support overlapping copiesWarner Losh2025-01-061-7/+10
| | | | | | | | | | | | | | | | | | | 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
* memcmp.3: Clarify return valueEd Maste2024-11-201-16/+25
| | | | | | | | | | 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
* libc: indicate existing functions that are POSIX 2024Ed Maste2024-11-151-3/+3
| | | | | | Reviewed by: brooks, imp Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47581
* lib/libc/string: apply SSP hardening and tests to memset_explicitRobert Clausecker2024-11-141-1/+2
| | | | | Reviewed by: emaste, kevans Differential Revision: https://reviews.freebsd.org/D47286
* lib/libc/string: add memset_explicit() for compliance with C23Robert Clausecker2024-11-144-3/+49
| | | | | | | Patterned after explicit_bzero, visible from C23 onwards. Reviewed by: emaste, kevans Differential Revision: https://reviews.freebsd.org/D47286
* contrib/mandoc: add -isoC-2024 and -svid1 to mdoc(7)Robert Clausecker2024-10-261-4/+2
| | | | | | | | 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
* manuals: Fix "unusual .Xr" warnings with a scriptGraham Percival2024-10-151-1/+0
| | | | | | | | | | | | | 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
* include: ssp: don't shadow the mempcpy builtinKyle Evans2024-07-161-1/+1
| | | | | | | | | | | 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
* Remove residual blank line at start of MakefileWarner Losh2024-07-151-1/+0
| | | | | | | 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
* include: ssp: fortify <wchar.h>Kyle Evans2024-07-1312-12/+29
| | | | | | | | | | | | 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
* include: ssp: round out fortification of current set of headersKyle Evans2024-07-135-2/+12
| | | | | | | | | | | | | | | | | | | | 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
* Prepare the system for _FORTIFY_SOURCEKyle Evans2024-05-136-0/+17
| | | | | | | | | | | | | | | 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
* libc: make strerror_rl() usable for libcKonstantin Belousov2024-04-231-5/+5
| | | | | | | Reviewed by: imp Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D44916
* swab.c(libc): use a simplified version of byte swappingrilysh2024-04-231-47/+11
| | | | | | | | | | | | | | 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
* wmemchr(3): fix prototypes for wcpcpy() and wcpncpy()Konstantin Belousov2024-03-211-3/+3
| | | | | | | PR: 277855 Reported by: Paul Floyd <pjfloyd@wanadoo.fr> Sponsored by: The FreeBSD Foundation MFC after: 3 days
* strnlen(3): add HISTORYDavid E. O'Brien2024-03-151-0/+5
| | | | Complete 4b7f35db's HISTORY's train of thought
* timingsafe_bcmp(3) was MFC'ed to 11.1David E. O'Brien2024-01-061-1/+1
|
* lib/libc/string: document restrict qualification of memccpy() argumentsRobert Clausecker2023-12-253-6/+39
| | | | | | | | | | | | | 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
* lib: Automated cleanup of cdefs and other formattingWarner Losh2023-11-272-4/+0
| | | | | | | | | | | | | | | | 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
* sccs: Manual changesWarner Losh2023-11-272-14/+5
| | | | | | | | | | | | | | | 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
* lib: Remove ancient SCCS tags.Warner Losh2023-11-2761-148/+0
| | | | | | | | 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
* Add a HISTORY section for memcpy(3) and mempcpy(3)Gordon Bergling2023-11-181-2/+13
| | | | | | | | | | 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
* libc: Remove empty comments in Symbol.mapBrooks Davis2023-11-151-3/+0
| | | | | | | These were left over from $FreeBSD$ removal. Reviewed by: emaste Differential Revision: https://reviews.freebsd.org/D42612
* libc: Purge unneeded cdefs.hWarner Losh2023-11-0167-67/+0
| | | | | | | | | 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
* strlcpy/strlcat: Remove references to snprintfWarner Losh2023-10-271-20/+7
| | | | | | | | | | | | 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
* */string.3,arch.7,environ.7: add cross-references to simd(7)Robert Clausecker2023-09-051-2/+3
| | | | | | | | | | | 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
* libc: fix history for strverscmp(3) and versionsort(3)Mina Galić2023-08-311-2/+2
| | | | | | | | | 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
* Document strnstr()'s history.David E. O'Brien2023-08-221-0/+6
|
* Remove $FreeBSD$: one-line nroff patternWarner Losh2023-08-1632-32/+0
| | | | Remove /^\.\\"\s*\$FreeBSD\$$\n/
* Remove $FreeBSD$: two-line nroff patternWarner Losh2023-08-166-12/+0
| | | | Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/
* Remove $FreeBSD$: one-line sh patternWarner Losh2023-08-161-1/+0
| | | | Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
* Remove $FreeBSD$: one-line .c patternWarner Losh2023-08-1683-166/+0
| | | | Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
* Remove $FreeBSD$: one-line .h patternWarner Losh2023-08-161-1/+0
| | | | Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
* Remove $FreeBSD$: two-line .h patternWarner Losh2023-08-161-2/+0
| | | | Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
* lib/libc/string/bcmp.c: fix integer overflow bugRobert Clausecker2023-07-161-2/+2
| | | | | | | | | | | | | | | | | 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
* lib/libc/string/ffs*.c: work around gcc warningRobert Clausecker2023-07-143-3/+3
| | | | | | | | | | | | | | | 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
* lib/libc/string: replace ffs/fls implementations with clang builtinsRobert Clausecker2023-07-036-42/+34
| | | | | | | | | | | | | | 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