summaryrefslogtreecommitdiff
path: root/lib/libc/string
Commit message (Collapse)AuthorAgeFilesLines
* 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-125/+194
| | | | | | | | | | | 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
* Remove "All rights reserved" from my files.Xin LI2018-05-101-1/+0
| | | | | | | | | See r333391 for the rationale. MFC after: 1 week Notes: svn path=/head/; revision=333449
* Remove "all rights reserved" on files where I have copyright.Niclas Zeising2018-05-091-1/+0
| | | | | | | | | | According to r333391 it is not needed any more. Reviewed by: imp, emaste Differential Revision: https://reviews.freebsd.org/D15370 Notes: svn path=/head/; revision=333420
* Don't put multiple names on a single .Nm line. This fixes apropos(1)Edward Tomasz Napierala2018-04-172-2/+6
| | | | | | | | | | | | | | | | output, from this: strnlen, strlen, strlen,(3) - find length of string │······· ... to this: strlen, strnlen(3) - find length of string PR: 223525 MFC after: 2 weeks Notes: svn path=/head/; revision=332642
* Document memset_s(3). memset_s(3) is defined inCy Schubert2018-02-162-3/+63
| | | | | | | | | | | | | | | | | | | C11 standard (ISO/IEC 9899:2011) K.3.7.4.1 The memset_s function (p: 621-622) Fix memset(3) portion of the man page by replacing the first argument (destination) "b" with "dest", which is more descriptive than "b". This also makes it consistent with the term used in the memset_s() portion of the man page. See also http://en.cppreference.com/w/c/string/byte/memset. Reviewed by: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D13682 Notes: svn path=/head/; revision=329361
* libc: further adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-2532-1/+65
| | | | | | | | | | | | | | | Mainly focus on files that use BSD 2-Clause license, however the tool I was using mis-identified many licenses so this was mostly a manual - error prone - task. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Notes: svn path=/head/; revision=326193
* General further adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-2040-14/+94
| | | | | | | | | | | | | | | | | Mainly focus on files that use BSD 3-Clause license. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Special thanks to Wind River for providing access to "The Duke of Highlander" tool: an older (2014) run over FreeBSD tree was useful as a starting point. Notes: svn path=/head/; revision=326025
* Add notes about overlapping copies.Warner Losh2017-11-133-0/+25
| | | | | | | | | | | | | | Add notes to each of these that specifically state that results are undefined if the strings overlap. In the case of memcpy, we document the overlapping behavior on FreeBSD (pre-existing). For str*, it is left unspecified, however, since the default (and x86) implementations do not handle overlapping strings properly. PR: 223653 Sponsored by: Netflix Notes: svn path=/head/; revision=325765
* Improve standard compliance for memset_s() and abort_handler_s().Konstantin Belousov2017-08-121-4/+7
| | | | | | | | | | | | | | | | | | | | abort_handler_s() currently simply calls abort(), though the standard specifies more: "Writes an implementation-defined message to stderr which must include the string pointed to by msg and calls abort()." memset_s() is missing error condition "n > smax", and does not invoke the constraint handler after filling the buffer: "following errors are detected at runtime and call the currently installed constraint handler function after storing ch in every location of the destination range [dest, dest+destsz) if dest and destsz are themselves valid", one of the errors is "n > smax" itself. Submitted by: Yuri Pankov <yuripv@gmx.com> MFC after: 1 week Differential revision: https://reviews.freebsd.org/D11991 Notes: svn path=/head/; revision=322427
* Fix indent.Konstantin Belousov2017-08-121-1/+1
| | | | | | | | Submitted by: Yuri Pankov <yuripv@gmx.com> MFC after: 3 days Notes: svn path=/head/; revision=322426
* Fix strcoll_l disagreeing with strxfrm by reworking the forward order case inBaptiste Daroussin2017-04-161-58/+62
| | | | | | | | | | | | | | | | | | | | wcscoll_l(). Illumos fixed this while grabbing back our patches: https://www.illumos.org/rb/r/402/ This does not 100% fix what postgresql folks reported as there is still a remaining issue: https://www.illumos.org/issues/7962, it improves the situation The initial issue was reported in postgresql mailing lists: https://www.postgresql.org/message-id/flat/111D0E27-A8F3-4A84-A4E0-B0FB703863DF@s24.com#111D0E27-A8F3-4A84-A4E0-B0FB703863DF@s24.com Submitted by: Yuri Pankov <yuri.pankov@nexenta.com> Obtained from: Illumos MFC after: 2 weeks Notes: svn path=/head/; revision=317034
* Implement the memset_s(3) function as specified by the C11 ISO/IECKonstantin Belousov2017-03-303-1/+65
| | | | | | | | | | | | | | | | | 9899:2011 Appendix K 3.7.4.1. Other needed supporting types, defines and constraint_handler infrastructure is added as specified in the C11 spec. Submitted by: Tom Rix <trix@juniper.net> Sponsored by: Juniper Networks Discussed with: ed MFC after: 3 weeks Differential revision: https://reviews.freebsd.org/D9903 Differential revision: https://reviews.freebsd.org/D10161 Notes: svn path=/head/; revision=316213
* libc: add reference to two-way algorithm and bad shift table in memmem/strstrEd Maste2017-03-182-0/+16
| | | | | | | Requested by: ed Notes: svn path=/head/; revision=315468
* libc: Use musl's O(n) memmem and strstrEd Maste2017-03-183-97/+327
| | | | | | | | | | | | | | | | | | | | | | It is O(n) in the length of the haystack (big) string, and has special cases for short needle (little) strings, of one to four bytes, to avoid excessive overhead. There are a small set of nearly trivial cases where the startup overhead of the musl implementation makes it slightly slower -- for example, a 31 byte needle that matches the beginning of the haystack. It's faster for non-trivial cases, and significantly so for inputs that trigger worst- case behaviour of the previous implementation. As an example, in my tests a 16K needle that matches the end of a 64K haystack is nearly 2000x faster with this implementation. Reviewed by: bapt (earlier), ed (earlier) Obtained from: musl (snapshot at commit c718f9fc) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D2601 Notes: svn path=/head/; revision=315467
* Replace dot-dot relative pathing with SRCTOP-relative paths where possibleEnji Cooper2017-01-201-1/+1
| | | | | | | | | | | | | | | | | | | | This reduces build output, need for recalculating paths, and makes it clearer which paths are relative to what areas in the source tree. The change in performance over a locally mounted UFS filesystem was negligible in my testing, but this may more positively impact other filesystems like NFS. LIBC_SRCTOP was left alone so Juniper (and other users) can continue to manipulate lib/libc/Makefile (and other Makefile.inc's under lib/libc) as include Makefiles with custom options. Discussed with: marcel, sjg MFC after: 1 week Reviewed by: emaste Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D9207 Notes: svn path=/head/; revision=312451
* Add timingsafe_bcmp and timingsafe_memcmp.Xin LI2016-08-147-5/+199
| | | | | | | | | | Obtained from: OpenBSD Reviewed by: trasz MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D7280 Notes: svn path=/head/; revision=304106
* libc: spelling fixes.Pedro F. Giffuni2016-04-301-1/+1
| | | | | | | Mostly on comments. Notes: svn path=/head/; revision=298830
* Make it clear that the "size" argument is for the dst string.Bryan Drewery2016-02-261-3/+3
| | | | | | | | | | | The rest of the manpage already referenced 'dstsize' but did not rename the variable in the prototype in r257646. MFC after: 3 days Sponsored by: EMC / Isilon Storage Division Notes: svn path=/head/; revision=296128
* Add a standards compliance note for strtok_r as suggested by cpercival.Warren Block2016-01-221-1/+6
| | | | | | | | Reviewed by: cpercival MFC after: 1 week Notes: svn path=/head/; revision=294597
* Avoid reading pass the end of the source buffer when it is not NULBrooks Davis2016-01-131-1/+1
| | | | | | | | | | | | | | terminated. If this buffer is adjacent to an unmapped page or a version of C with bounds checked is used this may result in a crash. PR: 206178 Submitted by: Alexander Cherepanov <cherepan@mccme.ru> MFC after: 1 week Notes: svn path=/head/; revision=293856
* Avoid reading pass the end of the source buffer when it is not NULBrooks Davis2016-01-131-1/+1
| | | | | | | | | | | | | | terminated. If this buffer is adjacent to an unmapped page or a version of C with bounds checked is used this may result in a crash. PR: 206177 Submitted by: Alexander Cherepanov <cherepan@mccme.ru> MFC after: 1 week Notes: svn path=/head/; revision=293855
* collate: Fix expansion substitions (broken upstream too)Baptiste Daroussin2015-10-231-40/+59
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Through testing, the user noted that some Cyrillic characters were not sorting correctly, and this was confirmed. After extensive testing and review, the localedef tool was eliminated as the culprit. The sustitutions were encoded correctly in LC_COLLATE. The error was mainly in wcscoll where character expansions were mishandled. The main directive pass routines had to be written to go back for a new collation value when the "state" variable was set. Before pointers were being advanced, the second lookup was gettting applied to the wrong character, etc. The "eat expansion codes" section on collate.c also had a bug. Later own, the "state" variable logic was changed to only set if next code was greater than zero (rather than >= 0). Some additional cleanups got captured from previous work: 1) The previous commit moved the binary search comment from the correct location to a wrong location because it's wrong upstream in Illumos. The comment has little value so I just removed it. 2) Don't check if pointers are null before freeing, this is redundant as free() handles null pointers. 3) The two binary search trees were standardized wrt initialization 4) On the binary search trees, a negative "high" exits rather than checking the table count again. Submitted by: marino Obtained from: DragonflyBSD Notes: svn path=/projects/collation/; revision=289860
* Merge from headBaptiste Daroussin2015-10-191-2/+3
|\ | | | | | | Notes: svn path=/projects/collation/; revision=289573
| * Document bitset(9)Conrad Meyer2015-10-171-2/+3
| | | | | | | | Notes: svn path=/head/; revision=289467