aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
Commit message (Collapse)AuthorAgeFilesLines
* libc: Implement N2630.Dag-Erling Smørgrav2023-09-078-4/+51
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This adds formatted input/output of binary integer numbers to the printf(), scanf(), and strtol() families, including their wide-character counterparts. Reviewed by: imp, emaste Differential Revision: https://reviews.freebsd.org/D41511 (cherry picked from commit d9dc1603d6e48cca84cad3ebe859129131b8387c) libc: Add unit tests for N2630 and possible collateral damage. Reviewed by: imp, emaste Differential Revision: https://reviews.freebsd.org/D41512 (cherry picked from commit b9385720f34b536ef2568a642e8b1fad0450056f) libc: Document support for binary integers. Reviewed by: debdrup, emaste Differential Revision: https://reviews.freebsd.org/D41522 (cherry picked from commit 76edfabbecdec686a570b8e009d5ea4112f943e0) libc: Fix fixed-width case in the new integer parser. Fixes: d9dc1603d6e4 Differential Revision: https://reviews.freebsd.org/D41622 (cherry picked from commit aca3bd1602577591e5cd237c4bb0bb71b3be0c75) libc: Add a wide version of snprintf_test. Reviewed by: imp, emaste Differential Revision: https://reviews.freebsd.org/D41726 (cherry picked from commit 4ec9ee9912765ac4ca57353999caa92a23283d8e) libc: Suppress format checks on printf() / scanf() tests. Reviewed by: jrtc27, markj, emaste Differential Revision: https://reviews.freebsd.org/D41727 (cherry picked from commit 294bd2827e61a78041f6613f4b82235fcc454157) Approved by: re (gjb)
* libc: regoranize malloc buildBrooks Davis2023-08-164-3/+4
| | | | | | | | | | | | | Create a stdlib/malloc to hold the definition of the malloc interface (e.g., the Symbol.map file) and make jemalloc a subdirectory. This will make it easier to integrate alternative allocators such as snmalloc while making it clear that the current jemalloc symbols are the FreeBSD API/ABI (for better or worse). Suggested by: jrtc27 Reviewed by: jrtc27, emaste Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D41457
* jemalloc: drop unused .PATHBrooks Davis2023-08-161-3/+0
| | | | | | | | | All jemalloc sources are in contrib so don't look for them in the jemalloc subdirectory. Reviewed by: jrtc27, emaste Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D41481
* libc: include malloc via stdlib/Makefile.incBrooks Davis2023-08-161-0/+2
| | | | | | | | | | There's a hierarchy here and we should use it. Improves: cbeacb7c46f3a3650e5dbefa9a1a18bc9943a8cc Reviewed by: jrtc27, jhb, emaste Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D41456
* Remove $FreeBSD$: one-line nroff patternWarner Losh2023-08-1629-29/+0
| | | | Remove /^\.\\"\s*\$FreeBSD\$$\n/
* Remove $FreeBSD$: two-line nroff patternWarner Losh2023-08-1614-28/+0
| | | | Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/
* Remove $FreeBSD$: one-line sh patternWarner Losh2023-08-162-2/+0
| | | | Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
* Remove $FreeBSD$: one-line .c patternWarner Losh2023-08-1658-116/+0
| | | | Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
* Remove $FreeBSD$: one-line .h patternWarner Losh2023-08-163-3/+0
| | | | Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
* Remove $FreeBSD$: two-line .h patternWarner Losh2023-08-1611-22/+0
| | | | Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
* libc: unsplit log message to to make it greppable in srcEd Maste2023-08-051-2/+3
| | | | | | | | | Also use __func__ rather than hardcoding the function name. This code now fits in 80 columns without splitting the log message. Reviewed by: zlei Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D39619
* spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSDWarner Losh2023-05-1211-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
* Revert "libc: Implement bsort(3) a bitonic type of sorting algorithm."Hans Petter Selasky2023-04-206-505/+1
| | | | | | | | | | | | | | | | | | | | | | | | Some points for the future: - libc is not the right place for sorting algorithms. Probably libutil is better suited for this purpose or a dedicated libsort. Should move all sorting algorithms away from libc eventually. - CheriBSD uses capabilities for memory access, and could benefit from a standard memswap() function. - Do something about qsort() in FreeBSD's libc like: - Mark it deprecated on FreeBSD, as a first step, due to missing limits on CPU time. - Audit the use of qsort() in the FreeBSD base system and consider swapping to other existing sorting algorithms. Discussed with: brooks@ Differential Revision: https://reviews.freebsd.org/D36493 This reverts commit a7469c9c0a504a5e6e9b89e148cd78df5e67ff7f. This reverts commit 7d65a450cdcc7cc743f2ecd114ba3428a21c0033. This reverts commit 8dcf3a82c54cb216df3213a013047907636a01da.
* libc: Sorting is not needed when there are less than two elementsHans Petter Selasky2023-04-191-1/+2
| | | | | | | | | | If there are less than two elements avoid executing the first sorting loop. No functional change intended. Reviewed by: kib@ MFC after: 1 week Sponsored by: NVIDIA Networking Differential Revision: https://reviews.freebsd.org/D39691
* libc: Add missing object size check to qsort_s(3)Hans Petter Selasky2023-04-192-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@ MFC after: 1 week Sponsored by: NVIDIA Networking Differential Revision: https://reviews.freebsd.org/D39687
* bsort.3: Fix warnings as reported by mandoc -W warningHans Petter Selasky2023-04-191-6/+4
| | | | | | Reported by: Yuri <yuri@aetern.org> MFC after: 1 week Sponsored by: NVIDIA Networking
* libc: Implement bsort(3) a bitonic type of sorting algorithm.Hans Petter Selasky2023-04-196-1/+507
| | | | | | | | | | | | | | | | | | | | | | | | | The bsort(3) algorithm works by swapping objects, similarly to qsort(3), and does not require any significant amount of additional memory. The bsort(3) algorithm doesn't suffer from the processing time issues known the plague the qsort(3) family of algorithms, and is bounded by a complexity of O(log2(N) * log2(N) * N), where N is the number of elements in the sorting array. The additional complexity compared to mergesort(3) is a fair tradeoff in situations where no memory may be allocated. The bsort(3) APIs are identical to those of qsort(3), allowing for easy drop-in and testing. The design of the bsort(3) algorithm allows for future parallell CPU execution when sorting arrays. The current version of the bsort(3) algorithm is single threaded. This is possible because fixed areas of the sorting data is compared at a time, and can easily be divided among different CPU's to sort large arrays faster. Reviewed by: gbe@, delphij@, pauamma_gundo.com (manpages) Sponsored by: NVIDIA Networking Differential Revision: https://reviews.freebsd.org/D36493
* secure_getenv: Improve documentation wordingWarner Losh2023-03-251-14/+16
| | | | | | | | Improve the documentation wording to be more consistent with FreeBSD manual pages. Suggested by: mjg (though reworded) Sponsored by: Netflix
* secure_getenv: Add () around return valuesWarner Losh2023-03-251-2/+2
| | | | | | Style only change, no functional change intended. Sponsored by: Netflix
* Add GNU glibc compatible secure_getenvlucy2023-03-144-3/+40
| | | | | | | | | | | | Add mostly glibc and msl compatible secure_getenv. Return NULL if issetugid() indicates the process is tainted, otherwise getenv(x). The rational behind this is the fact that many Linux applications use this function instead of getenv() as it's widely consider a, "best practice". Reviewed by: imp, mjg (feedback) Pull Request: https://github.com/freebsd/freebsd-src/pull/686 Signed-off-by: Lucy Marsh <seafork@disroot.org>
* libc: move declaration of 'char **environ' to common private headerKonstantin Belousov2023-03-111-2/+1
| | | | | | | | | Suggested by: imp Reviewed by: markj Tested by: markj (aarch64) Sponsored by: The FreeBSD Foundation MFC after: 3 weeks Differential revision: https://reviews.freebsd.org/D37220
* xlocale: garbage collect references to strtoq_l/strtouq_lVal Packett2023-03-021-2/+0
| | | | | | | | | | | | | These were explicitly never implemented (see lib/libc/locale/DESIGN.xlocale), but were referenced in the manpage and the symbol map. Fixes: 3c87aa1d3dc ("Implement xlocale APIs from Darwin") Reported by: ld.lld 16 being --no-undefined-version by default Reviewed by: theraven, emaste Sponsored by: https://www.patreon.com/valpackett Pull Request: https://github.com/freebsd/freebsd-src/pull/679 Differential Revision: https://reviews.freebsd.org/D38408
* getopt_long(3): note an inconsistency with getopt(3) in BUGSKyle Evans2023-02-111-1/+17
| | | | | | | | | | | getopt_long(3) will not allow an `optind` setting of 0 to be bug-for-bug compatible with the GNU implementation, as some software does rely on it. Document it as a BUG, since it affects previous declarations of compatibility with getopt(3). Reviewed by: pauamma (markup) Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D37867
* strfmon(3): Match the return typeJose Luis Duran2023-01-251-2/+2
| | | | | Reviewed by: kib MFC after: 1 week
* strfmon(3): Wording improvementsJose Luis Duran2023-01-251-12/+11
| | | | | | | Use the same terminology as the other `_l` xlocale(3) functions. Reviewed by: kib MFC after: 1 week
* strfmon(3): Add an EXAMPLES sectionJose Luis Duran2023-01-251-0/+25
| | | | | Reviewed by: kib MFC after: 1 week
* strfmon: Remove XXX marksJose Luis Duran2022-10-291-3/+3
| | | | | | | | | | | | phantom@'s HDD crashed with the final version of strfmon.c, as explained in 9d430a5991d3f64a75fee951a1efab3593207832. Now there are tests in place that cover these code paths. Reviewed by: kib PR: 267410 Github PR: #620 MFC after: 1 week
* strfmon_l: Use specified locale for number formattingJose Luis Duran2022-10-291-24/+23
| | | | | | | | | | | | | strfmon_l does not take fully into consideration the explicitly passed locale to perform the formatting. Parallel universe bug report: https://sourceware.org/bugzilla/show_bug.cgi?id=19633 Obtained from: Darwin Reviewed by: kib PR: 267410 Github PR: #620 MFC after: 1 week
* strfmon_l(3): Add name to the man pageJose Luis Duran2022-10-291-3/+5
| | | | | | | Reviewed by: kib PR: 267410 Github PR: #620 MFC after: 1 week
* strfmon(3): Fix # explanationJose Luis Duran2022-10-251-2/+8
| | | | | | | | | | | | | | There's only one value that specifies the number of digits after the decimal point (oh, sorry, the "radix character") the other specifies the number before... While here, add a little more info on the effects of using the #n value. Obtained from: https://github.com/NetBSD/src/commit/d1dd1a086400ae719bde1f2c45938d9bc1d29e8b Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* strfmon(3): Remove repeated wordsJose Luis Duran2022-10-251-1/+1
| | | | | | | Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* strfmon: Fix formatting of a second fixed-width valueJose Luis Duran2022-10-251-1/+1
| | | | | | | | | | | | | | There is a bug when formatting two consecutive values using fixed-widths and the values need padding. This was because the value of pad_size was zeroed only every other time. Format Before After [%8n] [%8n] [ $123.45] [ $123.45] [ $123.45] [ $123.45] Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* strfmon: Fix an edge case when sep_by_space is 2Jose Luis Duran2022-10-251-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fix an edge case by printing the required space when, the currency symbol succeeds the value, a space separates the sign from the value and the sign position precedes the quantity and the currency symbol. In other words: n_cs_precedes = 0 n_sep_by_space = 2 n_sign_posn = 1 From The Open Group's localeconv[1]: > When {p,n,int_p,int_n}_sep_by_space is 2: > If the currency symbol and sign string are adjacent, a space separates > them; otherwise, a space separates the sign string from the value. Format Before After [%n] [-123.45¤] [- 123.45¤] [1]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/localeconv.html Obtained from: Darwin Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* strfmon: Fix alignment when enclosed by parenthesesJose Luis Duran2022-10-251-2/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | Take into consideration the possibility of quantities enclosed by parentheses when aligning. Matches the examples from The Open Group's: Format Before After %(#5n [$ 123.45] [ $ 123.45 ] Use an alternative pos/neg style [($ 123.45)] [($ 123.45)] [$ 3,456.78] [ $ 3,456.78 ] %!(#5n [ 123.45] [ 123.45 ] Disable the currency symbol [( 123.45)] [( 123.45)] [ 3,456.78] [ 3,456.78 ] https://pubs.opengroup.org/onlinepubs/9699919799/functions/strfmon.html SD5-XSH-ERN-29 is applied, updating the examples for %(#5n and %!(#5n. Obtained from: Darwin Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* strfmon: Trim the SPACE from international currency symbolJose Luis Duran2022-10-251-1/+3
| | | | | | | | | | | | | | | | | | | | | | | The international currency symbol (int_curr_symbol) has a mandatory SPACE character as the last character. Trim this space after reading it, otherwise this extra space will always be printed when displaying the int_curr_symbol. Fixes the output when the international currency format is selected (%i). Locale Format Before After en_US.UTF-8 [%i] [USD 123.45] [USD123.45] fr_FR.UTF-8 [%i] [123,45 EUR ] [123,45 EUR] Note that the en_US.UTF-8 locale states that no space should be printed between the currency symbol and the value (sep_by_space = 0). Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* strfmon: Avoid an out-of-bounds accessJose Luis Duran2022-10-251-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Avoid an out-of-bounds access when trying to set the space_char using an international currency format (%i) and the C/POSIX locale. The current code tries to read the SPACE from int_curr_symbol[3]: currency_symbol = strdup(lc->int_curr_symbol); space_char = *(currency_symbol+3); But on C/POSIX locales, int_curr_symbol is empty. Three implementations have been examined: NetBSD[1], Darwin[2], and Illumos[3]. Only NetBSD has fixed it[4]. Darwin and NetBSD also trim the mandatory final SPACE character after reading it. Locale Format Darwin/NetBSD FreeBSD/Illumos en_US.UTF-8 [%i] [USD123.45] [USD 123.45] fr_FR.UTF-8 [%i] [123,45 EUR] [123,45 EUR ] This commit only fixes the out-of-bounds access. [1]: https://github.com/NetBSD/src/blob/trunk/lib/libc/stdlib/strfmon.c [2]: https://opensource.apple.com/source/Libc/Libc-1439.141.1/stdlib/NetBSD/strfmon.c.auto.html [3]: https://github.com/illumos/illumos-gate/blob/master/usr/src/lib/libc/port/locale/strfmon.c [4]: https://github.com/NetBSD/src/commit/3d7b5d498aa9609f2bc9ece9c734c5f493a8e239 Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* strfmon: Fix typos in source code commentsJose Luis Duran2022-10-251-2/+2
| | | | | | | | | | s/defult/default s/internaltion/international Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* strfmon: Fix typo in constantJose Luis Duran2022-10-251-4/+4
| | | | | | | | | s/SUPRESS_CURR_SYMBOL/SUPPRESS_CURR_SYMBOL Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* strfmon: Code cleanupJose Luis Duran2022-10-251-64/+68
| | | | | | | | | | No functional change intended. Not claiming full style(9) compliance. Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week
* Alter the prototype of qsort_r(3) to match POSIX, which adopted theEd Schouten2022-09-306-19/+47
| | | | | | | | | | | | | | | | | | | | | | | | | | | | glibc-based interface. Unfortunately, the glibc maintainers, despite knowing the existence of the FreeBSD qsort_r(3) interface in 2004 and refused to add the same interface to glibc based on grounds of the lack of standardization and portability concerns, has decided it was a good idea to introduce their own qsort_r(3) interface in 2007 as a GNU extension with a slightly different and incompatible interface. With the adoption of their interface as POSIX standard, let's switch to the same prototype, there is no need to remain incompatible. C++ and C applications written for the historical FreeBSD interface get source level compatibility when building in C++ mode, or when building with a C compiler with C11 generics support, provided that the caller passes a fifth parameter of qsort_r() that exactly matches the historical FreeBSD comparator function pointer type and does not redefine the historical qsort_r(3) prototype in their source code. Symbol versioning is used to keep old binaries working. MFC: never Relnotes: yes Reviewed by: cem, imp, hps, pauamma Differential revision: https://reviews.freebsd.org/D17083
* libc: drop "All rights reserved" from Foundation copyrightsEd Maste2022-08-0411-11/+11
| | | | | | | | | 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: ANSIfy div / ldiv function definitionsEd Maste2022-07-282-4/+2
| | | | | MFC after: 1 week Sponsored by: The FreeBSD Foundation
* Remove "All Rights Reserved" from FreeBSD Foundation libc copyrightsEd Maste2022-07-212-2/+0
| | | | | | | As per the updated FreeBSD copyright template. These were unambiguous cases where the Foundation was the only listed copyright holder. Sponsored by: The FreeBSD Foundation
* libc: Add HISTORY sections to the manual pagesGordon Bergling2022-05-052-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 MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D34759
* getenv(3): Fix two typos in source code commentsGordon Bergling2022-04-091-2/+2
| | | | | | - s/peform/perform/ MFC after: 3 days
* qsort.c: prevent undefined behaviorStefan Eßer2022-01-131-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. MFC after: 1 week
* libc: add clearenv functionMariusz Zaborski2021-11-074-3/+49
| | | | | | | | | | The clearenv(3) function allows us to clear all environment variable in one shot. This may be useful for security programs that want to control the environment or what variables are passed to new spawned programs. Reviewed by: scf, markj (secteam), 0mp (manpages) Differential Revision: https://reviews.freebsd.org/D28223
* Fix null pointer subtraction in mergesort()Dimitry Andric2021-08-271-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. MFC after: 3 days
* _Exit(3): document implementationKonstantin Belousov2021-08-081-6/+14
| | | | | | | | | | | | | | Remove a useless note about unlinking temporary files, they are unlinked in tmpfile(3) [1]. Add a note about __cxa_atexit(). Explain exactly what are the FreeBSD implementation differences between exit() and _Exit(). Noted by: markj [1] Reviewed by: emaste, markj Sponsored by: The FreeBSD Foundation MFC after: 3 days Differential revision: https://reviews.freebsd.org/D31425
* libc qsort(3): Eliminate ambiguous sign comparisonConrad Meyer2021-07-292-1/+8
| | | | | | | | | | | | | | | | The left side of the MIN() expression is the (signed) result of pointer subtraction (ptrdiff_t). The right hand side is the also the (signed) result of pointer subtraction, additionally subtracting the element size ('es'), which is unsigned size_t. This coerces the right-hand expression into an unsigned value. MIN(signed, unsigned) triggers -Wsign-compare. Sorting elements of size greater than SSIZE_MAX is nonsensical, so we can instead treat the element size as ssize_t, leaving the right-hand result the same signedness as the left. Reviewed by: arichardson, kib Differential Revision: https://reviews.freebsd.org/D31292