72 Commits

Author SHA1 Message Date
Rich Felker
291f839a44 fix scanf handling of "0" (followed by immediate EOF) with "%x"
other cases with %x were probably broken too.

I would actually like to go ahead and replace this code in scanf with
calls to the new __intparse framework, but for now this calls for a
quick and unobtrusive fix without the risk of breaking other things.
2012-03-13 12:37:51 -04:00
Rich Felker
5816592389 make stdio open, read, and write operations cancellation points
it should be noted that only the actual underlying buffer flush and
fill operations are cancellable, not reads from or writes to the
buffer. this behavior is compatible with POSIX, which makes all
cancellation points in stdio optional, and it achieves the goal of
allowing cancellation of a thread that's "stuck" on IO (due to a
non-responsive socket/pipe peer, slow/stuck hardware, etc.) without
imposing any measurable performance cost.
2012-02-02 00:11:29 -05:00
Rich Felker
f753049a50 simplify atexit and fflush-on-exit handling 2011-10-14 23:00:24 -04:00
Rich Felker
5f814682b4 don't crash on null strings in printf
passing null pointer for %s is UB but lots of broken programs do it anyway
2011-09-28 22:07:58 -04:00
Rich Felker
ca52e34767 avoid setting FILE lock count when not using flockfile
for now this is just a tiny optimization, but later if we support
cancellation from __stdio_read and __stdio_write, it will be necessary
for the recusrive lock count to be zero in order for these functions
to know they are responsible for unlocking the FILE on cancellation.
2011-09-21 21:30:45 -04:00
Rich Felker
d2e061a2bd more fmemopen null termination fixes
null termination is only added when current size grows.
in update modes, null termination is not added if it does not fit
(i.e. it is not allowed to clobber data).

these rules make very little sense, but that's how it goes..
2011-09-04 21:53:20 -04:00
Rich Felker
e72ee5786b fix some fmemopen behaviors
read should not be allowed past "current size".
append mode should write at "current size", not buffer size.
null termination should not be written except when "current size" grows.
2011-09-04 21:40:42 -04:00
Rich Felker
22e4542348 fmemopen: fix eof handling, hopefully right this time 2011-09-04 16:06:38 -04:00
Rich Felker
f81279ff58 fmemopen fixes
disallow seek past end of buffer (per posix)
fix position accounting to include data buffered for read
don't set eof flag when no data was requested
2011-09-04 16:04:28 -04:00
Rich Felker
7ee3dcb3c6 memstreams: fix incorrect handling of file pos > current size
the addition is safe and cannot overflow because both operands are
positive when considered as signed quantities.
2011-09-04 10:29:04 -04:00
Rich Felker
c88f36f556 optimize seek function for memory streams 2011-09-04 00:08:32 -04:00
Rich Felker
32d67e938e fix twos complement overflow bug in mem streams boundary check
the expression -off is not safe in case off is the most-negative
value. instead apply - to base which is known to be non-negative and
bounded within sanity.
2011-09-04 00:06:01 -04:00
Rich Felker
d4fa6f0e08 implement fmemopen
testing so far has been minimal. may need further work.
2011-09-03 23:26:17 -04:00
Rich Felker
1e69376435 fix some length calculations in memory streams 2011-09-03 20:19:51 -04:00
Rich Felker
1461e02757 implement open_wmemstream
not heavily tested, but it seems to be correct, including the odd
behavior that seeking is in terms of wide character count. this
precludes any simple buffering, so we just make the stream unbuffered.
2011-09-03 19:49:46 -04:00
Rich Felker
b158b32a44 implement open_memstream
this is the first attempt, and may have bugs. only minimal testing has
been performed.
2011-09-03 00:45:21 -04:00
Rich Felker
e95b0a9d10 fix crash in dns code with new stdio locking code 2011-08-01 00:03:50 -04:00
Rich Felker
dba68bf98f add proper fuxed-based locking for stdio
previously, stdio used spinlocks, which would be unacceptable if we
ever add support for thread priorities, and which yielded
pathologically bad performance if an application attempted to use
flockfile on a key file as a major/primary locking mechanism.

i had held off on making this change for fear that it would hurt
performance in the non-threaded case, but actually support for
recursive locking had already inflicted that cost. by having the
internal locking functions store a flag indicating whether they need
to perform unlocking, rather than using the actual recursive lock
counter, i was able to combine the conditionals at unlock time,
eliminating any additional cost, and also avoid a nasty corner case
where a huge number of calls to ftrylockfile could cause deadlock
later at the point of internal locking.

this commit also fixes some issues with usage of pthread_self
conflicting with __attribute__((const)) which resulted in crashes with
some compiler versions/optimizations, mainly in flockfile prior to
pthread_create.
2011-07-30 08:02:14 -04:00
Rich Felker
7683fceede eliminate dependence of perror on printf 2011-07-30 06:11:16 -04:00
Rich Felker
94a0171d80 fix logic error in fread
fread was calling f->read without checking that the file was in
reading mode. this could:
1. crash, if f->read was a null pointer
2. cause unwanted blocking on a terminal already at eof
3. allow reading on a write-only file
2011-07-16 21:24:02 -04:00
Rich Felker
a9e6d01114 printf: "if a precision is specified, the '0' flag shall be ignored." 2011-07-04 11:55:52 -04:00
Rich Felker
cc44d9f201 zero precision with zero value should not inhibit prefix/width printing 2011-07-04 01:57:00 -04:00
Rich Felker
3d54adbe47 printf("%#x",0) should print 0 not 0x0 2011-07-04 01:01:58 -04:00
Rich Felker
e5cb55fedd fix logic in __fwriting 2011-06-30 13:27:08 -04:00
Rich Felker
a0b56b947a add and consolidate nasty stdio_ext junk
hopefully this resolves the rest of the issues with hideously
nonportable hacks in programs that use gnulib.
2011-06-30 12:44:48 -04:00
Rich Felker
7640497f5f implement the nonstandard GNU function fpurge
this is a really ugly and backwards function, but its presence will
prevent lots of broken gnulib software from trying to define its own
version of fpurge and thereby failing to build or worse.
2011-06-30 11:42:33 -04:00
Rich Felker
0e1762539c avoid 64bit warnings when using pointers as entropy for temp names 2011-06-13 20:52:01 -04:00
Rich Felker
8628eff912 fix the last known rounding bug in floating point printing
the observed symptom was that the code was incorrectly rounding up
1.0625 to 1.063 despite the rounding mode being round-to-nearest with
ties broken by rounding to even last place. however, the code was just
not right in many respects, and i'm surprised it worked as well as it
did. this time i tested the values that end up in the variables round,
small, and the expression round+small, and all look good.
2011-05-11 19:58:03 -04:00
Rich Felker
77f15d108e reduce some ridiculously large spin counts
these should be tweaked according to testing. offhand i know 1000 is
too low and 5000 is likely to be sufficiently high. consider trying to
add futexes to file locking, too...
2011-05-06 21:45:48 -04:00
Rich Felker
78c808b126 fix fclose return status logic, again
the previous fix was incorrect, as it would prevent f->close(f) from
being called if fflush(f) failed. i believe this was the original
motivation for using | rather than ||. so now let's just use a second
statement to constrain the order of function calls, and to back to
using |.
2011-05-02 09:18:03 -04:00
Rich Felker
bd67467325 fix undefined call order in fclose, possible lost output depending on compiler
pcc turned up this bug by calling f->close(f) before fflush(f),
resulting in lost output and error on flush.
2011-05-01 22:59:14 -04:00
Rich Felker
d02c50d6a3 minor optimization in puts: use inline putc_unlocked macro for newline 2011-05-01 20:12:51 -04:00
Rich Felker
5efc6af4eb fix 2 eof-related bugs in scanf
1. failed match of literal chars from the format string would always
return matching failure rather than input failure at eof, leading to
infinite loops in some programs.

2. unread of eof would wrongly adjust the character counts reported by
%n, yielding an off-by-one error.
2011-04-25 10:40:25 -04:00
Rich Felker
9080cc153c clean up handling of thread/nothread mode, locking 2011-04-17 16:53:54 -04:00
Rich Felker
eb0e8fa0b1 debloat: use __syscall instead of syscall where possible
don't waste time (and significant code size due to function call
overhead!) setting errno when the result of a syscall does not matter
or when it can't fail.
2011-04-17 16:32:15 -04:00
Rich Felker
69cf09c821 avoid setting errno when checking for tty
setting errno here is completely valid, but some programs, notably
busybox printf, assume that errno will not be set during output and
treat this as an error condition. in any case, skipping it slightly
reduces code size and saves time.
2011-04-15 12:04:13 -04:00
Rich Felker
b172dc8b58 make tmpfile slightly more efficient (use unlink syscall instead of remove) 2011-04-14 21:43:49 -04:00
Rich Felker
e514228043 fix printf("%.9g", 1.1) and similar not dropping trailing zeros 2011-04-12 11:50:52 -04:00
Rich Felker
52458cfa8c fix fputwc return value 2011-04-11 01:52:23 -04:00
Rich Felker
2cff36a84f work around a nasty bug in linux readv syscall
according to posix, readv "shall be equivalent to read(), except..."
that it places the data into the buffers specified by the iov array.
however on linux, when reading from a terminal, each iov element
behaves almost like a separate read. this means that if the first iov
exactly satisfied the request (e.g. a length-one read of '\n') and the
second iov is nonzero length, the syscall will block again after
getting the blank line from the terminal until another line is read.
simply put, entering a single blank line becomes impossible.

the solution, fortunately, is simple. whenever the buffer size is
nonzero, reduce the length of the requested read by one byte and let
the last byte go through the buffer. this way, readv will already be
in the second (and last) iov, and won't re-block on the second iov.
2011-04-09 01:17:55 -04:00
Rich Felker
e72180083e add more legacy functions: setlinebuf and setbuffer 2011-04-05 12:25:31 -04:00
Rich Felker
2f3d02cd83 fix overflow in printf %N$ argument handling 2011-04-05 09:24:03 -04:00
Rich Felker
f9569662c0 fix various floating point rounding and formatting errors in *printf 2011-04-05 09:16:40 -04:00
Rich Felker
98c5583ad5 simplify vdprintf implementation greatly based on recent vfprintf changes
since vfprintf will provide a temporary buffer in the case where the
target FILE has a zero buffer size, don't bother setting up a real
buffer for vdprintf. this also allows us to skip the call to fflush
since we know everything will be written out before vfprintf returns.
2011-04-04 16:30:39 -04:00
Rich Felker
bd57e2b43a use a local temp buffer for unbuffered streams in vfprintf
this change makes it so most calls to fprintf(stderr, ...) will result
in a single writev syscall, as opposed to roughly 2*N syscalls (and
possibly more) where N is the number of format specifiers. in
principle we could use a much larger buffer, but it's best not to
increase the stack requirements too much. most messages are under 80
chars.
2011-04-04 16:24:49 -04:00
Rich Felker
8de03e1a90 don't disable seeking after first seek failure
this could cause problems if the application uses dup2(fd,fileno(f))
to redirect, and the old fd was not seekable but the new fd is.
2011-04-02 13:55:54 -04:00
Rich Felker
9a909fcd91 apparently fseek should not set the error flag on failed seek 2011-04-02 13:54:55 -04:00
Rich Felker
8250742b90 fix tempnam name generation, and a small bug in tmpnam on retry limit 2011-03-29 09:00:22 -04:00
Rich Felker
a88edbec15 make tmpfile fail after exceeding max tries. 2011-03-29 08:37:57 -04:00
Rich Felker
507a9fa6ff fix tmpnam to generate better names, not depend on non-ISO-C symbols 2011-03-29 08:34:47 -04:00