Commit graph

799 commits

Author SHA1 Message Date
Kyle Evans e55512504d Prepare the system for _FORTIFY_SOURCE
Notably:
- libc needs to #undef some of the macros from ssp/* for underlying
  implementations
- ssp/* wants a __RENAME() macro (snatched more or less from NetBSD)

There's some extra hinkiness included for read(), since libc spells it
as "_read" while the rest of the world spells it "read."

Reviewed by:	imp, ngie
Sponsored by:	Stormshield
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D32307
2024-05-13 00:23:50 -05:00
Konstantin Belousov aa66995b4c libc printf_render_errno(): do not use strerror()
Reviewed by:	imp
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44916
2024-04-23 22:44:07 +03:00
Konstantin Belousov bac9d7e8f2 libc: minor style, wrap long lines
Reviewed by:	imp
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44916
2024-04-23 22:44:07 +03:00
Konstantin Belousov f887667694 __vprintf(): switch from strerror() to strerror_rl()
This eliminates the use of non-thread-safe function in printf*() family,
and make the call locale-aware.  Also, it stops obliterating the
strerror() static buffer, which aligns with the POSIX requirement that
implementations must behave as if no standard-mandated functions call
strerror().

PR:	278556
Reported by:	Jonathan Gruber <jonathan.gruber.jg@gmail.com>
Reviewed by:	imp
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44916
2024-04-23 22:43:01 +03:00
Brooks Davis 63b179b68d libc: don't try to export __sF
This symbol has been static since 2008 (commit 1e98f88776).

Differential Revision:	https://reviews.freebsd.org/D44188
2024-04-16 22:14:20 +01:00
Maxim Konovalov 1250c96479 fclose.3: remove a confusing sentence
PR:		277037
Reviewed by:	oshogbo
2024-02-16 18:50:12 +00:00
Warner Losh a2f733abcf lib: Automated cleanup of cdefs and other formatting
Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by:		Netflix
2023-11-26 22:23:59 -07:00
Warner Losh dc36d6f9bb lib: Remove ancient SCCS tags.
Remove ancient SCCS tags from the tree, automated scripting, with two
minor fixup to keep things compiling. All the common forms in the tree
were removed with a perl script.

Sponsored by:		Netflix
2023-11-26 22:23:28 -07:00
Brooks Davis 9fc0ff9d85 libc: remove some obsolete VCS data
These wide char support files were copied from the previous versions
with expanded $FreeBSD$ strings in #if 0 blocks.  Remove them and the
scssid definitions in the same #if 0 blocks.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D42688
2023-11-21 22:46:43 +00:00
Brooks Davis 1ca63a8219 libc: Remove empty comments in Symbol.map
These were left over from $FreeBSD$ removal.

Reviewed by:	emaste
Differential Revision:	https://reviews.freebsd.org/D42612
2023-11-15 17:51:03 +00:00
Ed Maste 4e0e01bf65 fflush: correct buffer handling in __sflush
Two additional stdio changes followed 86a16ada1e and need to be
reverted as part of the fflush fix.

This reverts commit 6e13794fbe.
This reverts commit bafaa70b6f.

Fixes: d09a3bf72c ("fflush: correct buffer handling in __sflush")
Reviewed by: markj
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D42491
2023-11-07 11:03:34 -05:00
Ed Maste 418f026bd5 libc: remove unused errno.h include
errno.h was added in 44cf1e5eb4, which has been reverted.

Fixes: d09a3bf72c ("fflush: correct buffer handling in __sflush")
Sponsored by: The FreeBSD Foundation
2023-11-07 10:23:20 -05:00
Dag-Erling Smørgrav d09a3bf72c fflush: correct buffer handling in __sflush
This fixes CVE-2014-8611 correctly.

The commit that purported to fix CVE-2014-8611 (805288c2f0) only hid
it behind another bug.  Two later commits, 86a16ada1e and
44cf1e5eb4, 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: 805288c2f0
Fixes: 86a16ada1e
Fixes: 44cf1e5eb4
Sponsored by:	Klara, Inc.
2023-11-07 08:21:12 -05:00
Dag-Erling Smørgrav 1f90b4edff fflush: Split a temporary variable in two.
It is clearer to avoid reusing temporary variables for different
purposes.

Sponsored by:	Klara, Inc.
2023-11-03 13:38:12 -04:00
Warner Losh 559a218c9b libc: Purge unneeded cdefs.h
These sys/cdefs.h are not needed. Purge them. They are mostly left-over
from the $FreeBSD$ removal. A few in libc are still required for macros
that cdefs.h defines. Keep those.

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D42385
2023-11-01 16:44:30 -06:00
Dag-Erling Smørgrav dc41de36ab libc: Fix missing or misspelled MLINKS.
Differential Revision:	https://reviews.freebsd.org/D42192
2023-10-18 16:07:46 +02:00
Dag-Erling Smørgrav bce0bef3c6 libc: Implement N2680.
This adds specific width length modifiers in the form of wN and wfN (where N is 8, 16, 32, or 64) which allow printing intN_t and int_fastN_t without resorting to casts or PRI macros.

Reviewed by:	imp, emaste
Differential Revision:	https://reviews.freebsd.org/D41725
2023-09-07 06:40:14 +00:00
Dag-Erling Smørgrav c9f5889d05 libc: Further nit in fopen(3) man page.
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D41687
2023-09-01 22:53:35 +00:00
Dag-Erling Smørgrav 5a57401e71 libc: Fix fmemopen(3) prototype in fopen(3) man page.
While here, also update a mention of ANSI C.

Sponsored by:	Klara, Inc.
Reviewed by:	kevans, markj
Differential Revision:	https://reviews.freebsd.org/D41686
2023-09-01 20:56:26 +00:00
Dag-Erling Smørgrav aca3bd1602 libc: Fix fixed-width case in the new integer parser.
Fixes:		d9dc1603d6
Differential Revision:	https://reviews.freebsd.org/D41622
2023-08-28 23:38:19 +00:00
Dag-Erling Smørgrav 76edfabbec libc: Document support for binary integers.
Reviewed by:	debdrup, emaste
Differential Revision:	https://reviews.freebsd.org/D41522
2023-08-28 15:34:17 +00:00
Dag-Erling Smørgrav d9dc1603d6 libc: Implement N2630.
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
2023-08-28 15:33:51 +00:00
Warner Losh b2c76c41be Remove $FreeBSD$: one-line nroff pattern
Remove /^\.\\"\s*\$FreeBSD\$$\n/
2023-08-16 11:55:15 -06:00
Warner Losh fa9896e082 Remove $FreeBSD$: two-line nroff pattern
Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/
2023-08-16 11:55:10 -06:00
Warner Losh d0b2dbfa0e Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
2023-08-16 11:55:03 -06:00
Warner Losh 1d386b48a5 Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:42 -06:00
Warner Losh 42b388439b Remove $FreeBSD$: one-line .h pattern
Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
2023-08-16 11:54:23 -06:00
Warner Losh b3e7694832 Remove $FreeBSD$: two-line .h pattern
Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
2023-08-16 11:54:16 -06:00
Warner Losh 4d846d260e spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD
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
2023-05-12 10:44:03 -06:00
Mark Johnston 68ca8363c7 libc: Use secure_getenv(3) where appropriate
No functional change intended.

Reviewed by:	mjg, imp, kib
Differential Revision:	https://reviews.freebsd.org/D39278
2023-03-27 08:56:22 -04:00
Ed Maste 9515313b26 libc: Fix size range check in setvbuf
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.)
2022-10-04 12:03:56 -04:00
Konstantin Belousov 44cf1e5eb4 libc/stdio: only roll FILE state back on EINTR
Reported by:	JunT <takimoto-j@kba.biglobe.ne.jp>
PR:	266171
Reviewed by:	emaste, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D36427
2022-09-03 04:06:41 +03:00
Konstantin Belousov 120bacabfd __swbuf(): style
Reviewed by:	emaste, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D36427
2022-09-03 04:06:41 +03:00
Ed Maste 5b5fa75acf libc: drop "All rights reserved" from Foundation copyrights
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
2022-08-04 16:57:50 -04:00
Gordon Bergling 4b7f35db44 libc: Add HISTORY sections to the manual pages
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
2022-05-05 18:46:32 +02:00
Mark Johnston 6e13794fbe libc: Restore fp state upon flush error in fputc
This is akin to commit bafaa70b6f.

Reported by:	Guy Yur <guyyur@gmail.com>
Fixes:		86a16ada1e
MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
2022-03-28 11:23:46 -04:00
Konstantin Belousov bafaa70b6f libc __sfvwrite(): roll back FILE buffer pointer on fflush error
__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
MFC after:	1 week
Fixes:	86a16ada1e
2022-03-06 10:29:51 -05:00
Konstantin Belousov 86a16ada1e __sflush(): on write error, if nothing was written, reset FILE state back
otherwise the data is just dropped.  Check for current position equal to
the buffer base at the entry of the function; if not equal, setvbuf()
was done from the write method and it is not our business to override
the decision.

PR:	76398
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D34007
2022-01-25 17:26:05 +02:00
Stefan Eßer 10af8e45a8 fread.c: fix undefined behavior
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.

Reported by:	Mark Millard (marklmi@yahoo.com)
Tested by:	Mark Millard (marklmi@yahoo.com)
Differential Revision:	https://reviews.freebsd.org/D33903
MFC after:	2 weeks
2022-01-16 00:43:56 +01:00
Konstantin Belousov c8a4a49d58 libc/stdio/vfwscanf.c::convert_wstring(): wcp0 is write-only 2021-11-29 17:39:51 +02:00
Bryan Drewery 8f8a794775 getdelim(3): Fix losing data on [EAGAIN]
Currently when an [EAGAIN] is encountered we return a partial result
that does not contain the delimeter.  On the next (successful) read we
were returning the next part of the line without the preceding string
from the first failed call.

Fix this by using the same mechanism as ungetc(3) does.  For the buffered
case we could simply set fp->_r and fp->_p back to their values before
sappend() is ran but for simplicity ungetc(3) is done in there as well.

Reviewed by:	kib
Differential Revision: https://reviews.freebsd.org/D31687
2021-09-02 11:26:26 -07:00
Pedro F. Giffuni a45843c8ed fread: improve performance for unbuffered reads
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
2021-06-01 16:00:28 -05:00
Yuri Pankov 01a856c666 open_memstream(3): fix typo
While here, pet mandoc: .Tn -> .Vt
2021-04-28 12:18:25 +03:00
Mateusz Guzik b22fdf45ff libc: try to skip memcpy in _gettemp 2021-01-24 11:06:36 +00:00
Mateusz Guzik 6fe328ace8 libc: remove open-coded strlen in _gettemp 2021-01-24 11:06:36 +00:00
Mateusz Guzik 97a463120b libc: skip spurious stat in _gettemp
It was only done to catch ENOTDIR, but the kernel already returns the
error where appropriate.
2021-01-24 11:06:36 +00:00
Gordon Bergling 1f325602e4 tmpnam(3): Also mention tmpfile in the ENVIRONMENT section
PR:		181785
Submitted by:	Kevin P. Neal <kpneal atpobox dot com>
MFC after:	1 week
2020-08-07 16:20:07 +00:00
Gordon Bergling b7d33ccd65 fgetln(3): Add a Caveats Section
Reviewed by:	yuripv, bcr (mentor)
Approved by:	bcr (mentror)
Obtained from:	OpenBSD
MFC after:	7 days
Differential Revision:	https://reviews.freebsd.org/D24916
2020-06-18 13:13:04 +00:00
Gordon Bergling e0f7c06de2 libc manpages: various improvements from NetBSD
- 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
2020-06-14 05:59:30 +00:00
Sergio Carlavilla Delgado 73129d4797 Add HISTORY section to getc(3)
PR:		240269
Submitted by:	Gordon Bergling
Differential Revision:	https://reviews.freebsd.org/D24295
2020-04-10 09:37:20 +00:00