aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/stdio
Commit message (Collapse)AuthorAgeFilesLines
* fgets.3: document gets_s() __STDC_WANT_LIB_EXT1__Graham Percival2024-12-271-1/+2
| | | | | | | | | | This matches the man page for qsort_s(). PR: 281828 Signed-off-by: Graham Percival <gperciva@tarsnap.com> Sponsored by: Tarsnap Backup Inc. (cherry picked from commit d0a3fd34a05794bc5cbf48709001a78e9f85169a)
* libc: fix access mode tests in fmemopen(3)Ed Maste2024-10-291-5/+5
| | | | | | | | | | | | | | | | | | Previously a stream opened as read-only could be written to. Add a test case for the fix. Also correct another incorrect access mode check that worked by accident, and improve the tests for that. PR: 281953 Reported by: Erkki Moorits, fuz Reviewed by: fuz, khng (earlier) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47265 (cherry picked from commit 0953460ce149e6f384aafbcb1e6213dfbf8f6a16) (cherry picked from commit 6b9f7133aba44189d9625c352bc2c2a59baf18ef) (cherry picked from commit 4fbd6e0e3ca8e69d2d3789ecda6e4dd76c34e06a)
* printf(): Save errno earlier.Dag-Erling Smørgrav2024-10-067-27/+34
| | | | | | | | | | | | | | The manual page says %m is replaced with “the string representation of the error code stored in the errno variable at the beginning of the call”. However, we don't actually save `errno` until fairly late in `__vfprintf()`. Make sure it is saved before we do anything that might perturb `errno`. MFC after: 1 week Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D46718 (cherry picked from commit 74f1007fcc838501c74a633792c3f01833bf65e1)
* libc printf_render_errno(): do not use strerror()Konstantin Belousov2024-04-301-3/+4
| | | | (cherry picked from commit aa66995b4c804cbb579f71645d97fe282a798bfc)
* libc: minor style, wrap long linesKonstantin Belousov2024-04-301-2/+4
| | | | (cherry picked from commit bac9d7e8f2931149815c3d5a9ab69594bbdffde6)
* __vprintf(): switch from strerror() to strerror_rl()Konstantin Belousov2024-04-301-1/+5
| | | | | | PR: 278556 (cherry picked from commit f887667694632c829b0599b54ff86a072e93df87)
* fflush: correct buffer handling in __sflushEd Maste2023-11-072-21/+4
| | | | | | | | | | | | | | | | | | Two additional stdio changes followed 86a16ada1ea6 and need to be reverted as part of the fflush fix. This reverts commit 6e13794fbe6e82c21365d0fd66769bf8b19c0197. This reverts commit bafaa70b6f9098d83d074968c8e6747ecec1e118. Fixes: d09a3bf72c0b ("fflush: correct buffer handling in __sflush") Reviewed by: markj Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D42491 (cherry picked from commit 4e0e01bf6511c28212d7dff94fe131a502e13026) (cherry picked from commit d2c65a1c948648f11342274029a3f18b90aa58d2) Approved by: so
* libc: remove unused errno.h includeEd Maste2023-11-071-1/+0
| | | | | | | | | | | | errno.h was added in 44cf1e5eb470, which has been reverted. Fixes: d09a3bf72c0b ("fflush: correct buffer handling in __sflush") Sponsored by: The FreeBSD Foundation (cherry picked from commit 418f026bd5a5084c1c4e2e91ad38051f6caa928c) (cherry picked from commit abe12d2f4ce31c3da0961b1b0a58df11f5a41e19) Approved by: so
* fflush: correct buffer handling in __sflushDag-Erling Smørgrav2023-11-073-17/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes CVE-2014-8611 correctly. The commit that purported to fix CVE-2014-8611 (805288c2f062) only hid it behind another bug. Two later commits, 86a16ada1ea6 and 44cf1e5eb470, attempted to address this new bug but mostly just confused the issue. This commit rolls back the three previous changes and fixes CVE-2014-8611 correctly. The key to understanding the bug (and the fix) is that `_w` has different meanings for different stream modes. If the stream is unbuffered, it is always zero. If the stream is fully buffered, it is the amount of space remaining in the buffer (equal to the buffer size when the buffer is empty and zero when the buffer is full). If the stream is line-buffered, it is a negative number reflecting the amount of data in the buffer (zero when the buffer is empty and negative buffer size when the buffer is full). At the heart of `fflush()`, we call the stream's write function in a loop, where `t` represents the return value from the last call and `n` the amount of data that remains to be written. When the write function fails, we need to move the unwritten data to the top of the buffer (unless nothing was written) and adjust `_p` (which points to the next free location in the buffer) and `_w` accordingly. These variables have already been set to the values they should have after a successful flush, so instead of adjusting them down to reflect what was written, we're adjusting them up to reflect what remains. The bug was that while `_p` was always adjusted, we only adjusted `_w` if the stream was fully buffered. The fix is to also adjust `_w` for line-buffered streams. Everything else is just noise. Fixes: 805288c2f062 Fixes: 86a16ada1ea6 Fixes: 44cf1e5eb470 Sponsored by: Klara, Inc. (cherry picked from commit d09a3bf72c0b5f1779c52269671872368c99f02a) (cherry picked from commit 92709431b14df6c0687446247ac57cfc189ee827) Approved by: so
* fflush: Split a temporary variable in two.Dag-Erling Smørgrav2023-11-061-4/+4
| | | | | | | | | | It is clearer to avoid reusing temporary variables for different purposes. Sponsored by: Klara, Inc. (cherry picked from commit 1f90b4edffe815aebb35e74b79e10593b31f6b75) (cherry picked from commit 1e99535be2ea9c0ef8bc57fc885e9c01fa95d2dd)
* Remove "All Rights Reserved" from Foundation copyrightsEd Maste2023-09-2541-41/+41
| | | | | | | | Sponsored by: The FreeBSD Foundation (cherry picked from commit 7fde0187cc443468561f0a30d589ff0cfe45eef5) (cherry picked from commit 560e22c8fe460e00d16e5268fe1fbb316ad81101) (cherry picked from commit 5b5fa75acff11d871d0c90045f8c1a58fed85365)
* libc: Fix fmemopen(3) prototype in fopen(3) man page.Dag-Erling Smørgrav2023-09-081-6/+9
| | | | | | | | | | | | | | | | | | While here, also update a mention of ANSI C. Sponsored by: Klara, Inc. Reviewed by: kevans, markj Differential Revision: https://reviews.freebsd.org/D41686 (cherry picked from commit 5a57401e7106132b61b16e34365cebf52b773007) libc: Further nit in fopen(3) man page. Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D41687 (cherry picked from commit c9f5889d05b5854be033849a4ff9985699071548)
* 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-236-12/+0
| | | | | | | Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/ Similar commit in main: (cherry picked from commit fa9896e082a1)
* Remove $FreeBSD$: one-line sh patternWarner Losh2023-08-231-1/+0
| | | | | | | Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/ Similar commit in main: (cherry picked from commit d0b2dbfa0ecf)
* Remove $FreeBSD$: one-line .c patternWarner Losh2023-08-23102-204/+0
| | | | | | | Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/ Similar commit in main: (cherry picked from commit 1d386b48a555)
* Remove $FreeBSD$: one-line .h patternWarner Losh2023-08-235-5/+0
| | | | | | | Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/ Similar commit in main: (cherry picked from commit 42b388439bd3)
* Remove $FreeBSD$: two-line .h patternWarner Losh2023-08-2311-22/+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-2531-31/+31
| | | | | | | | | | | 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: Fix size range check in setvbufEd Maste2022-10-071-1/+2
| | | | | | | | | | From enh at google.com via openbsd-tech mailing list via pfg@: The existing test is wrong for LP64, where size_t has twice as many relevant bits as int, not just one. (Found by inspection by rprichard.) (cherry picked from commit 9515313b26beb005a521aff2e6edd4d75cd010da)
* libc/stdio: only roll FILE state back on EINTRKonstantin Belousov2022-09-093-4/+6
| | | | | | PR: 266171 (cherry picked from commit 44cf1e5eb470380442fa8e240e213a71b8fe81d4)
* __swbuf(): styleKonstantin Belousov2022-09-091-2/+2
| | | | (cherry picked from commit 120bacabfd4f383f028bb309581e2366110d0c28)
* libc: Add HISTORY sections to the manual pagesGordon Bergling2022-06-045-5/+51
| | | | | | | | | | | 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)
* libc: Restore fp state upon flush error in fputcMark Johnston2022-03-311-2/+10
| | | | | | | | | | This is akin to commit bafaa70b6f9098d83d074968c8e6747ecec1e118. Reported by: Guy Yur <guyyur@gmail.com> Fixes: 86a16ada1ea6 Sponsored by: The FreeBSD Foundation (cherry picked from commit 6e13794fbe6e82c21365d0fd66769bf8b19c0197)
* libc __sfvwrite(): roll back FILE buffer pointer on fflush errorKonstantin Belousov2022-03-141-2/+11
| | | | | | | | | | | | | __sfvwrite() advances the pointer before calling fflush. If fflush() fails, it is not enough to roll back inside it, because we cannot know how much was advanced by the caller. Reported by: Peter <pmc@citylink.dinoex.sub.org> Reviewed by: markj Sponsored by: The FreeBSD Foundation Fixes: 86a16ada1ea608408cec370171d9f59353e97c77 (cherry picked from commit bafaa70b6f9098d83d074968c8e6747ecec1e118)
* fread.c: fix undefined behaviorStefan Eßer2022-03-041-5/+7
| | | | | | | | | | | | | | | | | | | | | | | | A case of undefined behavior in __fread() has been detected by UBSAN and reported by Mark Millard: /usr/main-src/lib/libc/stdio/fread.c:133:10: runtime error: applying zero offset to null pointer SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior in /usr/main-src/lib/libc/stdio/fread.c:133:10 While being benign (the NULL pointer is later passed to memcpy() with a length argument of 0), this issue causes in the order of 600 Kyua test cases to fail on systems running a world built with WITH_UBSAN and WITH_ASAN. The undefined behavior can be prevented by skipping operations that have no effect for r == 0. Mark Millard has suggested to only skip this code segment if fp->_p == NULL, but I have verified that for the case of r == 0 no further argument checking is performed on the addresses passed to memcpy() and thus no bugs are hidden from the sanitizers due to the simpler condition chosen. (cherry picked from commit 10af8e45a89818754b80315539e167ae49599f17)
* __sflush(): on write error, if nothing was written, reset FILE state backKonstantin Belousov2022-02-011-2/+7
| | | | | | PR: 76398 (cherry picked from commit 86a16ada1ea608408cec370171d9f59353e97c77)
* libc/stdio/vfwscanf.c::convert_wstring(): wcp0 is write-onlyKonstantin Belousov2021-12-061-2/+0
| | | | (cherry picked from commit c8a4a49d582f539c73ef436ceb9d623dd9f20221)
* fread: improve performance for unbuffered readsPedro F. Giffuni2021-06-151-0/+29
| | | | | | | | | | | | | We can use the buffer passed to fread(3) directly in the FILE *. The buffer needs to be reset before each call to __srefill(). This preserves the expected behavior in all cases. The change was found originally in OpenBSD and later adopted by NetBSD. MFC after: 2 weeks Obtained from: OpenBSD (CVS 1.18) Differential Revision: https://reviews.freebsd.org/D30548
* libc: try to skip memcpy in _gettempMateusz Guzik2021-02-011-3/+8
| | | | (cherry picked from commit b22fdf45ff8ef1d1f9a6c28f1d7f59ca4b012da6)
* libc: remove open-coded strlen in _gettempMateusz Guzik2021-02-011-2/+1
| | | | (cherry picked from commit 6fe328ace8f418bab0ab7f78c6fa1f2e757dd8a9)
* libc: skip spurious stat in _gettempMateusz Guzik2021-02-011-21/+0
| | | | | | | It was only done to catch ENOTDIR, but the kernel already returns the error where appropriate. (cherry picked from commit 97a463120bf99819fcb21a781e410fb43dde2a43)
* tmpnam(3): Also mention tmpfile in the ENVIRONMENT sectionGordon Bergling2020-08-071-1/+3
| | | | | | | | | PR: 181785 Submitted by: Kevin P. Neal <kpneal atpobox dot com> MFC after: 1 week Notes: svn path=/head/; revision=364029
* fgetln(3): Add a Caveats SectionGordon Bergling2020-06-181-1/+31
| | | | | | | | | | | Reviewed by: yuripv, bcr (mentor) Approved by: bcr (mentror) Obtained from: OpenBSD MFC after: 7 days Differential Revision: https://reviews.freebsd.org/D24916 Notes: svn path=/head/; revision=362326
* libc manpages: various improvements from NetBSDGordon Bergling2020-06-143-3/+34
| | | | | | | | | | | | | | - 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
* Add HISTORY section to getc(3)Sergio Carlavilla Delgado2020-04-101-1/+17
| | | | | | | | | PR: 240269 Submitted by: Gordon Bergling Differential Revision: https://reviews.freebsd.org/D24295 Notes: svn path=/head/; revision=359773
* Note some functions that appeared in First Edition UnixWarner Losh2020-04-011-0/+7
| | | | | | | | | These functions first appeared in the First Edition of Unix (or earlier in the pdp-7 version). Just claim 1st Edition for all this. The pdp-7 code is too fragmented at this point to extend history that far back. Notes: svn path=/head/; revision=359547
* libc: provide fputc_unlockedKyle Evans2020-02-022-3/+12
| | | | | | | | | | | | | | Among the same justification as the other stdio _unlocked; in addition to an inline version in <stdio.h>, we must provide a function in libc as well for the functionality. This fixes the lang/gcc* builds, which want to use the symbol from libc. PR: 243810 Reported by: antoine, swills, Michael <michael.adm gmail com> X-MFC-With: r357284 Notes: svn path=/head/; revision=357419
* stdio: provide _unlocked variants of fflush, fputc, fputs, fread, fwriteKyle Evans2020-01-3011-16/+102
| | | | | | | | | | | | | | | | fflush_unlocked is currently desired in ports by sysutils/metalog, and redefined as the locked fflush. fputc_unlocked, fputs_unlocked, fread_unlocked, and fwrite_unlocked are currently desired in ports by devel/elfutils, and redefined as the locked fputs, fread, and fwrite respectively. Reviewed by: kib MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D23336 Notes: svn path=/head/; revision=357284
* libc: remove getsEd Maste2019-09-013-44/+10
| | | | | | | | | | | | | | | | | | | | gets is unsafe and shouldn't be used (for many years now). Leave it in the existing symbol version so anything that previously linked aginst it still runs, but do not allow new software to link against it. (The compatability/legacy implementation must not be static so that the symbol and in particular the compat sym gets@FBSD_1.0 make it into libc.) PR: 222796 (exp-run) Reported by: Paul Vixie Reviewed by: allanjude, cy, eadler, gnn, jhb, kib, ngie (some earlier) Relnotes: Yes Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D12298 Notes: svn path=/head/; revision=351659
* Add mkostempsat(3).Mark Johnston2019-07-294-19/+54
| | | | | | | | | | | | | | | This is a variant of mkostemps() which takes a directory descriptor and returns a descriptor for a tempfile relative to that directory. Unlike the other mktemp functions, mkostempsat() can be used in capability mode. Reviewed by: cem Discussed with: brooks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D21031 Notes: svn path=/head/; revision=350420
* GC a historical artifact.Brooks Davis2019-02-071-10/+0
| | | | | | | | | | | | Back in 1993, the fgetln (then fgetline) interface was changed to not return a C string. The change was accomplished by ifdefing out the code that did the termination. Changing the interface would violate our API stability rules so remove the old implementation. Sponsored by: DARPA, AFRL Notes: svn path=/head/; revision=343884
* Follow up on r331936. gets_s(3) will also fail in the same way thatCy Schubert2018-10-231-0/+2
| | | | | | | | | | gets(3) does. This was missed in r331936. Reported by: emaste@ MFC after: 3 days Notes: svn path=/head/; revision=339656
* Fix formatting.Mark Johnston2018-10-201-2/+2
| | | | | | | | MFC after: 1 week Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=339458
* Implement printf(3) family %m format string extension.Konstantin Belousov2018-05-222-1/+28
| | | | | | | | | Reviewed by: ed, dim (code only) Sponsored by: Mellanox Technologies MFC after: 1 week Notes: svn path=/head/; revision=334031
* Conform to Berne Convention.Cy Schubert2018-05-191-1/+1
| | | | | | | | Prompted by: Recent discussion MFC after: 3 days Notes: svn path=/head/; revision=333895
* Don't put multiple names on a single .Nm line. This fixes apropos(1)Edward Tomasz Napierala2018-04-171-2/+12
| | | | | | | | | | | | | | | | 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
* The correct symbol version for FreeBSD 12 is 1.5.Cy Schubert2018-04-031-1/+4
| | | | | | | | | | Reported by: kib@ MFC after: 2 weeks X-MFC with: r331936 Differential Revision: https://reviews.freebsd.org/D12785 Notes: svn path=/head/; revision=331947
* Correct the version number for gets_s(3).Cy Schubert2018-04-031-1/+1
| | | | | | | | | | Reported by: kib@ MFC after: 2 weeks X-MFC with: r331936 Differential Revision: https://reviews.freebsd.org/D12785 Notes: svn path=/head/; revision=331945
* Add gets_s(3) to the man page title (noticed by ed@).Cy Schubert2018-04-031-2/+3
| | | | | | | | | | | | While I'm at it correct the update date in the man page. Reported by: ed@ MFC after: 2 weeks X-MFC with: r331936 Differential Revision: https://reviews.freebsd.org/D12785 Notes: svn path=/head/; revision=331942