[libc-commits] [libc] [libc] Ignore unknown characters after the fopen mode (PR #236968)
Mike Lothian via libc-commits
libc-commits at lists.llvm.org
Fri Nov 6 02:54:52 PST 2026
FireBurn wrote:
Fair question, and sed is a weak example: 4.10 only uses "rt" when its configure probe says fopen accepts it (`HAVE_FOPEN_RT`), so a sed configured against this libc picks "r". The break is for binaries configured or built elsewhere.
I scanned the 5,800 source tarballs I have for fopen/fdopen calls with a "t" mode. About 40 packages have one. Many are Windows-only (OS/2 in m4, MSYS in bash, nmake helpers in tcl/tk, the msvcp tests in wine), so I checked each hit for a guard. These have no guard and run on Linux:
- lcms2 `cmsIT8LoadFromFile` (`fopen(name, "rt")`)
- libXfont2 `dirfile.c` (`fdopen(fd, "rt")` reading fonts.dir)
- gnupg dirmngr `dns.c`, nghttp2 neverbleed, tpm2-tss `ifapi_io.c`
- tiffset, giflib `gifclrmp`, xxhsum, gst-stats, w3m, yasm, e2fsprogs ext2ed
- wxWidgets `dialup.cpp` reading /proc/net/route
libcap-ng and glib pass "rte" in their tools.
I ran the same calls on glibc 2.44 and on musl: every mode in the list above is accepted, and only a bad first character fails. Upstream's FileMode accepts "rtb" but rejects "rt", "wt", "at", "r+t" and "rz", which is the order dependence this fixes (rejecting by position would be consistent, but then "rtb" would stop working too).
I agree with the doc on not copying undefined behaviour without a reason. The reason here is that these programs are not fixable from our side, since "rt" is what the portable Windows/Unix code in them uses, and rejecting it makes their fopen fail with EINVAL. If you would rather accept only 't' and keep rejecting other unknown characters, that is a one-line change and I can do that instead.
https://github.com/llvm/llvm-project/pull/236968
More information about the libc-commits
mailing list