`IO.FS.createTempFile` / `createTempDir` abort with a `PATH_MAX` assertion when `TMPDIR` is within 13 bytes of `PATH_MAX`
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 63/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- cpp
- Ambito
- backend, operating-systems
Direzione di ricerca
The affected functions are lean_io_create_tempfile and lean_io_create_tempdir in src/runtime/io.cpp; the report identifies the PATH_MAX assertions at lines 1343, 1350, 1398, and 1405. Read those functions and run the supplied Lean reproducer for both operations with a long TMPDIR. Done means each call returns a catchable IO error instead of aborting; the report also names tests/async/elab/io_enoent_without_file_name.lean as a related test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”) - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
If in doubt, please reach out on [email protected] for responsible disclosure.
(Not a soundness bug.)
Description
lean_io_create_tempdir copies the uv_os_tmpdir result into char path[PATH_MAX], checks only that one '/' fits (io.cpp:1398), then calls lean_always_assert(PATH_MAX >= strlen(path) + 12 + 1) (io.cpp:1405). lean_io_create_tempfile contains the same code (io.cpp:1343 and io.cpp:1350). With a TMPDIR whose length is between PATH_MAX - 13 and PATH_MAX - 1 (4083-4095 bytes on Linux, 1011-1023 bytes on macOS), that assert fails and the process aborts (at exactly PATH_MAX - 1 it is the preceding PATH_MAX >= base_len + 1 + 1 assert that fires). A slightly shorter TMPDIR makes mkdtemp/mkstemp return ENAMETOOLONG, so the assert is the only reason this case aborts instead of returning an error. TMPDIR is user-controlled input, so the functions should return an IO error. A TMPDIR this close to PATH_MAX is far outside real use. Both functions would be fixed in one patch.
The program sets TMPDIR to "/nonexistent-lean-test-dir" and calls IO.FS.createTempFile (resp. IO.FS.createTempDir). That call returns the error "no such file or directory (error code: 2)", as the repo's tests/async/elab/io_enoent_without_file_name.lean test requires. It then sets TMPDIR to "/" followed by PATH_MAX - 10 'a' characters (a path that also does not exist, and leaves no room for the appended "/tmp.XXXXXXXX") and calls the function again. Instead of returning an IO error result, the process is killed by SIGABRT with a LEAN ASSERTION VIOLATION at io.cpp:1350 (createTempFile) / io.cpp:1405 (createTempDir). The same program with a TMPDIR about 75 bytes shorter returns "invalid argument (error code: 63, name too long)" (macOS; 36 on Linux). So the abort is the runtime's own buffer arithmetic, not an OS limit.
IO.FS.createTempFile (lean_io_create_tempfile)
Aborts at io.cpp:1350, PATH_MAX >= strlen(path) + file_pattern_size + 1.
IO.FS.createTempDir (lean_io_create_tempdir)
Aborts at io.cpp:1405, the same assertion.
Context
- Affected functions:
lean_io_create_tempfile,lean_io_create_tempdir(src/runtime/io.cpp) - Upstream: no existing issue found. #16202 (closed by #15175) covered the missing-TMPDIR segfault in the same functions, not this length assert. The cited code is unchanged in nightly-2026-11-26.
Steps to Reproduce
- Save the following as
input.lean(imports only Init/Std). The length 1014 is for macOS (PATH_MAX = 1024); on Linux (PATH_MAX = 4096) use 4082.
import Std.Internal.UV
open Std.Internal.UV
def tryFile (label : String) : IO Unit := do
try
let (_h, p) ← IO.FS.createTempFile
IO.println s!"{label}: ok {p}"
catch e =>
IO.println s!"{label}: error {e}"
def main : IO Unit := do
-- the situation of tests/async/elab/io_enoent_without_file_name.lean
System.osSetenv "TMPDIR" "/nonexistent-lean-test-dir"
tryFile "short"
-- the same, with a TMPDIR whose path leaves no room for the appended "tmp.XXXXXXXX"
-- (PATH_MAX - 10 characters: 1014 on macOS, 4082 on Linux)
let long := "/" ++ String.ofList (List.replicate 1014 'a')
IO.println s!"len={long.length}"
System.osSetenv "TMPDIR" long
tryFile "long"
For createTempDir, replace tryFile with:
def tryDir (label : String) : IO Unit := do
try
let d ← IO.FS.createTempDir
IO.println s!"{label}: ok {d}"
catch e =>
IO.println s!"{label}: error {e}"
- Compile:
lean -c input.c input.lean && leanc -o prog input.c - Run:
./prog
Expected behavior: both calls return a catchable IO error, e.g.
short: error no such file or directory (error code: 2)
file:
len=1015
long: error invalid argument (error code: 63, name too long)
Actual behavior (createTempFile; exit status 134, SIGABRT):
short: error no such file or directory (error code: 2)
file:
len=1015
LEAN ASSERTION VIOLATION
File: .../src/runtime/io.cpp
Line: 1350
PATH_MAX >= strlen(path) + file_pattern_size + 1
libc++abi: terminating due to uncaught exception of type lean::unreachable_reached: 'unreachable' code was reached
createTempDir gives the same output with Line: 1405.
On macOS a TMPDIR of length 1011 aborts in the same way, and 1023 aborts on the earlier PATH_MAX >= base_len + 1 + 1 assert (line 1343 / 1398). Lengths 1010 or 941 make both functions return long: error invalid argument (error code: 63, name too long) and exit 0.
Versions
Lean (version 4.38.0-nightly-2026-11-26, arm64-apple-darwin24.6.0, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
macOS 26.6.2 (arm64)
Additional Information
Root cause analysis. The functions take no arguments; their input is the process's TMPDIR, which any caller may set (here with System.osSetenv, as tests/async/elab/io_enoent_without_file_name.lean does). libuv's uv_os_tmpdir accepts that base and returns 0 with the correct NUL-terminated base path and base_len = PATH_MAX - 9. Since the base does not end in '/', the code appends one (the guard PATH_MAX >= base_len + 1 + 1 holds). Then the check PATH_MAX >= strlen(path) + strlen("tmp.XXXXXXXX") + 1 evaluates to PATH_MAX >= PATH_MAX + 1 and fails. lean_always_assert throws lean::unreachable_reached out of an extern "C" runtime function, which reaches std::terminate and SIGABRT. The function should have returned an IO error result, as it does for the same nonexistent TMPDIR at 25 bytes and at slightly shorter long lengths. The bytes it still needs (the '/', "tmp.XXXXXXXX" and the terminator) beyond what is left of char path[PATH_MAX], and the decision to assert rather than return an error, are in these functions' own code (io.cpp:1340-1351 and 1397-1406).
Suggested fix: replace the lean_always_asserts on PATH_MAX >= strlen(path) + file_pattern_size + 1 (and on PATH_MAX >= base_len + 1 + 1) in both lean_io_create_tempfile and lean_io_create_tempdir with an IO error result, e.g. io_result_mk_error(decode_uv_error(UV_ENAMETOOLONG, nullptr)), when the appended pattern does not fit, the way the following uv_fs_mkstemp/uv_fs_mkdtemp failure path already reports ENAMETOOLONG.
Impact
Only a process whose TMPDIR is within 13 bytes of PATH_MAX (4083-4095 bytes on Linux, 1011-1023 on macOS) is affected: it aborts on IO.FS.createTempFile/withTempFile and IO.FS.createTempDir/withTempDir instead of getting a catchable error.
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
- Lingua principale
- Lean
- Stelle
- 10.3k
- Fork
- 1.1k
- Merge medio
- 16h 45m
- PR unite (30g)
- 361
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leanprover/lean4
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
leanprover/lean4#16876 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16842 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16841 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
leanprover/lean4#16840 ·
I maintainer di solito rispondono entro 1 giorno
-
doc: stray "public" in Bool and HEq documentationForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
leanprover/lean4#16825 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
Thai project names become 'project', 'project-2'… (slug keeps ASCII only)Forse già presa @mastel2 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
storytold/craft-anything#2538 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
compiler-explorer/compiler-explorer#9738 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
openfoodfacts/openfoodfacts-python#573 ·
I maintainer di solito rispondono entro 2 giorni
-
Rasterio reads over HTTPS do not send the Earthdata token and return 500 "HTTP response code: 404"Apertacredentials rasterio
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
developmentseed/titiler-cmr#359 ·
I maintainer di solito rispondono entro 1 giorno