`IO.currentDir`, `IO.Process.getCurrentDir` and `Std.Internal.UV.System.cwd` fail when the working directory is longer than `PATH_MAX`
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 56/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
Read src/runtime/io.cpp around lean_io_current_dir, src/runtime/process.cpp around lean_io_process_get_current_dir, and src/runtime/uv/system.cpp around get_uv_string and lean_uv_cwd. Reproduce with the provided deep-directory example, then check each API against the expected path and the /bin/pwd -P result. Done means all three APIs return the full path when it exceeds PATH_MAX.
索引モデルが issue の本文から書いたものです。
説明
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
All three runtime functions that return the current working directory read it into a fixed PATH_MAX buffer and give up when the path does not fit, instead of retrying with a larger buffer:
lean_io_current_dir(IO.currentDir) callsgetcwd(buffer, PATH_MAX)on a stack buffer (io.cpp:1490-1491) and returns an error on ERANGE (io.cpp:1495).lean_io_process_get_current_dir(IO.Process.getCurrentDir) callsgetcwdwith a fixedchar[PATH_MAX]buffer (process.cpp:59-65) and returns the ERANGE error.get_uv_string(used bylean_uv_cwd,Std.Internal.UV.System.cwd) starts with aPATH_MAXbuffer and retries only onUV_ENOBUFS(src/runtime/uv/system.cpp:119-139). libuv'suv_cwdreturnsUV_ENOBUFSwith a size only when the path fits in its ownPATH_MAXscratch buffer; for a longer path it returnsUV_ERANGE, so the retry never happens.
A working directory longer than PATH_MAX can be reached by relative chdirs, the OS can report it (getcwd(NULL, 0) or a large enough buffer returns it), and the docstrings promise the current directory and state no limit. So the error is wrong. Many tools have this limit, so it is low priority; a single fix (grow and retry on ERANGE, or getcwd(NULL, 0)) would cover all three. The same fixed-buffer pattern is also in realpath (io.cpp:1084).
Each program below moves the process into a working directory whose absolute path is over 8000 bytes long (40 components of 200 bytes, entered one short relative IO.Process.setCurrentDir at a time, none of which the OS rejects), then reads that directory through the API under test. In the same run it runs /bin/pwd -P in that directory from a child process and checks that the printed path equals the path the program built, which shows that the value is obtainable and not an OS limit.
IO.currentDir (lean_io_current_dir)
The call fails with "failed to retrieve current working directory", a userError, so the Lean program never sees its working directory. The cause is char buffer[PATH_MAX]; char* cwd = getcwd(buffer, sizeof(buffer));: when the path does not fit, getcwd sets ERANGE and the function returns io_result_mk_error("failed to retrieve current working directory") instead of growing the buffer.
Documented behavior it contradicts (IO.currentDir docstring, src/Init/System/IO.lean:964):
Returns the current working directory of the executing process.
IO.Process.getCurrentDir (lean_io_process_get_current_dir)
The call fails with unsupported operation (error code: 34, result too large), an IO.Error carrying ERANGE, instead of returning the directory. The cause is char path[PATH_MAX]; if (getcwd(path, PATH_MAX)) ... else return io_result_mk_error(decode_io_error(errno, nullptr));: when the path does not fit the fixed buffer, getcwd sets ERANGE and the function reports the error rather than retrying with a buffer of the size the path needs.
Documented behavior it contradicts (IO.Process.getCurrentDir docstring, src/Init/System/IO.lean:1389):
Returns the current working directory of the calling process.
Std.Internal.UV.System.cwd (lean_uv_cwd / get_uv_string)
The call fails with unsupported operation (error code: 34, result too large), an IO.Error carrying libuv's UV_ERANGE. The shared helper get_uv_string only handles a too-small buffer reported as UV_ENOBUFS with a size. libuv's uv_cwd reports it as UV_ERANGE with no size, so the retry never happens. That contradicts the helper's own comment ("retrying with the size it asks for when the buffer is too small"). Its sibling lean_uv_get_process_title in the same file doubles the buffer until it fits precisely because "libuv does not report the size the title needs". libuv documents only the ENOBUFS path, so this is partly a libuv limit that Lean passes through, but Lean could double the buffer on ERANGE in the same way.
Documented behavior it contradicts (src/Std/Internal/UV/System.lean:140):
Gets the current working directory.
Context
- Affected functions:
lean_io_current_dir(src/runtime/io.cpp),lean_io_process_get_current_dir(src/runtime/process.cpp),get_uv_string/lean_uv_cwd(src/runtime/uv/system.cpp) - Upstream: no existing issue found; the cited code is identical in nightly-2026-11-26.
Steps to Reproduce
- Save the following as
input.lean(imports only Init/Std). This is theIO.currentDirversion. For the other two functions, replace theIO.currentDircall insideruntime(and the label) withIO.Process.getCurrentDirorStd.Internal.UV.System.cwd(for the latter, comparecwd == expecteddirectly, since it returns aString):
import Std
/-
The process moves into a working directory whose absolute path is longer than `PATH_MAX` (such a
directory is reached one component at a time, as a deeply nested build tree or cache is), and then
reads that directory with `IO.currentDir`, and with the operating system itself, by running
`/bin/pwd -P` in the same directory from a child process.
-/
def component : String := String.ofList (List.replicate 200 'd')
def sh (script : String) : IO Unit := do
let child ← IO.Process.spawn { cmd := "/bin/sh", args := #["-c", script], stdin := .null }
discard child.wait
def main : IO Unit := do
let base := (← IO.currentDir).toString
-- 40 components in total; `mkdir -p` is given 4 at a time so each argument stays below
-- macOS's PATH_MAX of 1024 (on Linux 4 rounds of 10 work as well)
let rounds := 10
let perRound := 4
let chunk := String.intercalate "/" (List.replicate perRound component)
for _ in List.range rounds do
sh s!"mkdir -p {chunk}"
for _ in List.range perRound do
IO.Process.setCurrentDir component
let expected := base ++ String.join (List.replicate (rounds * perRound) ("/" ++ component))
let runtime ← try
let cwd ← IO.currentDir
pure (if cwd.toString == expected then "ok" else "wrong value")
catch e => pure s!"error: {e}"
IO.println s!"IO.currentDir: {runtime}"
sh "/bin/pwd -P > pwd.txt"
let osSays ← try
let out ← IO.FS.readFile "pwd.txt"
pure (out.startsWith expected && out.length == expected.length + 1)
catch _ => pure false
IO.println s!"operating system reports the directory: {osSays}"
- Compile:
lean -c input.c input.lean && leanc -o prog input.c - Run:
./prog(it creates the deepddd…tree in the current directory)
Expected behavior:
IO.currentDir: ok
operating system reports the directory: true
(and likewise IO.Process.getCurrentDir: ok / System.cwd: ok)
Actual behavior:
IO.currentDir: error: failed to retrieve current working directory
operating system reports the directory: true
IO.Process.getCurrentDir: error: unsupported operation (error code: 34, result too large)
operating system reports the directory: true
System.cwd: error: unsupported operation (error code: 34, result too large)
operating system reports the directory: true
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. In each case the process is in a real working directory: it entered the directory with the runtime's own IO.Process.setCurrentDir (uv_chdir) one 200-byte component at a time, and a child process prints exactly that path. No callee hands back a wrong value. getcwd correctly reports ERANGE because the fixed PATH_MAX buffer is too small, and uv_cwd returns UV_ERANGE for the PATH_MAX-sized buffer. io_result_mk_error/decode_io_error/io_result_mk_uv_error encode exactly what they are given. The decision to stop instead of retrying with a larger buffer is each function's own: io.cpp:1495, process.cpp:63-64, and the UV_ENOBUFS-only retry in get_uv_string. On macOS, a C probe in the same 8102-byte directory shows that getcwd(NULL, 0) and getcwd with a 64 KiB buffer both succeed and return the full 8102-byte path.
Suggested fix: retry with a larger buffer on ERANGE in all three places: getcwd(NULL, 0) (supported by glibc and macOS) or doubling, as lean_uv_get_process_title does in system.cpp:36-49. For get_uv_string, also double the buffer when uv_cwd returns UV_ERANGE.
Impact
Only processes whose working directory is longer than PATH_MAX (4096 bytes on Linux, 1024 on macOS), for example inside a very deep generated tree, are affected: they get an error from IO.currentDir, IO.Process.getCurrentDir and System.cwd instead of the path. This is very rare.
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
- 主要言語
- Lean
- スター
- 10.3k
- フォーク
- 1.1k
- 平均マージ
- 16時間 45分
- マージ済み PR(30日)
- 361
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
leanprover/lean4 のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
leanprover/lean4#16876 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
leanprover/lean4#16842 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
leanprover/lean4#16841 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
leanprover/lean4#16840 ·
メンテナーはふだん 1 日以内に返信
-
doc: stray "public" in Bool and HEq documentation再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
leanprover/lean4#16825 ·
メンテナーはふだん 1 日以内に返信
leanprover/lean4 の issue をすべて見る
似ている issue
-
Thai project names become 'project', 'project-2'… (slug keeps ASCII only)対応中かも @mastel2 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
storytold/craft-anything#2538 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
compiler-explorer/compiler-explorer#9738 ·
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 3 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
openfoodfacts/openfoodfacts-python#573 ·
メンテナーはふだん 2 日以内に返信
-
Rasterio reads over HTTPS do not send the Earthdata token and return 500 "HTTP response code: 404"オープンcredentials rasterio
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
developmentseed/titiler-cmr#359 ·
メンテナーはふだん 1 日以内に返信