`IO.FS.Handle.mk` with `.append` leaves the cursor at offset 0, so `Handle.truncate` on a fresh append handle empties the file

オープン
#16,799 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
領域
backend

調査の方向性

Start in src/runtime/io.cpp at lean_io_prim_handle_mk and read the .append branch alongside Handle.truncate, which uses the stream cursor. Reproduce with the provided Lean program: create a.txt, open it in append mode, and truncate without writing. The issue is resolved when the program prints 6 and the existing file contents remain intact.

索引モデルが issue の本文から書いたものです。

説明

bug
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

For Mode.append, lean_io_prim_handle_mk opens with O_WRONLY|O_CREAT|O_APPEND (io.cpp:428) and wraps the descriptor with fdopen(fd, "a") (io.cpp:444-446). fdopen does not move the descriptor's offset, so the cursor stays at 0. fopen("a") would have positioned it at the end. The Mode.append docstring (src/Init/System/IO.lean:672-681) says the cursor is positioned at the end of the file, and Handle.truncate cuts at ftello (io.cpp:598), so truncating an unwritten append handle empties the file. Writes are unaffected because O_APPEND still sends them to the end, so the only effect is truncate (and any other cursor query) before the first write.

The program below writes the 6-byte file a.txt, opens it with IO.FS.withFile "a.txt" .append (i.e. IO.FS.Handle.mk "a.txt" .append) and calls Handle.truncate on the untouched handle, then prints the file's length. It prints 0, though truncate cuts at the handle's read/write cursor and an existing file opened in append mode has its read/write cursor at the end of the file: truncating an untouched append handle must therefore keep all six bytes and the program must print 6. As it is, merely opening a file in append mode and truncating it silently empties the file.

Documented behavior it contradicts, from the IO.FS.Mode.append docstring (src/Init/System/IO.lean) / the reference manual section "Files, File Handles, and Streams":

If the file does not already exist, it is created. If the file already exists, it is opened, and the read/write cursor is positioned at the end of the file.

Context
  • Affected function: lean_io_prim_handle_mk (src/runtime/io.cpp)
  • Upstream: no existing issue found; the cited code is unchanged in nightly-2026-11-26.
Steps to Reproduce
  1. Save the following as input.lean (imports only Init/Std):
import Std
open IO

def main : IO Unit := do
  -- an existing 6-byte file
  IO.FS.writeFile "a.txt" "abcdef"
  -- opened in append mode: per the documented mode, the read/write cursor is at the end of the file
  IO.FS.withFile "a.txt" .append (fun h => h.truncate)
  -- so truncating to the cursor must leave the 6 bytes in place
  IO.println s!"{(← IO.FS.readFile "a.txt").length}"
  1. Compile: lean -c input.c input.lean && leanc -o prog input.c
  2. Run: ./prog

Expected behavior:

6

Actual behavior:

0

(exit status 0; a.txt is now empty)

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 call receives filename = "a.txt" and mode = 4 (the IO.FS.Mode.append scalar), exactly the arguments IO.FS.withFile "a.txt" .append / IO.FS.Handle.mk "a.txt" .append pass, on an existing file that opens fine. The ok result wraps a stream whose read/write cursor is at byte offset 0, whereas the append mode requires the cursor to be at the end of the file (offset 6 here, the existing contents kept). Because the cursor is at 0, the following Handle.truncate (which cuts at the cursor, ftruncate(fileno(fp), ftello(fp)), io.cpp:598) empties the file. No callee hands it that wrong state: open with O_WRONLY|O_CREAT|O_APPEND and fdopen(fd, "a") both succeed and do what they are documented to do. Unlike fopen(..., "a"), fdopen does not move the descriptor's offset, and O_APPEND only redirects writes, so positioning the cursor at the end is exactly the step this function never performs (there is no lseek/fseek in the append branch).

Suggested fix: at the end of the append branch of lean_io_prim_handle_mk (src/runtime/io.cpp:428/444-446), position the stream at the end (e.g. fseek(fp, 0, SEEK_END)) before returning io_result_mk_ok(io_wrap_handle(fp)). Handle.truncate would then keep the existing contents.

Impact

Calling Handle.truncate on a freshly opened append handle deletes the file's contents instead of doing nothing. This is a rare call pattern, but it loses data.

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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

leanprover/lean4 のほかの issue

leanprover/lean4 の issue をすべて見る

似ている issue

Backend & API Design の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。