Commit Graph

770 Commits

Author SHA1 Message Date
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
Warner Losh
5310132318 Note some functions that appeared in First Edition Unix
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.
2020-04-01 22:50:41 +00:00
Kyle Evans
b85ea80975 libc: provide fputc_unlocked
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
2020-02-02 19:45:12 +00:00
Kyle Evans
12fe218f0b stdio: provide _unlocked variants of fflush, fputc, fputs, fread, fwrite
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
2020-01-30 03:31:16 +00:00
Ed Maste
7381dcc9ee libc: remove gets
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
2019-09-01 16:12:05 +00:00
Mark Johnston
076574744c Add mkostempsat(3).
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
2019-07-29 19:02:16 +00:00
Brooks Davis
60835849e5 GC a historical artifact.
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
2019-02-07 23:17:44 +00:00
Cy Schubert
3ad12d2782 Follow up on r331936. gets_s(3) will also fail in the same way that
gets(3) does. This was missed in r331936.

Reported by:	emaste@
MFC after:	3 days
2018-10-23 14:16:02 +00:00
Mark Johnston
4ac9afdd2c Fix formatting.
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2018-10-20 17:48:05 +00:00
Konstantin Belousov
e95725feca Implement printf(3) family %m format string extension.
Reviewed by:	ed, dim (code only)
Sponsored by:	Mellanox Technologies
MFC after:	1 week
2018-05-22 11:05:40 +00:00
Cy Schubert
b97c07ccc1 Conform to Berne Convention.
Prompted by:	Recent discussion
MFC after:	3 days
2018-05-19 21:26:07 +00:00
Edward Tomasz Napierala
604f1c416c Don't put multiple names on a single .Nm line. This fixes apropos(1)
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
2018-04-17 09:05:46 +00:00
Cy Schubert
e8a4bb5866 The correct symbol version for FreeBSD 12 is 1.5.
Reported by:	kib@
MFC after:	2 weeks
X-MFC with:	r331936
Differential Revision:	https://reviews.freebsd.org/D12785
2018-04-03 20:53:53 +00:00
Cy Schubert
c515994ef4 Correct the version number for gets_s(3).
Reported by:	kib@
MFC after:	2 weeks
X-MFC with:	r331936
Differential Revision:	https://reviews.freebsd.org/D12785
2018-04-03 20:38:25 +00:00
Cy Schubert
7ad6003d87 Add gets_s(3) to the man page title (noticed by ed@).
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
2018-04-03 19:51:23 +00:00
Cy Schubert
a77546fbb3 Add new gets_s(3) stdio function.
This implements the gets_s(3) function as documented at
http://en.cppreference.com/w/c/io/gets. It facilitates the
optional removal of gets(3).

Reviewed by:	ed
MFC after:	2 weeks
Relnotes:	yes
Differential Revision:	https://reviews.freebsd.org/D12785
2018-04-03 18:52:38 +00:00
Pedro F. Giffuni
bc86c883b0 __printf_render_int(): small type change to match use.
Variable l is consistently used as an int rather than a char.
Sort names while here.

Obtained from:	Apple's Libc-1244.30.3
MFC after:	5 days
2018-02-23 01:11:57 +00:00
Eitan Adler
2b3b473696 fsync(3): correctly document return values
In r268924 the behavior of fflush was changed to return success
on read only streams. Document this.

Reported by:	zrj@DragonFlyBSD.org
2017-12-25 19:49:05 +00:00
Eitan Adler
60419a9c89 fopen.1: document truncation
This documentation truncation similar to POSIX and glibc.

PR:		202545
Reported by:	intron@intron.ac
2017-12-23 05:13:39 +00:00
Stephen J. Kiernan
8b17691466 The function fwscanf() return value is wrong when encountering an early
matching failure.

According to the Open Group documentation for fwscanf:
"Upon successful completion, these functions shall return the number of
successfully matched and assigned input items; this number can be zero in
the event of an early matching failure."

Without this change, fwscanf would return EOF in the case of an early
matching failure, instead of the proper return value of 0.

This change aligns fwscanf(3) with the implementation in fscanf(3).

PR:		202240
Submitted by:	rajendra.sy@gmail.com
Reviewed by:	jhb, cem
Approved by:	sjg (mentor)
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D13288
2017-12-06 21:12:24 +00:00
Poul-Henning Kamp
22adaea1ee Truncate negative lengths to zero 2017-11-27 09:57:37 +00:00
Pedro F. Giffuni
d915a14ef0 libc: further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 2-Clause license, however the tool I
was using mis-identified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.
2017-11-25 17:12:48 +00:00
Pedro F. Giffuni
8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Cy Schubert
aee1526ce7 While discussing the new gets_s.c in D12785, ed@ suggested putting
{}'s around the if (c == EOF) block to prevent potential 'trailing else'
issues from being introduced when refactoring. As my gets_s() code
is based on this, it makes sense to fix the same issue here first
here and now, then do an svn copy again to capture this history).

Suggested by:	ed@ in D12785
2017-11-03 13:08:29 +00:00
Cy Schubert
e223d957c3 Revert r325031. This breaks the build due to __FBSDID.
Pointy hat to:	cy
2017-10-27 05:04:29 +00:00
Cy Schubert
d022f5de53 Ensure all incude statements are kept together by moving the
sys/cdefs.h include appropriately.

MFC after:	1 week
X-MFC with:	r325030
2017-10-27 04:51:05 +00:00
Cy Schubert
d3add8e174 Remove redundant sys/cdefs.h include.
MFC after:	1 week
2017-10-27 04:47:44 +00:00
Konstantin Belousov
b73ac66839 In the stdio cleanup push and pop wrappers, always call libc stubs for
__pthread_cleanup_push/pop_imp instead of symbols also exported from
libthr.

This prevents calls into libthr if libthr is not yet initialized.  The
situation occurs e.g. when an LD_PRELOADed object is not linked
against libthr, but the main binary is.

Reported and tested by:	jbeich
PR:	 220381
Discussed with:	vangyzen
Sponsored by:	The FreeBSD Foundation
MFC after:	13 days
2017-06-30 20:27:51 +00:00
Konstantin Belousov
14d1c3b581 Fix typo in the r320472 change to fgetws(). Return proper value.
Reported by:	Oleg Ginzburg <olevole@olevole.ru>
Reviewed by:	vangyzen
Sponsored by:	The FreeBSD Foundation
MFC after:	13 days
2017-06-30 20:23:46 +00:00
Konstantin Belousov
fda0a14f47 Make stdio deferred cancel-safe.
If used with fopen(3)/fdopen(3)-ed FILEs, stdio accurately uses
non-cancellable internal versions of the functions, i.e. it seems to
be fine with regard to cancellation.  But if the funopen(3) and
f{r,w}open(3) functions were used to open the FILE, and corresponding
user functions create cancellation points (they typically have no
other choice), then stdio code at least leaks FILE' lock.

The change installs cleanup handler which unlocks FILE.  Some minimal
restructuring of the code was required to make it use common return
place to satisfy hand-rolled pthread_cleanup_pop() requirements.

Noted by:	eugen
Reviewed by:	eugen, vangyzen
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D11246
2017-06-29 14:44:17 +00:00