aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
Commit message (Collapse)AuthorAgeFilesLines
* _exit.2: Cross-reference atexit(3)Artem Bunichev2026-03-311-1/+2
| | | | | | | | | atexit(3) is one of the cases when _exit(2) must be used instead of exit(3). MFC after: 3 days Reviewed by: mhorne, ziaee Differential Revision: https://reviews.freebsd.org/D54467
* realpath: Improve manual pageDag-Erling Smørgrav2026-03-191-24/+12
| | | | | | | | | | | | | | * Try to make the RETURN VALUES section flow better. * Add basename(3), dirname(3), free(3) to the SEE ALSO section. * Drop the CAVEATS section, which was obsolete the moment realpath(3) was added to the Single Unix Specification in 1994. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D55928
* realpath: Improve prev_len logicDag-Erling Smørgrav2026-03-191-3/+7
| | | | | | | | | | | | | | | * Save prev_len after having checked for and appended a trailing slash, not before. This requires us to back up if we end up returning a partial result, but previously we would sometimes return a partial result with a trailing slash and sometimes without. * Replace strlcat() with a faster strlcpy() since we know exactly how far into the buffer we are. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D55914
* libc: Fix dtor order in __cxa_thread_atexitShengYi Hung2026-03-141-2/+2
| | | | | | | | | | | | | | | The thread_local variable may creates another thread_local variable inside its dtor. This new object is immediately be registered in __cxa_thread_atexit() and need to be freed before processing another variable. This fixes the libcxx test thread_local_destruction_order.pass.cpp. Reported by: kib Approved by: lwhsu (mentor) MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D55826
* system(3): Unwrap execve()Dag-Erling Smørgrav2026-03-041-1/+3
| | | | | | | | | | | There is no need to call execl(), which will allocate an array and copy our arguments into it, when we can use a static array and call execve() directly. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D55648
* system(3): Fix null caseDag-Erling Smørgrav2026-02-251-1/+1
| | | | | | | | | | | | | | | | | | | | | Our manual page states that if given a null pointer, system() returns non-zero if the shell is available and zero if it is not. This is consistent with the C standard's description of system(), but it is not what we actually do. What we actually do is always return non-zero, as required by POSIX. As the POSIX rationale explains, implementing the logic required by the C standard does not violate POSIX, since a conforming system always has a shell, therefore the logic will always return non-zero. Since our libc is commonly used in non-conforming situations such as chroots or thin jails, we should implement the full logic required by the C standard. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: obiwac, bnovkov, kevans Differential Revision: https://reviews.freebsd.org/D55484
* system(3): Clarify return valuesDag-Erling Smørgrav2026-02-251-3/+5
| | | | | | | | | | | | Our manual page currently states that system() will return 127 if it fails to execute the shell. The actual return value is, to quote POSIX, “as if the command language interpreter had terminated using exit(127) or _exit(127)”. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: bnovkov, kevans Differential Revision: https://reviews.freebsd.org/D55483
* system(3): Improve signal handlingDag-Erling Smørgrav2026-02-251-43/+73
| | | | | | | | | | | | | | | | Ignore SIGINT and SIGQUIT and block SIGCHLD, as POSIX requires. To deal with the concurrency problem described in POSIX, we keep track of the count of concurrent invocations. We ignore and block signals only when the counter was zero before we incremented it, and restore them only when the counter reaches zero after we decrement it. Note that this does not address the issue of thread cancellation. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: bnovkov, sef, kevans Differential Revision: https://reviews.freebsd.org/D55471
* libc: Roll {l,ll,imax}abs(3) manpages into just abs(3)Aymeric Wibo2026-02-219-216/+61
| | | | | | | | | | | | No need to have 4 separate manpages for these functions. Use opportunity to change parameter names in the source from j -> i to reflect the name used in POSIX. (The ISO C standard uses j but i is a better name anyway.) Reviewed by: des, rpokala Approved by: rpokala Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D55361
* libc: Roll {l,ll,imax}div(3) manpages into just div(3)Aymeric Wibo2026-02-215-242/+67
| | | | | | | | | No need to have 4 separate manpages for these functions. Reviewed by: ziaee, rpokala, des Approved by: rpokala, des Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D55360
* Summary: *.3: misc man page fixesRobert Clausecker2026-02-131-1/+1
| | | | | | Approved by: markj (mentor) MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D55249
* libc: Improve {,l,ll,imax}div(3) manpagesAymeric Wibo2026-02-126-24/+28
| | | | | | | | | | Mainly rename numerator parameter of div(3) and ldiv(3) from num to numer, and explicitly specify what "numer", "denom", and "rem" mean in the manpages. MFC after: 3 days Obtained from: https://github.com/apple-oss-distributions/libc (partially) Sponsored by: Klara, Inc.
* libc: Remove leftover commentsAymeric Wibo2026-02-123-4/+0
| | | | | | | These comments refer to a comment in div.c which doesn't exist anymore. Fixes: 7c7299df76e2 ("libc: Remove support for pre-C99 C standards") Sponsored by: Klara, Inc.
* tdestroy: don't visit one-child node twiceDoug Moore2026-01-161-34/+32
| | | | | | | | | Change tdestroy() to immediately free a node with no right child as soon as it is encountered. Currently, such nodes are visited twice before deletion. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D54699
* libc/stdlib: Port strtonumx() from IllumosHans Rosenfeld2026-01-154-23/+80
| | | | | | | | | | | Add strtonumx(), a companion to strtonum(3) that preserves its safety and error-reporting semantics while allowing the caller to specify a conversion base, similar to the strtol(3) family of functions. Reviewed by: emaste, kib, ziaee Obtained from: https://www.illumos.org/issues/15365 Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D54270
* getopt(3): be more explicit about :: extensionSimon Wollwage2026-01-091-15/+34
| | | | | | | | | | | | Make it possible to search for literal two colons (::) and actually find something. Make the "x"/"x:"/"x::" examples more explicit and more visibile. Signed-off-by: Simon Wollwage <rootnode+freebsd@wollwage.com> Obtained from: NetBSD, nbuwe <uwe@stderr.spb.ru>, 856d5b6 PR: 291374 Reviewed by: imp, jlduran Pull Request: https://github.com/freebsd/freebsd-src/pull/1923
* tdestroy(3) man pageKonstantin Belousov2025-12-292-3/+25
| | | | | | | Reviewed by: alc, emaste, ziaee Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54365
* libc: add glibc-compatible tdestroy(3)Konstantin Belousov2025-12-293-0/+70
| | | | | | | | | | | The function clears the whole tree. Relnotes: yes Reviewed by: alc, emaste Discussed with: dougm Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54365
* libc/stdlib/Makefile: one line for each source file nameKonstantin Belousov2025-12-291-13/+69
| | | | | | | Reviewed by: alc, emaste Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54365
* strfmon: Fix negative sign handling for C localeJose Luis Duran2025-11-261-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
* strfmon: EINVAL if the '+' flag and both signs are emptyJose Luis Duran2025-11-262-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
* strfmon: Fix typo s/poistion/position/Jose Luis Duran2025-11-261-1/+1
| | | | MFC after: 1 week
* jemalloc: apply freebsd changes to jemalloc 5.3.0 man pageMinsoo Choo2025-11-251-1/+31
| | | | | Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/1890
* jemalloc: import jemalloc 5.3.0 man pageMinsoo Choo2025-11-251-52/+180
| | | | | Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/1890
* exit.3: Fix a typo in the manual pageGordon Bergling2025-11-191-1/+1
| | | | | | - s/avaliable/available/ MFC after: 3 days
* lib/libc: implement C23 memalignment()Robert Clausecker2025-11-124-2/+84
| | | | | | | | | | | | | | | | | | This new function computes the alignment of a pointer. It is part of ISO/IEC 9899:2024, the new C standard. If the pointer is a null pointer, null is returned. I have tried to write an implementation that can cope with traditional address-based architectures, even if size_t and uintptr_t are of different length. Adjustments may be needed for CHERI though. A man page is provided, too. No unit test for now. Reviewed by: kib, imp, ziaee (manpages), pauamma@gundo.com Approved by: markj (mentor) MFC after: 1 month Relnotes: yes Differential Revision: https://reviews.freebsd.org/D53673
* realpath: Belatedly document POSIX conformanceDag-Erling Smørgrav2025-10-131-6/+6
| | | | | | | | | | | We've been mostly POSIX-conforming since r236400 and fully since r240410, which fixed a corner case where a missing non-leaf directory would be reported as ENOTDIR instead of ENOENT. Sponsored by: Klara, Inc. Fixes: 7877ed7ce33e ("Avoid mapping ENOENT to ENOTDIR for non-existent path components.") Reviewed by: ziaee, markj Differential Revision: https://reviews.freebsd.org/D53027
* realpath: Report correct path on failureDag-Erling Smørgrav2025-10-131-2/+12
| | | | | | | | | | | If lstat() fails with EACCES or ENOTDIR, the path we need to return in the caller-provided buffer is that of the parent directory (which is either unreadable or not a directory; the latter can only happen in the case of a race) rather than that of the child we attempted to stat. Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D53025
* lib/libc: add recallocarray()Robert Clausecker2025-10-034-2/+115
| | | | | | | | | | | | This function from OpenBSD is a hybrid of reallocarray() and calloc(). It reallocates an array, clearing any newly allocated items. reallocarray() ultimately originates from OpenBSD. The source is taken from lib/libopenbsd, which now no longer has the function unless when bootstrapping (needed for mandoc). Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D52863
* lib{c,openbsd}: use ckd_mul() for overflow checking in re(c)allocarrayRobert Clausecker2025-10-031-9/+5
| | | | | | | | | | Summary: This makes the code easier to understand and slightly faster, but requires C23. calloc() would benefit, too, but I didn't want to touch the imported jemalloc code base. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D52854
* jemalloc: don't error out on initializer warning in gccWarner Losh2025-08-171-0/+2
| | | | | | | | gcc doesn't like something about the initializer that comes with jemalloc. Since it's vendor code, make this warning not an error for -Werror purposes. Sponsored by: Netflix
* jemalloc: Merge from jemalloc 5.3.0 vendor branchWarner Losh2025-08-158-128/+648
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This brings in a merge from jemalloc 5.3.0. It's almost fully scripted, except: Three conflicts resolved by hand: include/jemalloc/internal/test_hooks.h Use the new name src/extent.c Use the new code src/jemalloc.c Use the new code since je_realloc has moved The script is recorded in FREEBSD-upgrade. The old script did svn commands that were basically a rebase of our changes. This update has a series of diff reduction changes before this. Note: I'd planned on fixing the above three conflicts with commits, but ran out of time when I did this work in January. I got discouraged when jemalloc was EOL'd and didn't pick this back up. I did the above by hand to get this into FreeBSD 15.0 This work is a repeat of the work by Minsoo Choo who did all these changes and created a pull request. Given the importance of jemalloc, I audited these changes by redoing them in this series of commits (and with the script that was checked in). I did this to confince myself and anybody else in doubt that there was no supply chain attack. The diffs between this series of commits and Minsoo's work are minor (though the version skew makes adds some noise). Interested parties can independent audit each step, I hope. I've listed Minsoo as a co-author since without his pull request to test again, this wouldn't have been possible. Thanks to brooks@ for help with getting the jemalloc 3 ABI compat symbols right. Co-authored-by: Minsoo Choo <minsoochoo0122@proton.me> Pull Request: https://github.com/freebsd/freebsd-src/pull/1337 Sponsored by: Netflix
* libc: Drop incorrect qsort optimizationDag-Erling Smørgrav2025-08-151-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
* getopt_long.3: Use "optstring" consistentlyEd Maste2025-04-291-2/+2
| | | | | | | The SYNOPSIS uses "optstring" as the argument name but the body text used a mixture of "optstr" and "optstring." Sponsored by: The FreeBSD Foundation
* alloca.3: move to share/man/man3Brooks Davis2025-04-292-86/+1
| | | | | | | The alloca() API is a compiler builtin and not generally part of libc. Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D50055
* libc: Add getenv_r() function.Dag-Erling Smørgrav2025-04-273-3/+75
| | | | | | | | | | This is a calque of the NetBSD function of the same name. MFC after: never Relontes: yes Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D49979
* issetugid() is a system call and belongs in section 2 (not 3).Jens Schweikhardt2025-04-171-2/+2
|
* libc: allow __cxa_atexit handlers to be added during __cxa_finalizeAurélien Croc de Suray2025-04-051-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) MFC after: 1 week
* libc: use __sys___realpathat directly in realpathBrooks Davis2025-02-191-9/+3
| | | | | | | | | | | | We don't need to use an interposable symbol for this purpose and it's simpler to just call the syscall in libsys. This resolves a bug where we were incorrectly using __realpathat in libc not libsys. While here, drop support for running on a FreeBSD 12 kernel and simplify includes. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D49048
* jemalloc: Move generated files into lib/libc treeWarner Losh2024-12-139-1/+1738
| | | | | | | | | | | | | | | Make it easire to update jemalloc by moving the FreeBSD specific files and the generated files into lib/libc. This allows us to regenerate them more easily, and emphasizes a bit that we may have to regenerate stuff from upstream. This is necessary to also unthin the import from the vendor branch as well (which will be needed to simplify the imports in the future since we are trying to use contrib/jemalloc for two different things). No functional change. Sponsored by: Netflix
* jemalloc: Move generated jemalloc.3 into lib/libc treeWarner Losh2024-12-132-4/+2563
| | | | | | | | | | | The more generated things that are in contrib/jemalloc tree, the more chances for interference goes way up. So, move this file into our lib/libc tree. I didn't add a 'generated file' line / info, but this is funky enough I don't think we need that. We do add things to the man page, and that should be tracked in the contrib/jemalloc tree to allow better importing experience. Sponsored by: Netflix
* libc: indicate existing functions that are POSIX 2024Ed Maste2024-11-152-1/+6
| | | | | | Reviewed by: brooks, imp Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47581
* Revise qsort(3 reflect POSIX.1-2024 update.Xin LI2024-10-251-23/+24
| | | | | | Reviewed by: emaste, trasz MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D47262
* manuals: Fix "unusual .Xr" warnings with a scriptGraham Percival2024-10-151-1/+1
| | | | | | | | | | | | | 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
* jemalloc: set LG_VADDR to 64 on amd64Konstantin Belousov2024-09-261-0/+3
| | | | | | | | | | | and allow to revert it back to 48 with WITHOUT_JEMALLOC_LG_VADDR_WIDE build option. Reviewed by: andrew, emaste Sponsored by: Advanced Micro Devices (AMD) Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D46686
* realpath(3): Minor style issues.Dag-Erling Smørgrav2024-09-181-9/+10
| | | | | | Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D46695
* jemalloc: don't expose 3.0 compat symbolsBrooks Davis2024-08-221-10/+0
| | | | | | | | | | | | Don't provide default linkage for jemalloc 3.0 compatability symbols. We stopped declaring these interfaces with the introduction of jemalloc 4.0 prior to FreeBSD 11.0. Any code using them would have had to declare them manually so stop declaring them and export the symbols directly for compatability. Arguably they should be x86 only as they were never declared on other Tier-1 architectures. Reviewed by: imp, kib Differential Revision: https://reviews.freebsd.org/D46407
* exit(3): clarify how to obtain full exit status of the exited processKonstantin Belousov2024-07-291-0/+14
| | | | | Sponsored by: The FreeBSD Foundation MFC after: 3 days
* exit(3): make it thread-safeKonstantin Belousov2024-07-292-1/+39
| | | | | | | | | | | It was explained by Rich Felker <dalias@libc.org> on libc-coord. See https://austingroupbugs.net/view.php?id=1845. Reviewed by: imp, markj Tested by: antoine (exp-run) Sponsored by: The FreeBSD Foundation MFC after: 1 month Differential revision: https://reviews.freebsd.org/D46108
* 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