Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- `parse_commit` now parses the committer timestamp with strict
full-consumption `std::from_chars`: overflowing timestamps (saturating
to ±LLONG_MAX) and residual or malformed ones (`12-34`,
`1700000000-`, `-`, `--1`) fail the whole commit object (→ EIO)
instead of leaking into `stat(2)` mtimes. This aligns rawobj with
libgit2's `git__strntol64`, which already failed these commits during
`/commits` generation — one repository object no longer has two
semantics. Negative (pre-epoch) timestamps remain accepted, as in
libgit2. Also removes the per-commit `std::string` temporary on the
hot metadata path (issue #7).

## [0.0.4] - 2026-09-26

### Fixed
Expand Down
13 changes: 10 additions & 3 deletions src/rawobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "rawobj.hpp"

#include <algorithm>
#include <cerrno>
#include <charconv>
#include <cstring>

namespace gitmount::rawobj {
Expand Down Expand Up @@ -159,8 +159,15 @@ std::optional<CommitFacts> parse_commit(const char* data, std::size_t len, git_o
// strip a trailing tz if it glued on (shouldn't: tz is separated)
std::size_t sp = ts.find(' ');
if (sp != std::string_view::npos) ts = ts.substr(0, sp);
if (!ts.empty() && ts.find_first_not_of("0123456789-") == std::string_view::npos) {
out.committer_time = std::strtoll(std::string(ts).c_str(), nullptr, 10);
// Strict decimal parse: optional '-', digits only, full consumption,
// overflow rejected (from_chars yields result_out_of_range). Matches
// libgit2's git__strntol64, which fails the whole commit object on an
// overflowing or malformed committer timestamp; negative (pre-epoch)
// timestamps stay accepted, as in libgit2.
std::int64_t t = 0;
const auto [pend, ec] = std::from_chars(ts.data(), ts.data() + ts.size(), t);
if (!ts.empty() && ec == std::errc{} && pend == ts.data() + ts.size()) {
out.committer_time = t;
have_committer = true;
}
}
Expand Down
58 changes: 58 additions & 0 deletions tests/unit/test_rawobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,64 @@ TEST_CASE("parse_commit rejects missing headers", "[rawobj]") {
CHECK_FALSE(parse_commit(no_committer, std::strlen(no_committer), GIT_OID_SHA1).has_value());
}

TEST_CASE("parse_commit rejects overflowing and residual timestamps", "[rawobj]") {
// libgit2's git__strntol64 fails the whole commit object on these; the
// old strtoll path saturated to ±LLONG_MAX or silently dropped residue
// into stat mtimes (issue #7).
const char* overflow_pos =
"tree 0123456789abcdef0123456789abcdef01234567\n"
"committer X <x@y> 99999999999999999999999 +0000\n"
"\n"
"m\n";
CHECK_FALSE(parse_commit(overflow_pos, std::strlen(overflow_pos), GIT_OID_SHA1).has_value());
const char* overflow_neg =
"tree 0123456789abcdef0123456789abcdef01234567\n"
"committer X <x@y> -99999999999999999999 +0000\n"
"\n"
"m\n";
CHECK_FALSE(parse_commit(overflow_neg, std::strlen(overflow_neg), GIT_OID_SHA1).has_value());

// 19-digit boundary: INT64_MAX parses, INT64_MAX + 1 overflows.
const char* int64_max =
"tree 0123456789abcdef0123456789abcdef01234567\n"
"committer X <x@y> 9223372036854775807 +0000\n"
"\n"
"m\n";
auto fmax = parse_commit(int64_max, std::strlen(int64_max), GIT_OID_SHA1);
REQUIRE(fmax.has_value());
CHECK(fmax->committer_time == 9223372036854775807LL);
const char* int64_over =
"tree 0123456789abcdef0123456789abcdef01234567\n"
"committer X <x@y> 9223372036854775808 +0000\n"
"\n"
"m\n";
CHECK_FALSE(parse_commit(int64_over, std::strlen(int64_over), GIT_OID_SHA1).has_value());

// Residue the whitelist let through and strtoll parsed partially or to
// 0: "12-34" → 12, "1700000000-" → 1700000000, "-" / "--1" → 0.
const char* residual[] = {"12-34", "1700000000-", "-", "--1"};
for (const char* ts : residual) {
std::string body = "tree 0123456789abcdef0123456789abcdef01234567\ncommitter X <x@y> ";
body += ts;
body += " +0000\n\nm\n";
INFO(ts);
CHECK_FALSE(parse_commit(body.data(), body.size(), GIT_OID_SHA1).has_value());
}
}

TEST_CASE("parse_commit keeps negative pre-epoch timestamps", "[rawobj]") {
// libgit2 accepts negative committer times (pre-epoch commits exist);
// strict parsing must not regress them.
const char* body =
"tree 0123456789abcdef0123456789abcdef01234567\n"
"committer X <x@y> -14182940 +0000\n"
"\n"
"m\n";
auto f = parse_commit(body, std::strlen(body), GIT_OID_SHA1);
REQUIRE(f.has_value());
CHECK(f->committer_time == -14182940);
}

TEST_CASE("parse_tag extracts the target oid", "[rawobj]") {
const char* body =
"object 0123456789abcdef0123456789abcdef01234567\n"
Expand Down
Loading