aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/string
Commit message (Collapse)AuthorAgeFilesLines
...
* 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
* Correctly spell illumos (all lowercase) in man pagesYuri Pankov2023-06-291-2/+2
| | | | | | From illumos FAQ: "illumos (pronounced i-llu-MOS and written in lowercase)"
* Correct man page URI formatting.David E. O'Brien2023-06-101-2/+1
| | | | | 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.
* spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSDWarner Losh2023-05-1219-19/+19
| | | | | | | | | 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
* memmem: add a note about other systems which have memmemEd Maste2023-03-301-2/+3
| | | | | | | | 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
* Fix truncation when ssize_t is larger than MAX_INTAlfonso2023-02-051-6/+7
| | | | | | | | | | | | | | 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
* libc: Fix build with WITHOUT_MACHDEP_OPTIMIZATIONS=YES set.Gleb Popov2023-01-101-1/+6
| | | | | | | | | | Test Plan: `make buildword WITHOUT_MACHDEP_OPTIMIZATIONS=YES` on 14-CURRENT and 13-STABLE Reviewed by: emaste Differential Revision: https://reviews.freebsd.org/D38017 PR: 266900
* libc: Add strverscmp(3) and versionsort(3)Aymeric Wibo2022-08-254-2/+150
| | | | | | | | | | | | | | | Add a strverscmp(3) function to libc, a GNU extension I implemented by reading its glibc manual page. It orders strings following a much more natural ordering (e.g. "ent1 < ent2 < ent10" as opposed to "ent1 < ent10 < ent2" with strcmp(3)'s lexicographic ordering). Also add versionsort(3) for use as scandir(3)'s compar argument. Update manual page for scandir(3) and add one for strverscmp(3). Reviewed by: pstef, gbe, kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D35807
* libc: drop "All rights reserved" from Foundation copyrightsEd Maste2022-08-049-9/+7
| | | | | | | | | This has already been done for most files that have the Foundation as the only listed copyright holder. Do it now for files that list multiple copyright holders, but have the Foundation copyright in its own section. Sponsored by: The FreeBSD Foundation
* libc: Add HISTORY sections to the manual pagesGordon Bergling2022-05-053-3/+29
| | | | | | | | | | There are some sections which could be improved and work to do so is on going. The work will be covered via 'X-MFC-WITH' commits. Obtained from: OpenBSD MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D34759
* libc: correct SPDX tag on strstr.cEd Maste2022-01-161-1/+1
| | | | | | | It was obtained from musl, and is MIT licensed. MFC after: 3 days Sponsored by: The FreeBSD Foundation
* libc: fix misleading comment in strstrEd Maste2022-01-161-1/+1
| | | | Obtained from: musl c53e9b239418
* memcpy.3: remove BUGS section allowing overlapping stringsEd Maste2021-08-241-16/+0
| | | | | | | | | | | | | | | The removed text claimed that memcpy is implemented using bcopy and thus strings may overlap. Use of bcopy is an implementation detail that is no longer true, even if the implementation (on some archs) does allow overlap. In any case behaviour is undefined per the C standard if memcpy is called with overlapping objects, and this man page already claimed that src and dst may not overlap. Reviewed by: kib Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D31192
* libc: Disable ASAN for certain string functionsAlex Richardson2021-08-021-0/+9
| | | | | | | | | | They deliberately read out-of-bounds values to avoid byte-by-byte loads and check multiple bytes at once. While this will work on x86, it is flagged as an out-of-bounds read with ASAN, so we have to disable instrumentation here. This also causes bounds errors for CHERI, so in CheriBSD we use implementations that avoid OOB reads. Differential Revision: https://reviews.freebsd.org/D31045
* libc: add mempcpy(3) and wmempcpy(3)Konstantin Belousov2021-07-156-9/+114
| | | | | | | Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D31180
* Change strmode argument type to mode_tAlfonso Gregory2021-06-291-1/+1
| | | | | | | | Finally, we have the correct function definition for strmode. NetBSD/OpenBSD did this many years ago. This code is weird sign extension safe. Reviewed by: imp@ Pull Request: https://github.com/freebsd/freebsd-src/pull/493
* libc/string/memset.c: Use unsigned long for storesAlex Richardson2021-04-201-8/+11
| | | | | | | | | | While most 64-bit architectures have an assembly implementation of this file, RISC-V does not. As we now store 8 bytes instead of 4 it should speed up RISC-V. Reviewed By: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D29536
* libc/string/bcopy.c: Use intptr_t as the copy typeAlex Richardson2021-04-201-7/+5
| | | | | | | | | | | | | While most 64-bit architectures have an assembly implementation of this file RISC-V does not. As we now copy 8 bytes instead of 4 it should speed up RISC-V. Using intptr_t instead of int also allows using this file for CHERI pure-capability code since trying to copy pointers using integer loads/stores will invalidate pointers. Reviewed By: kib Obtained from: CheriBSD (partially) MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D29535
* libc: Use musl's optimized strchr and strchrnulEd Maste2021-03-022-73/+67
| | | | | | | | | | | | Parentheses added to HASZERO macro to avoid a GCC warning, and formatted with clang-format as we have adopted these and don't consider them 'contrib' code. Obtained from: musl (snapshot at commit 4d0a82170a25) Reviewed by: kib (libc integration), mjg (both earlier) MFC after: 1 month Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D17630
* Remove __NO_TLS.Konstantin Belousov2021-02-231-5/+0
| | | | | | | | | All supported platforms support thread-local vars and __thread. Reviewed by: emaste Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D28796
* Restore the augmented strlen commentaryMateusz Guzik2021-02-081-11/+4
| | | | ... lost in revert
* Revert "Reimplement strlen"Mateusz Guzik2021-02-031-28/+54
| | | | | | | | | This reverts commit 710e45c4b8539d028877769f1a4ec088c48fb5f1. It breaks for some corner cases on big endian ppc64. Given the stage of the release process it is best to revert for now. Reported by: jhibbits
* Reimplement strlenMateusz Guzik2021-01-291-54/+28
| | | | | | | | | | | | | | | | | | | | | | | The previous code neglected to use primitives which can find the end of the string without having to branch on every character. While here augment the somewhat misleading commentary -- strlen as implemented here leaves performance on the table, especially so for userspace. Every arch should get a dedicated variant instead. In the meantime this commit lessens the problem. Tested with glibc test suite. Naive test just calling strlen in a loop on Haswell (ops/s): $(perl -e "print 'A' x 3"): before: 211198039 after: 338626619 $(perl -e "print 'A' x 100"): before: 83151997 after: 98285919
* strerror.3: Fix whitespace issue introduced in r368714Jessica Clarke2020-12-181-1/+1
| | | | | | | MFC with: 368714 Notes: svn path=/head/; revision=368770
* strerror.3: Add an example for perror()Mateusz Piotrowski2020-12-171-1/+26
| | | | | | | | | | | This is a nice and quick reference. Reviewed by: jilles, yuripv MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D27623 Notes: svn path=/head/; revision=368714
* Implement strerror_l().Konstantin Belousov2020-12-164-21/+82
| | | | | | | | | | | | | | Only for the arches that provide user-mode TLS. PR: 251651 Requested by: yuri Discussed with: emaste, jilles, tijl Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D27495 MFC after: 2 weeks Notes: svn path=/head/; revision=368692
* strdup.3: Function appeared in 4.3BSD-Reno, not 4.4BSDJessica Clarke2020-12-141-1/+1
| | | | | | | | | | | | | Linux claims 4.3BSD, we claim 4.4BSD and OpenBSD claims 4.3BSD-Reno. It turns out that OpenBSD got it right: the function was added in late 1988 a few months after 4.3BSD-Tahoe, well in advance of 4.3BSD-Reno. Reviewed by: bcr Approved by: bcr Differential Revision: https://reviews.freebsd.org/D27392 Notes: svn path=/head/; revision=368625
* libc: fix undefined behavior from signed overflow in strstr and memmemEd Maste2020-11-192-8/+8
| | | | | | | | | | | | | | | | | | | | | unsigned char promotes to int, which can overflow when shifted left by 24 bits or more. this has been reported multiple times but then forgotten. it's expected to be benign UB, but can trap when built with explicit overflow catching (ubsan or similar). fix it now. note that promotion to uint32_t is safe and portable even outside of the assumptions usually made in musl, since either uint32_t has rank at least unsigned int, so that no further default promotions happen, or int is wide enough that the shift can't overflow. this is a desirable property to have in case someone wants to reuse the code elsewhere. musl commit: 593caa456309714402ca4cb77c3770f4c24da9da Obtained from: musl Notes: svn path=/head/; revision=367823
* libc: optimize memmem two-way bad character shiftEd Maste2020-11-191-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | first, the condition (mem && k < p) is redundant, because mem being nonzero implies the needle is periodic with period exactly p, in which case any byte that appears in the needle must appear in the last p bytes of the needle, bounding the shift (k) by p. second, the whole point of replacing the shift k by mem (=l-p) is to prevent shifting by less than mem when discarding the memory on shift, in which case linear time could not be guaranteed. but as written, the check also replaced shifts greater than mem by mem, reducing the benefit of the shift. there is no possible benefit to this reduction of the shift; since mem is being cleared, the full shift is valid and more optimal. so only replace the shift by mem when it would be less than mem. musl commits: 8f5a820d147da36bcdbddd201b35d293699dacd8 122d67f846cb0be2c9e1c3880db9eb9545bbe38c Obtained from: musl MFC after: 2 weeks Notes: svn path=/head/; revision=367822
* clang-format libc string functions imported from muslEd Maste2020-11-183-124/+193
| | | | | | | | | | | We have adopted these and don't consider them 'contrib' code, so bring them closer to style(9). This is a followon to r315467 and r351700. MFC after: 1 week Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=367821
* strtok(3): make it easier to find the RETURN VALUES sectionPiotr Pawel Stefaniak2020-06-221-1/+1
| | | | | | | MFC after: 1 week Notes: svn path=/head/; revision=362495
* libc manpages: various improvements from NetBSDGordon Bergling2020-06-141-1/+11
| | | | | | | | | | | | | | - Add STANDARDS and HISTORY sections within the appropriate manpages - Mention two USENIX papers within kqueue(2) and strlcpy(3) Reviewed by: bcr (mentor) Approved by: bcr (mentor) Obtained from: NetBSD MFC after: 7 days Differential Revision: https://reviews.freebsd.org/D24650 Notes: svn path=/head/; revision=362171
* strdup.3: Slightly canonicalize OOM return/error statusConrad Meyer2020-05-051-9/+14
| | | | | | | | | | Attempted to clean up the language around "this is a malloc'd object." May be passed as a parameter to free(3) is a bit obtuse. Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=360664
* memset.3: better fix previous typoEitan Adler2020-02-081-2/+3
| | | | | | | | | Upon re-reading the whole sentence this is a better fix. MFC with: r357681 Notes: svn path=/head/; revision=357683
* memset.3: fix a a duplicate wordEitan Adler2020-02-081-1/+1
| | | | Notes: svn path=/head/; revision=357681
* [PPC64] strncpy optimizationLeandro Lupori2020-01-151-1/+10
| | | | | | | | | | | | Assembly optimization of strncpy for PowerPC64, using double words instead of bytes to copy strings. Submitted by: Leonardo Bianconi <leonardo.bianconi_eldorado.org.br> (original version) Reviewed by: jhibbits Differential Revision: https://reviews.freebsd.org/D15369 Notes: svn path=/head/; revision=356766
* [PPC64] strcpy optimizationLeandro Lupori2020-01-151-1/+10
| | | | | | | | | | | | Assembly optimization of strcpy for PowerPC64, using double words instead of bytes to copy strings. Submitted by: Leonardo Bianconi <leonardo.bianconi_eldorado.org.br> (original version) Reviewed by: jhibbits Differential Revision: https://reviews.freebsd.org/D15368 Notes: svn path=/head/; revision=356765
* libc: Use musl's optimized memchrEd Maste2019-09-021-41/+40
| | | | | | | | | | | | Parentheses added to HASZERO macro to avoid a GCC warning. Reviewed by: kib, mjg Obtained from: musl (snapshot at commit 4d0a82170a) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D17631 Notes: svn path=/head/; revision=351700
* Fix a possible segfault in wcsxfrm(3) and wcsxfrm_l(3).Dimitry Andric2019-08-071-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | If the length of the source wide character string, passed in via the "size_t n" parameter, is set to zero, the function should only return the required length for the destination wide character string. In this case, it should *not* attempt to write to the destination, so the "dst" parameter is permitted to be NULL. However, when the internally called _collate_wxfrm() function returns an error, such as when using the "C" locale, as a fallback wcscpy(3) or wcsncpy(3) are used. But if the input length is zero, wcsncpy(3) will be called with a length of -1! If the "dst" parameter is NULL, this will immediately result in a segfault, or if "dst" is a valid pointer, it will most likely result in unexpectedly overwritten memory. Fix this by explicitly checking for an input length greater than zero, before calling wcsncpy(3). Note that a similar situation does not occur in strxfrm(3), the plain character version of this function, as it uses strlcpy(3) for the error case. The strlcpy(3) function does not write to the destination if the input length is zero. MFC after: 1 week Notes: svn path=/head/; revision=350697
* libc: update strstr implementation to match muslEd Maste2019-04-091-2/+1
| | | | | | | | | | | | | | | musl commits: 122d67f846cb0be2c9e1c3880db9eb9545bbe38c 0239cd0681e889a269fb7691f60e81ef8d081e6b 8f5a820d147da36bcdbddd201b35d293699dacd8 Submitted by: David CARLIER <devnexen_gmail.com> Obtained from: musl MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D19834 Notes: svn path=/head/; revision=346054
* Fix WITHOUT_NLS build after r342551.Konstantin Belousov2018-12-281-1/+1
| | | | | | | | | Reported by: gj MFC after: 13 days Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=342574
* Bump sys_errlist size to keep ABI backward-compatible for some time.Konstantin Belousov2018-12-271-9/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | Addition of the new errno values requires adding new elements to sys_errlist array, which is actually ABI-incompatible, since ELF records the object size. Expand array in advance to 150 elements so that we have our users to go over the issue only once, at least until more than 53 new errors are added. I did not bumped the symbol version, same as it was not done for previous increases of the array size. Runtime linker only copies as much data into binary object on copy relocation as the binary'object specifies. This is not fixable for binaries which access sys_errlist directly. While there, correct comment and calculation of the temporary buffer size for the message printed for unknown error. The on-stack buffer is used only for the number and delimiter since r108603. Requested by: mckusick Reviewed by: mckusick, yuripv MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D18656 Notes: svn path=/head/; revision=342551
* Avoid OOB reads in memmem(3).Gleb Smirnoff2018-10-151-9/+9
| | | | | | | | | | | | | | | | commit 51bdcdc424bd7169c8cccdc2de7cad17f5ea0f70 Author: Alexander Monakov <amonakov@ispras.ru> Date: Fri Jun 30 00:35:33 2017 +0300 fix OOB reads in Xbyte_memmem Reported by Leah Neukirchen. Reviewed by: emaste Approved by: re (kib) Notes: svn path=/head/; revision=339370
* Provide set_constraint_handler_s(3) man page.Konstantin Belousov2018-08-191-0/+1
| | | | | | | | | | | | Mention abort_handler_s(3) and ignore_handler_s(3), provide cross-reference from memset(3). Submitted by: Yuri Pankov <yuripv@yuripv.net> MFC after: 3 days Differential revision: https://reviews.freebsd.org/D16797 Notes: svn path=/head/; revision=338051
* Clarify that memset_s(3) requires __STDC_WANT_LIB_EXT1__ for visibility.Konstantin Belousov2018-08-191-5/+5
| | | | | | | | | | | Fix typos and other nits. Submitted by: Yuri Pankov <yuripv@yuripv.net> MFC after: 3 days Differential revision: https://reviews.freebsd.org/D16797 Notes: svn path=/head/; revision=338049
* strdup.3: Document standardization in POSIXConrad Meyer2018-06-061-1/+10
| | | | | | | Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=334738
* strcpy.3: Improve legibility and clarityConrad Meyer2018-06-061-22/+30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In the DESCRIPTION, put the more commonly used functions first in the corresponding sentence, to help catch the eye. Pull out the note about overlapping buffers to its own paragraph, as it applies to all routines documented by this page. Emphasize the potentially surprising strncpy(3) behavior of zero-filling the remainder of a buffer larger than the source string. Encourage strlcpy use; remove portability note about strlcpy(3). Adapting a strlcpy-using code base to a platform that does not provide strlcpy in libc is so trivial as to not be worth mentioning. (Just copy strlcpy.c out of any BSD libc, or include and link the pre-packaged libbsd library on non-BSD platforms.) Likewise, expand the page's warning about ease of potential misuse to cover all functions documented herein, and explicitly suggest using strlcpy most of the time. The text was mostly cribbed from a similar suggestion in gets(3). Finally, document the remaining valid use of strncpy -- the rare fixed-length record with no expectation of nul-termination. Sponsored by: Dell EMC Isilon Notes: svn path=/head/; revision=334733
* strsep.3: don't silently ignore errorsEd Maste2018-05-281-3/+4
| | | | | | | | Reported by: bde MFC with: r334275 Notes: svn path=/head/; revision=334291
* Remove unnecessary free(string) from EXAMPLES.Marcelo Araujo2018-05-281-1/+0
| | | | | | | Reported by: se Notes: svn path=/head/; revision=334276
* Update strsep(3) EXAMPLE section regards the usage of assert(3).Marcelo Araujo2018-05-281-5/+5
| | | | | | | | | | | | | As many people has pointed out, using assert(3) shall be not the best approach to verify if strdup(3) has allocated memory to string. Reviewed by: imp MFC after: 4 weeks. Sponsored by: iXsystems Inc. Differential Revision: https://reviews.freebsd.org/D15594 Notes: svn path=/head/; revision=334275