Skip to content

fix(rawobj): committer 时间戳改用 from_chars 严格解析——拒绝溢出与残留 - #10

Closed
OrbitZore wants to merge 1 commit into
mainfrom
fix/commit-timestamp-strict-parse
Closed

OrbitZore wants to merge 1 commit into
mainfrom
fix/commit-timestamp-strict-parse

Conversation

@OrbitZore

Copy link
Copy Markdown
Owner

问题(issue #7)

parse_commit(src/rawobj.cpp)解析 committer 时间戳原先用 std::strtoll(std::string(ts).c_str(), nullptr, 10),既不查 errno == ERANGE 也不查 endptr,且字符集白名单 "0123456789-" 先行放行:

输入 ts 旧行为 新行为
99999999999999999999999(23 位) 饱和 LLONG_MAX 透传到 st_mtime 拒绝(整个 commit 对象)
-99999999999999999999 饱和 LLONG_MIN 拒绝
9223372036854775807 正常 保持接受,值不变
9223372036854775808 饱和 LLONG_MAX 拒绝
12-34 / 1700000000- 解析到 12 / 1700000000,残留静默丢弃 拒绝
- / --1 strtoll 无转换返回 0,静默接受 拒绝
-14182940(pre-epoch) 接受 保持接受,值不变

一致性:同一个溢出 commit 此前在 gitmount 内部已分裂成两种语义——/commits 生成走 libgit2 git__strntol64,对溢出/无数字时间戳拒绝整个对象 → EIO;而 /branch/<name> 路径经 rawobj 却正常 stat。本 PR 使 rawobj 与 libgit2 对齐,消除同仓两种语义。

实现

  • src/rawobj.cpp:committer 时间戳改为 std::from_chars 全消耗严格解析(新增 #include <charconv>)——可选 -、纯数字、必须完整消费、溢出返回 result_out_of_range 一律使该 commit 解析为 nullopt(→ commit_facts_locked 失败 → EIO)。负的 pre-epoch 时间戳保持接受,与 libgit2 一致。
  • 顺带删除字符集白名单判断(find_first_not_of("0123456789-"))与热路径上每次 commit 解析的 std::string(ts) 临时堆分配;清理不再使用的 <cerrno> include。
  • 拒绝路径闭环:parse_commit → nullopt → commit_facts_locked rc≠0 → 分支访问 EIO、/commits 生成时该 ref 被 vlog 跳过——与 libgit2 行为一致。

测试结果

tests/unit/test_rawobj.cpp 新增两个用例:

  • 溢出与残留拒绝:99999999999999999999999 / -99999999999999999999、19 位边界(9223372036854775807 接受且值正确 / 9223372036854775808 拒绝)、残留 12-34 / 1700000000- / - / --1(旧值分别为 12 / 1700000000 / 0 / 0);
  • 负时间戳不误伤:-14182940 +0000 接受且 committer_time == -14182940。

既有三个 parse_commit 用例(正常±时区、gpgsig 续行、缺头拒绝)保持绿。

cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DGITMOUNT_WERROR=ON
cmake --build build -j$(nproc)     # 无告警(-Wall -Wextra -Wpedantic -Werror)
ctest --test-dir build --output-on-failure
# 100% tests passed: unit 0.18s + integration 1.26s(2/2)

改动文件已过 clang-format -i(pinned 18.1.8)。CHANGELOG [Unreleased] / Fixed 已记录。

风险

  • 行为变化:畸形/溢出时间戳的 commit 从"可访问(带垃圾 mtime)"变为 EIO——这正是与 libgit2/git fsck 对齐的目标语义(该对象不可见);正常仓库不受影响。
  • 负时间戳明确保留,pre-epoch 提交不受影响。
  • RFC 0000 语义未变(属 bug 修复对齐,非语义变更,见 CONTRIBUTING "Bug fixes that realign behavior with the RFC do not [need discussion]",且 issue parse_commit 时间戳不检查 ERANGE/endptr——饱和值与畸形串进入 stat 时间戳 #7 即讨论)。
English summary

parse_commit parsed the committer timestamp with strtoll, checking neither errno nor endptr: overlong values saturated to ±LLONG_MAX and leaked into stat mtimes, and the '-' in the character whitelist let 12-34/-/--1 parse to garbage or 0. The same overflowing commit already failed whole /commits generation through libgit2's git__strntol64, so gitmount exposed two semantics for one object. Fix: full-consumption std::from_chars (rejects overflow and residue, keeps negative pre-epoch timestamps as libgit2 does) that fails the commit object → EIO. This also drops the per-commit std::string temporary and the whitelist check on the hot path. New unit tests pin the 19-digit boundary (INT64_MAX accepted, +1 rejected), residue rejection, and negative-timestamp acceptance; existing parse_commit cases stay green; build is warning-clean under -Werror; CHANGELOG [Unreleased] records the fix.

Closes #7

parse_commit 解析 committer 时间戳原先用 strtoll,既不查 ERANGE 也不查
endptr:超长数字饱和到 ±LLONG_MAX 并透传到 stat(2) 的 st_mtime;字符集
白名单放行的 "12-34"/"1700000000-"/"-"/"--1" 分别被静默解析为
12/1700000000/0/0(issue #7)。

改用 std::from_chars 全消耗严格解析:可选 '-'、纯数字、必须完整消费、
溢出(result_out_of_range)与残留一律使整个 commit 对象解析失败。语义与
libgit2 的 git__strntol64 对齐——同一个溢出 commit 此前已使 /commits 生成
EIO,而 /branch/<name> 路径经 rawobj 却正常 stat,同仓两种语义;现在统一
为"该对象不可见"(nullopt → commit_facts_locked 失败 → EIO)。负的
pre-epoch 时间戳保持接受,与 libgit2 一致,避免误伤真实存在的早期提交。
顺带消除热路径上每次 commit 解析的 std::string 临时堆分配,并整段删除
字符集白名单判断;清理不再使用的 <cerrno> include。

单测新增:正/负溢出拒绝、19 位边界(9223372036854775807 接受且值正确、
9223372036854775808 拒绝)、残留(12-34 / 1700000000- / - / --1)拒绝、
负时间戳 -14182940 接受且值正确;既有三个 parse_commit 用例(正常±时区、
gpgsig 续行、缺头拒绝)保持绿。
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in 14 days. It will be closed in 7 days if no further activity occurs.

@github-actions github-actions Bot added the Stale label Nov 16, 2026
@github-actions

Copy link
Copy Markdown

Closed automatically due to inactivity. Feel free to reopen.

@github-actions github-actions Bot closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parse_commit 时间戳不检查 ERANGE/endptr——饱和值与畸形串进入 stat 时间戳

1 participant