aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
Commit message (Collapse)AuthorAgeFilesLines
* strfmon: Fix negative sign handling for C localeJose Luis Duran2025-12-031-1/+1
| | | | | | | | | | | | | | | | | | | | | If the locale's positive_sign and negative_sign values would both be returned by localeconv() as empty strings, strfmon() shall behave as if the negative_sign value was the string "-". This occurs with the C locale. The implementation previously assigned "0" to sign_posn (parentheses around the entire string); now it assigns it to "1" (sign before the string) when it is undefined (CHAR_MAX). Austin Group Defect 1199[1] is applied, changing the requirements for the '+' and '(' flags. [1]: https://www.austingroupbugs.net/view.php?id=1199 Reviewed by: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D53913 (cherry picked from commit cf85e7034ad5640b18a3b68d6b291b7bf89bfc80)
* strfmon: EINVAL if the '+' flag and both signs are emptyJose Luis Duran2025-12-032-2/+14
| | | | | | | | | | | | | | | | | | According to the Open Group Base Specifications Issue 8[1], strfmon(3) should return EINVAL when the '+' flag was included in a conversion specification and the locale's positive_sign and negative_sign values would both be returned by localeconv(3) as empty strings. Austin Group Defect 1199[2] is applied, adding the [EINVAL] error. [1]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/strfmon.html [2]: https://www.austingroupbugs.net/view.php?id=1199 Reviewed by: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D53912 (cherry picked from commit 1fd018972a18b682521bb8f004dfd162327e5db2)
* strfmon: Fix typo s/poistion/position/Jose Luis Duran2025-12-031-1/+1
| | | | | | MFC after: 1 week (cherry picked from commit 91e7f19ec4056587a85c1461a4f34a6d5d4b7b52)
* libc: Drop incorrect qsort optimizationDag-Erling Smørgrav2025-08-271-13/+0
| | | | | | | | | | | | | | | | | | As pointed out in the PR and the article linked below, the switch to insertion sort in the BSD qsort code is based on a misunderstanding of Knuth's TAOCP and is actually a pessimization. As demonstrated by the added test, it is trivially easy to construct pathological input which results in quadratic runtime. Without that misguided optimization, the same input runs in nearly linearithmic time. https://www.raygard.net/2022/02/26/Re-engineering-a-qsort-part-3 PR: 287089 MFC after: 1 week Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D51907 (cherry picked from commit 5205b32de3fb7702e96b3991f5b1a61eee406d8b)
* libc: allow __cxa_atexit handlers to be added during __cxa_finalizeAurélien Croc de Suray2025-04-171-25/+36
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | science/dlib-cpp reveals an interesting scenario that works fine on other platforms but not on FreeBSD; notably, it ends up creating a new global object from some destructor which is called during __cxa_finalize. This breaks when libdlib is dlopen()ed and then subsequently dlclose()ed, as we never end up invoking the created object's dtor until program exit when the shlib is already unmapped. Fix it by noting when we're in the middle of __cxa_finalize for a dso, and then restarting the search if __cxa_atexit() was called in the middle somewhere. We wait until we've processed the initial set before starting over and processing the newly added handlers as if it were a complete set of handlers added during runtime. The alternative is calling them as they're added to maintain a LIFO in terms of total ordering, but in theory a constructor could add another global object that also needs to be destroyed, and that object needs to be destroyed after the one that constructed it to avoid creating unexpected lifetime issues. This manifests in the pdlib PHP extension for dlib crashing, see [0]. [0] https://github.com/goodspb/pdlib/issues/39 PR: 285870 Reviewed by: kevans (also supplied commit message) (cherry picked from commit 23427c8e1fedb9fc68ad0bd27a59c7ffd2b3008c)
* __cxa_thread_call_dtors(3): fix dtor pointer validity checkKonstantin Belousov2024-05-101-1/+1
| | | | | | PR: 278701 (cherry picked from commit b27eb9ce96b838622e125fd969e8dc4914aabe18)
* strfmon.c: Use the restrict keyword directlyKonstantin Belousov2023-12-161-3/+3
| | | | (cherry picked from commit 86e2bcbf47fb4c8dbd799f2f21c0ed338b2e8f1b)
* strfmon: style fixesJose Luis Duran2023-12-161-98/+99
| | | | (cherry picked from commit 56a0d5444d6f39302f3476b61c1b81ed39abe589)
* strfmon: Silence scan-build warningJose Luis Duran2023-12-161-1/+0
| | | | (cherry picked from commit 6abee52e0d79f68fd725de748d7027ca8eef2294)
* strfmon.3: Cleanup example codeJose Luis Duran2023-12-161-3/+5
| | | | (cherry picked from commit 2a163c3649e59dd616e057994ec02092362f0ae7)
* ptsname.3: accommodate upcoming POSIX Issue 8 ptsname_rEd Maste2023-10-161-2/+15
| | | | | | | | | | | | | | | | POSIX has accepted a proposal[1] to add glibc-compatible ptsname_r. It indicates an error by returning the error number, rather than returning -1 and setting errno. Update RETURN VALUES in ptsname_r's man page now to encourage folks to test that the return value != 0 rather than == -1. [1] https://www.austingroupbugs.net/bug_view_page.php?bug_id=508 Reported by: Collin Funk Reviewed by: kib Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D42204 (cherry picked from commit a5ed6a815e38d6c622cd97a6020592ded579cf7a)
* libc: Rewrite quick_exit() and at_quick_exit() using C11 atomics.Dag-Erling Smørgrav2023-10-051-24/+16
| | | | | | | | | | | | | | | | | | | | | | | Compiler memory barriers do not prevent the CPU from executing the code out of order. Switch to C11 atomics. This also lets us get rid of the mutex; instead, loop until the compare_exchange succeeds. While here, change the return value of at_quick_exit() on failure to the more traditional -1, matching atexit(). Sponsored by: Klara, Inc. Reviewed by: Olivier Certner, kevans, kib Differential Revision: https://reviews.freebsd.org/D41936 (cherry picked from commit 1dc3abb052430279e47c8922d22b30922adcf0f6) libc: Add a rudimentary test for quick_exit(3). Sponsored by: Klara, Inc. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D41937 (cherry picked from commit c7dd4601aeebbc1bbe131cbe6747476c124b47fe)
* Remove "All Rights Reserved" from Foundation copyrightsEd Maste2023-09-2513-13/+11
| | | | | | | | Sponsored by: The FreeBSD Foundation (cherry picked from commit 7fde0187cc443468561f0a30d589ff0cfe45eef5) (cherry picked from commit 560e22c8fe460e00d16e5268fe1fbb316ad81101) (cherry picked from commit 5b5fa75acff11d871d0c90045f8c1a58fed85365)
* Remove $FreeBSD$: one-line nroff patternWarner Losh2023-08-2329-29/+0
| | | | | | | Remove /^\.\\"\s*\$FreeBSD\$$\n/ Similar commit in main: (cherry picked from commit b2c76c41be32)
* Remove $FreeBSD$: two-line nroff patternWarner Losh2023-08-2314-28/+0
| | | | | | | Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/ Similar commit in main: (cherry picked from commit fa9896e082a1)
* Remove $FreeBSD$: one-line sh patternWarner Losh2023-08-232-2/+0
| | | | | | | Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/ Similar commit in main: (cherry picked from commit d0b2dbfa0ecf)
* Remove $FreeBSD$: one-line .c patternWarner Losh2023-08-2358-116/+0
| | | | | | | Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/ Similar commit in main: (cherry picked from commit 1d386b48a555)
* Remove $FreeBSD$: one-line .h patternWarner Losh2023-08-233-3/+0
| | | | | | | Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/ Similar commit in main: (cherry picked from commit 42b388439bd3)
* Remove $FreeBSD$: two-line .h patternWarner Losh2023-08-2310-20/+0
| | | | | | | Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/ Similar commit in main: (cherry picked from commit b3e7694832e8)
* spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSDWarner Losh2023-07-2511-11/+11
| | | | | | | | | | | 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 (cherry picked from commit 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
* libc: move declaration of 'char **environ' to common private headerKonstantin Belousov2023-06-121-2/+1
| | | | (cherry picked from commit 0c6f0c0db75ea5a1e89a68a163fc555bdd7d00f5)
* libc: Add missing object size check to qsort_s(3)Hans Petter Selasky2023-04-302-3/+11
| | | | | | | | | | | | | | When sorting, both the C11 standard (ISO/IEC 9899:2011, K.3.6.3.2) and the ISO/IEC JTC1 SC22 WG14 N1172 standard, does not define objects of zero size as undefined behaviour. However Microsoft's cpp-docs does. Add proper checks for this. Found while working on bsort(3). Reviewed by: kib@ and emaste@ Sponsored by: NVIDIA Networking Differential Revision: https://reviews.freebsd.org/D39687 (cherry picked from commit 27bb0d337c0d82a1a4f310315840236eb239963c)
* libc: Sorting is not needed when there are less than two elementsHans Petter Selasky2023-04-301-1/+2
| | | | | | | | | | | If there are less than two elements avoid executing the first sorting loop. No functional change intended. Reviewed by: kib@ Sponsored by: NVIDIA Networking Differential Revision: https://reviews.freebsd.org/D39691 (cherry picked from commit ecb2ce3a51e9b09a57cd42262fc798ae089c0758)
* strfmon(3): Match the return typeJose Luis Duran2023-01-311-2/+2
| | | | (cherry picked from commit f5924ad8fde4d5e9c233b821cb6097c6a46740b5)
* strfmon(3): Wording improvementsJose Luis Duran2023-01-311-12/+11
| | | | (cherry picked from commit 59cc636d94a6c9b28147304fa59351224f801e92)
* strfmon(3): Add an EXAMPLES sectionJose Luis Duran2023-01-311-0/+25
| | | | (cherry picked from commit cdd9d92dade61a6b5c37b758e9533a076bb5a2de)
* strfmon: Remove XXX marksJose Luis Duran2022-11-051-3/+3
| | | | (cherry picked from commit f0a15aafcb863f796e2a7f94d0fd8a857fb56103)
* strfmon_l: Use specified locale for number formattingJose Luis Duran2022-11-051-24/+23
| | | | (cherry picked from commit 621bf91893ad96c2ec46e603bf4c5b8762e3f730)
* strfmon_l(3): Add name to the man pageJose Luis Duran2022-11-051-3/+5
| | | | (cherry picked from commit d96088b3ab5f5b833a78ff363f540cc5fa2c1e78)
* strfmon(3): Fix # explanationJose Luis Duran2022-11-011-2/+8
| | | | (cherry picked from commit 7cfd67ce96d97c263c2c56c5c437426463467689)
* strfmon(3): Remove repeated wordsJose Luis Duran2022-11-011-1/+1
| | | | (cherry picked from commit 0efec50e9e08f76e9e9d6a024763ca0fe83ae1f2)
* strfmon: Fix formatting of a second fixed-width valueJose Luis Duran2022-11-011-1/+1
| | | | (cherry picked from commit 34f88528edba44b2703ba8c772bef077eca33dab)
* strfmon: Fix an edge case when sep_by_space is 2Jose Luis Duran2022-11-011-2/+6
| | | | (cherry picked from commit 750fe3e6a4619e040c7b0951775698b61290102e)
* strfmon: Fix alignment when enclosed by parenthesesJose Luis Duran2022-11-011-2/+10
| | | | (cherry picked from commit 947efadc3d6e778a824618d82f53f061bec69b77)
* strfmon: Trim the SPACE from international currency symbolJose Luis Duran2022-11-011-1/+3
| | | | (cherry picked from commit 6da51e19e347c13e133bcba68cc6100c16320a01)
* strfmon: Avoid an out-of-bounds accessJose Luis Duran2022-11-011-2/+3
| | | | (cherry picked from commit 9e03b903e377c75a60cbbb89ed78955769a1c804)
* strfmon: Fix typos in source code commentsJose Luis Duran2022-11-011-2/+2
| | | | (cherry picked from commit 0afd11d50f277c24e80dd0228b122bcc53d559c0)
* strfmon: Fix typo in constantJose Luis Duran2022-11-011-4/+4
| | | | (cherry picked from commit d5980dff6b512fa9ba08d6af3ff365805fed44fd)
* strfmon: Code cleanupJose Luis Duran2022-11-011-64/+68
| | | | (cherry picked from commit f81dfea2912dbc0560587ab534a3d8549dbbb95b)
* libc: ANSIfy div / ldiv function definitionsEd Maste2022-08-042-4/+2
| | | | | | | MFC after: 1 week Sponsored by: The FreeBSD Foundation (cherry picked from commit a8a43edc0f856167c483b554a89a868f53a6ce25)
* libc: Add HISTORY sections to the manual pagesGordon Bergling2022-06-042-2/+13
| | | | | | | | | | | 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 Differential Revision: https://reviews.freebsd.org/D34759 (cherry picked from commit 4b7f35db44cbf901e994fc9a4bcd4c98ebe8c4a1)
* getenv(3): Fix two typos in source code commentsGordon Bergling2022-04-141-2/+2
| | | | | | - s/peform/perform/ (cherry picked from commit 8dcf5860b369559ba176f5dc0d7ed278f8aecf38)
* qsort.c: prevent undefined behaviorStefan Eßer2022-03-041-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Mark Milliard has detected a case of undefined behavior with the LLVM UBSAN. The mandoc program called qsort with a==NULL and n==0, which is allowed by the POSIX standard. The qsort() in FreeBSD did not attempt to perform any accesses using the passed pointer for n==0, but it did add an offset to the pointer value, which is undefined behavior in case of a NULL pointer. This operation has no adverse effects on any achitecture supported by FreeBSD, but could be caught in more strict environments. After some discussion in the freebsd-current mail list, it was concluded that the case of a==NULL and n!=0 should still be caught by UBSAN (or cause a program abort due to an illegal access) in order to not hide errors in programs incorrectly invoking qsort(). Only the the case of a==NULL and n==0 should be fixed to not perform the undefined operation on a NULL pointer. This commit makes qsort() exit before reaching the point of potentially undefined behvior for the case n==0, but does not test the value of a, since the result will not depend on whether this pointer is NULL or an actual pointer to an array if n==0. The issue found by Mark Milliard in the whatis command has been reported to the upstream (OpenBSD) and has already been patched there. (cherry picked from commit d106f982a54cd299671ccad58bc456138a22ae7b)
* Fix null pointer subtraction in mergesort()Dimitry Andric2021-08-311-5/+1
| | | | | | | | | | | | | | | | | Clang 13 produces the following warning for this function: lib/libc/stdlib/merge.c:137:41: error: performing pointer subtraction with a null pointer has undefined behavior [-Werror,-Wnull-pointer-subtraction] if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) ^ ~~~~~~~~~ This is meant to check whether the size and base parameters are aligned to the size of an int, so use our __is_aligned() macro instead. Also remove the comment that indicated this "stupid subtraction" was done to pacify some ancient and unknown Cray compiler, and which has been there since the BSD 4.4 Lite Lib Sources were imported. (cherry picked from commit 4e5d32a445f90d37966cd6de571978551654e3f3)
* Fix race between first rand(3) calls with _once().Alexander Motin2021-08-191-4/+5
| | | | | | | | | | | | | Before this patch there was a chance for thread that called rand(3) slightly later to see rand3_state already allocated, but not yet initialized. While this API is not expected to be thread-safe, it is not expected to crash. ztest on 64-thread system reproduced it reliably for me. Submitted by: avg@ MFC after: 1 month (cherry picked from commit 3a57f08b5042f15bb8ffb2fcce99d082d225d4cf)
* _Exit(3): document implementationKonstantin Belousov2021-08-121-6/+14
| | | | (cherry picked from commit ee62fb2e1e14eab35d4e4e92535bcac9fc91eeb8)
* libc/qsort: Don't allow interposing recursive callsAlex Richardson2021-03-171-50/+48
| | | | | | | | | | | | | | | | | | | This causes problems when using ASAN with a runtime older than 12.0 since the intercept does not expect qsort() to call itself using an interposable function call. This results in infinite recursion and stack exhaustion when a binary compiled with -fsanitize=address calls qsort. See also https://bugs.llvm.org/show_bug.cgi?id=46832 and https://reviews.llvm.org/D84509 (ASAN runtime patch). To prevent this problem, this patch uses a static helper function for the actual qsort() implementation. This prevents interposition and allows for direct calls. As a nice side-effect, we can also move the qsort_s checks to the top-level function and out of the recursive calls. Reviewed By: kib Differential Revision: https://reviews.freebsd.org/D28133 (cherry picked from commit cbcfe28f9d5f975f97b7fb4a0d72bc9780eb0c46)
* libc: Fix null pointer arithmetic warning in mergesortAlex Richardson2021-01-201-4/+2
| | | | | | | | | This file has other questionable code and "optimizations" (such as copying one int at a time) that are probably no longer useful, so it might make sense to replace it with a different implementation at some point. Reviewed By: jhb Differential Revision: https://reviews.freebsd.org/D28134
* getopt: Fix conversion from string-literal to non-const char *Alex Richardson2021-01-192-4/+3
| | | | | | Define a non-const static char EMSG[] = "" to avoid having to add __DECONST() to all uses of EMSG. Also make current_dash a const char * to fix this warning.
* libc: Fix most issues reported by mandocGordon Bergling2020-12-191-1/+1
| | | | | | | | | | | | | | - varios "new sentence, new line" warnings - varios "sections out of conventional order" warnings - varios "unusual Xr order" warnings - varios "missing section argument" warnings - varios "no blank before trailing delimiter" warnings - varios "normalizing date format" warnings MFC after: 1 month Notes: svn path=/head/; revision=368817