aboutsummaryrefslogtreecommitdiff
path: root/libc/stdio/stdio.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Add the missing '--' to shell invocations.Elliott Hughes2021-11-041-1/+1
| | | | | | | | | | | | | | | This came up with POSIX recently. Doesn't seem like it matters since everyone's had this wrong for 40 years, but "meh" --- it's a trivial fix, and it's strictly correct even if nobody needs this, so let's just do it... (Geoff Clare pointed out that my app compat concern "what if someone's relying on this bug to pass flags to the shell?" isn't relevant because while you can indeed do that, you then can't pass a command!) Bug: https://austingroupbugs.net/view.php?id=1440 Test: treehugger Change-Id: I64f6440da55e2dc29d0136ee62007197d2f00d46
* Make fd overflow an abort.Elliott Hughes2021-02-091-14/+9
| | | | | | | | | | | | On LP32, just abort if we're asked to handle an fd that's too big for the `short` field in `struct FILE`. This is unreachable anyway because the ulimit is 32Ki, and this will make issues far more noticeable if we ever do increase that limit (which seems unlikely for LP32 devices). Also rename __finit() to __FILE_init() to match __FILE_close(). Test: treehugger Change-Id: I5db4d6c4529a1f558aff135b4dea071d73666be5
* Fix freopen() where the path is null.Elliott Hughes2021-02-041-37/+41
| | | | | | | | | | | | | | | | | | | | | | | This has been in the standard since C99, but we've never supported it before. It's apparently used by SPIRV-Tools. I tried implementing this the other way (with fcntl(2)) first, but eventually realized that that's more complicated and gives worse results. This implementation assumes that /proc is mounted, but so much of libc relies on that at this point that I don't think there's any realistic case where the fcntl(2) implementation would be preferable, and there are many where it's not. The fact that no-one's mentioned this until now suggests that it's not a heavily used feature anyway. I've also replaced AssertCloseOnExec() with a CloseOnExec() boolean-valued function instead, because it's really annoying getting assertion failures that don't point you at the test line in question, and instead point to some common helper code. Test: treehugger Change-Id: Ia2e53bf2664a4f782581042054ecd492830e2aed
* Fewer copies of ALIGN()/ALIGNBYTES.Elliott Hughes2021-01-111-2/+1
| | | | | | | | Noticed while updating fts.c. Bug: http://b/177003648 Test: treehugger Change-Id: Ic3625c1c3af47c4dafb8ad686bbbddbc82b69b70
* Various coverage improvements.Elliott Hughes2020-08-121-4/+1
| | | | | | | | | | | | | Mostly from extra test cases, but also: * Move the fgets size < 0 assertion into fgets. * Use ELF aliases for strtoq/strtouq rather than duplicating code. * Don't check uname() succeeded, since it can't fail. Test: treehugger Change-Id: I2e6b3b88b0a3eb16bd68be68b9bc9f40d8043291
* Changes for #inclusivefixit.Elliott Hughes2020-07-211-2/+2
| | | | | Test: treehugger Change-Id: I7ff0496c5c2792a41781e74634247f55b0548213
* Remove the ___ hack.Elliott Hughes2019-06-191-2/+0
| | | | | | | | | | | | Plain __ for generated syscalls didn't mean it was a hidden symbol, it just meant "please don't use this". We added ___ to signify that a hidden symbol should be generated, but then we added the map files anyway so you now have to explicitly export symbols. Given that, this convention serves no particular purpose so we may as well just use the nicer names have everything look the same. Test: treehugger Change-Id: If424e17a49c36f4be545f5d283c4561a6ea9c7ea
* Revert fwalk/sfp locking to fix concurrent readsRyan Prichard2019-05-041-5/+12
| | | | | | | | | | | | | | | | | | | The locking can fail in a couple of ways: - A concurrent fread from an unbuffered or line-buffered file flushes the output of other line-buffered files, and if _fwalk locks every file, then the fread blocks until other file reads have completed. - __sfp can initialize a file lock while _fwalk is locking/unlocking it. For now, revert to the behavior Bionic had in previous releases. This commit reverts the file locking parts of commit 468efc80da2504f4ae7de8b5e137426d44dda9d7. Bug: http://b/131251441 Bug: http://b/130189834 Test: bionic unit tests Change-Id: I9e20b9cd8ccd14e7962f7308e174f08af72b56c6
* Merge "Typo fix in comment. O_CLOEXEC is e, not x."Treehugger Robot2019-03-261-1/+1
|\
| * Typo fix in comment. O_CLOEXEC is e, not x.Dan Albert2019-03-261-1/+1
| | | | | | | | | | | | Test: None Bug: None Change-Id: I061fe1d3cac6307d878155f1fabdba70da8e9fd5
* | Merge "Fix internal uses of _PATH_BSHELL."Treehugger Robot2019-03-261-1/+2
|\ \
| * | Fix internal uses of _PATH_BSHELL.Elliott Hughes2019-03-251-1/+2
| |/ | | | | | | | | | | | | | | | | | | We regressed on this recently: code under the upstream-* directories has _PATH_BSHELL defined as a call to __bionic_get_shell_path(). In our own code, we may as well just call it directly. Bug: https://issuetracker.google.com/129030706 Test: ran tests Change-Id: Ic2423f521272be95e67f94771772fe8072636ef0
* / popen: stop using _fwalk.Elliott Hughes2019-03-251-8/+0
|/ | | | | | | | | | We don't need this now that popen always uses O_CLOEXEC, and it's unsafe because _fwalk takes a lock. (In <= P, the equivalent code walked the list without a lock in the child.) Bug: http://b/129156634 Test: ran tests Change-Id: Ic9cee7eb59cfc9397f370d1dc47ea3d3326179ca
* Make fclose/pclose distinct.Elliott Hughes2018-10-011-3/+12
| | | | | | | | | | | | | I failed to convince the compiler/linker to just refrain (via factoring out, attribute `noinline`, or meddling with `--icf=none`), but luckily I noticed that we should have CHECK_FP in each function for a better error message, and the distinct error messages keep the two functions apart. (Also add a missing CHECK_FP to `clearerr_unlocked`.) Bug: http://b/116969207 Test: manual with a modified `crasher` Change-Id: Ic122e90e94f7e22f486be57d3dac7137a225d682
* Make all popen(3) file descriptors O_CLOEXEC.Elliott Hughes2018-08-031-4/+0
| | | | | | | | | | | | POSIX says "The popen() function shall ensure that any streams from previous popen() calls that remain open in the parent process are closed in the new child process". It doesn't appear to disallow all popen(3) file descriptors from being O_CLOEXEC, and it's not obvious why anyone would want them inherited. Let's see if we can make the stricter guarantee... Bug: N/A Test: ran tests Change-Id: I2c85170d730b211637afb8ba10df150ca3237262
* Introduce api to track fd ownership in libc.Josh Gao2018-07-191-1/+20
| | | | | | | | | | | | | Add two functions to allow objects that own a file descriptor to enforce that only they can close their file descriptor. Use them in FILE* and DIR*. Bug: http://b/110100358 Test: bionic_unit_tests Test: aosp/master boots without errors Test: treehugger Change-Id: Iecd6e8b26c62217271e0822dc3d2d7888b091a45
* Reimplement popen(3)/pclose(3).Elliott Hughes2018-07-111-36/+134
| | | | | | | | | | | | | | | | | | | | | | pclose(3) is now an alias for fclose(3). We could add a FORTIFY check that you use pclose(3) if and only if you used popen(3), but there seems little value to that when we can just do the right thing. This patch also adds the missing locking to _fwalk --- we need to lock both the global list of FILE*s and also each FILE* we touch. POSIX says that "The popen() function shall ensure that any streams from previous popen() calls that remain open in the parent process are closed in the new child process", which we implement via _fwalk(fclose) in the child, but we might want to just make *all* popen(3) file descriptors O_CLOEXEC in all cases. Ignore fewer errors in popen(3) failure cases. Improve popen(3) test coverage. Bug: http://b/72470344 Test: ran tests Change-Id: Ic937594bf28ec88b375f7e5825b9c05f500af438
* Remove __overloadable/__RENAME_CLANGGeorge Burgess IV2018-02-061-2/+2
| | | | | | | | | | Now that we have a clang that supports transparent overloads, we can kill all of this cruft, and restore our upstream sources to their untouched glory. Woohoo! Bug: 12231437 Test: Built aosp_marlin; no obvious patch-related aosp_mips issues. Change-Id: I520a19d014f12137f80e43f973dccd6711c571cd
* Speed up __sfileext initialization.Elliott Hughes2017-12-201-1/+4
| | | | | | | | | | | | | | | | | | | | | | | The internal uses don't need to actually initialize a mutex since they'll never escape and don't need locking. It's a small saving, but easy. Before: BM_stdio_scanf_d 465 ns 465 ns 1507891 BM_stdio_scanf_maps 1836 ns 1836 ns 381082 BM_stdio_scanf_maps_baseline 846 ns 845 ns 830881 BM_stdio_scanf_s 419 ns 419 ns 1671979 After: BM_stdio_scanf_d 434 ns 434 ns 1612930 BM_stdio_scanf_maps 1815 ns 1815 ns 386470 BM_stdio_scanf_maps_baseline 875 ns 873 ns 803304 BM_stdio_scanf_s 382 ns 382 ns 1833198 Bug: http://b/70862888 Test: ran tests, benchmarks Change-Id: Ic8822aaca5d8ca1a73390089153d0fe35d91673e
* Move public scanf functions away from implementation.Elliott Hughes2017-11-101-0/+10
| | | | | | Bug: N/A Test: ran tests Change-Id: Ifb8bd91132865f8c6d0b95baf1021af3b3b3c353
* Allow 32-bit fseeko/fseeko64 SEEK_CUR/SEEK_SET to exceed 2 GiBRyan Prichard2017-11-071-1/+1
| | | | | | | | Bug: http://b/68837650 Test: /data/nativetest/bionic-unit-tests/bionic-unit-tests Test: /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static Test: /data/nativetest64/bionic-unit-tests/bionic-unit-tests Change-Id: I367e0238c31d35f76d8ad89fd0aa27ecfeb7c149
* More printf de-duplication.Elliott Hughes2017-11-021-0/+10
| | | | | | | | | | | | | | Fix the 'j' (intmax_t/uintmax_t) length qualifier in the wide variant. (With new tests that fail without this fix.) Fix a typo in the wide support for intmax_t*, which isn't testable because %n is disabled on Android (and will be removed in a later cleanup pass). Also move the public vfprintf/vfwprint functions into stdio.cpp. Bug: http://b/67371539 Test: ran tests Change-Id: Ib003599b1e9cb789044a068940b59e447f2cb7cb
* More missing _unlocked <stdio.h> functions.Elliott Hughes2017-10-311-15/+245
| | | | | | | | | | | | | Also simplify trivial one-liners like perror/puts/fputs, and clean up fread/fwrite slightly. Fix perror to match POSIX. Add basic perror and *_unlocked tests. Bug: N/A Test: ran tests Change-Id: I63f83c8e0c15c3c4096509d17421ac331b6fc23d
* Add explicit null checks to some stdio functions.Josh Gao2017-10-231-0/+36
| | | | | | | | | | Applications fopening files and then blindly trying to read are widespread, leading to a recurring problem of SELinux tightening resulting in segfaults. Add a friendly diagnostic for this case. Bug: http://b/67455242 Test: bionic-unit-tests32/64 on sailfish Change-Id: I1734fa94487c4eff9b55a02c6b01baf6b265d236
* Use O_APPEND for stdio append mode.Elliott Hughes2017-07-251-51/+28
| | | | | | | | | | | "Although not explicitly required by this volume of POSIX.1-2008, a good implementation of append (a) mode would cause the O_APPEND flag to be set." Yeah, about that... Bug: N/A Test: ran tests Change-Id: I23c4bc5c1ebc92e0cb44025d2d313f321f9ffa68
* Remove the HASLB and FREEUB macros.Elliott Hughes2017-07-241-2/+9
| | | | | | | | Because we hate macros. Bug: N/A Test: ran tests Change-Id: I2c94085ff502ec5ce6d8598ec6b3c10e7a4b5510
* Fix stdin/stdout/stderr for pre-M.Dan Albert2016-10-201-0/+13
| | | | | | | | | | | | This wasn't an array of pointers, it was an array of structs. Unfortunately we need a complete type to index into the struct for stdin/stdout/stderr, so add a phony struct that matches the size and alignment of `struct __sFILE`. This property is guaranteed by the static_asserts in libc/bionic/struct_file_test.cpp. Test: mma Bug: http://b/30465923 Change-Id: I8ce851dd64a261703bb44f9b5cd23b7caff4dd68
* Fortify vsnprintf in more cases.Elliott Hughes2016-08-101-2/+29
| | | | | Bug: http://b/30445072 Change-Id: I1893890f0e3b56533eef053eda1bd96a0b9a5119
* Remove more stdio copy/paste.Elliott Hughes2016-08-091-0/+21
| | | | Change-Id: Ia92629b75d2c153ecf1cec711e2f9575eef604ab
* Reimplement remove(3) without the lstat(2).Elliott Hughes2016-08-051-0/+6
| | | | | | | | | This assumes that it's more likely we're unlinking a file than a directory, though even if that's not true, as long as a failed unlink(2) is cheaper than a successful lstat(2) -- which seems likely since there's no data to copy -- we still win. Change-Id: I0210e9cd3d31b8cf1813c55c810262ef327382ed
* More stdio one-liners.Elliott Hughes2016-08-011-1/+94
| | | | | | | This actually turns up a bug in fmemopen, so I guess that's what I'll look at next... Change-Id: I2971ecd1b5a3a3c7f43c22d985f88e389af89e97
* More stdio cleanup.Elliott Hughes2016-07-291-0/+98
| | | | | | | Time to get back to cleaning up stdio, so start with a bunch of easy one-liners... Change-Id: I8df5fdc72500a89b977bfaa6c64c3639198d4e3e
* Add ctermid.Elliott Hughes2016-06-091-0/+5
| | | | Change-Id: I7c7c815c2725df222932db923632c8b6419741ab
* Fix google-explicit-constructor warnings.Chih-Hung Hsieh2016-05-031-1/+1
| | | | | Bug: 28341362 Change-Id: I84effbdfa1b9b39328a909b7f70fe17e7ee316c8
* Add fopen64/freopen64/tmpfile64 aliases.Elliott Hughes2016-02-031-0/+2
| | | | | | | | Our fopen/freopen/tmpfile are already always O_LARGEFILE, but let's add the aliases for _LARGEFILE_SOURCE compatibility. Bug: http://b/24807045 Change-Id: I5d99b3ef3c9f27ce70f13313f6a92e96c7f21f80
* Fix a sign extension bug in stdio.Elliott Hughes2016-01-261-3/+8
| | | | | | | | This also lets us test the EOVERFLOW behavior, which pointed out that the fgetpos/fsetpos return on failure has always been wrong... Bug: http://b/24807045 Change-Id: I35273eb07c8c9155af858adb27569983397580b6
* Implement funopen64.Elliott Hughes2016-01-261-0/+53
| | | | | Bug: http://b/24807045 Change-Id: I161920978161389be34b707cc6ce8e05f760d552
* Support _FILE_OFFSET_BITS=64 for most of <stdio.h>.Elliott Hughes2016-01-261-25/+41
| | | | | | | This doesn't address funopen, but does add fgetpos/fsetpos/fseeko/ftello. Bug: http://b/24807045 Change-Id: Ibff6f00df5fb699c8e8f13b91a75caf024540b73
* Add _seek64 to FILE.Elliott Hughes2016-01-221-24/+233
| | | | | | | | | | Move fdopen/fopen/freopen and change them to initialize _seek64 instead of the legacy _seek. The in-memory streams can stick with _seek for now, since you're not going to fit a > 4GiB in-memory stream on a 32-bit device anyway. Bug: http://b/24807045 Change-Id: I09dcb426817b571415ce24d4d15f364cdda395b3
* Put struct FILE back how NDK-built apps expect it.Elliott Hughes2016-01-221-1/+1
| | | | | | | | | | | | | The first rule of stdio is you never change struct FILE. This broke all NDK-built apps that used stdin/stdout/stderr. (Which is more than you might think, given that those streams don't go anywhere useful. Svelte!) I've added a big code comment because I knew when I removed the field that doing so was a mistake, but I couldn't think why. Bug: http://b/24807045 Bug: http://b/26747402 Change-Id: Ie1233586b223bb1cdf8e354c66d5ff23487a833a
* Simplify fseek/ftell.Elliott Hughes2016-01-211-76/+164
| | | | | | | Another step towards _FILE_OFFSET_BITS=64 support. Bug: http://b/24807045 Change-Id: I00b83c81a7b108176c4d9437bc32611f73b7e967
* Move stdio implementation details around a little.Elliott Hughes2016-01-201-0/+227
Change-Id: I24594426d5479bdd55cbef0ab1b7d76c249dbd0c