Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit d4b8de0

Browse files
peffgitster
authored andcommitted
fsck: report integer overflow in author timestamps
When we check commit objects, we complain if commit->date is ULONG_MAX, which is an indication that we saw integer overflow when parsing it. However, we do not do any check at all for author lines, which also contain a timestamp. Let's actually check the timestamps on each ident line with strtoul. This catches both author and committer lines, and we can get rid of the now-redundant commit->date check. Note that like the existing check, we compare only against ULONG_MAX. Now that we are calling strtoul at the site of the check, we could be slightly more careful and also check that errno is set to ERANGE. However, this will make further refactoring in future patches a little harder, and it doesn't really matter in practice. For 32-bit systems, one would have to create a commit at the exact wrong second in 2038. But by the time we get close to that, all systems will hopefully have moved to 64-bit (and if they haven't, they have a real problem one second later). For 64-bit systems, by the time we get close to ULONG_MAX, all systems will hopefully have been consumed in the fiery wrath of our expanding Sun. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7d9a281 commit d4b8de0

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

fsck.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
245245

246246
static int fsck_ident(char **ident, struct object *obj, fsck_error error_func)
247247
{
248+
char *end;
249+
248250
if (**ident == '<')
249251
return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
250252
*ident += strcspn(*ident, "<>\n");
@@ -264,10 +266,11 @@ static int fsck_ident(char **ident, struct object *obj, fsck_error error_func)
264266
(*ident)++;
265267
if (**ident == '0' && (*ident)[1] != ' ')
266268
return error_func(obj, FSCK_ERROR, "invalid author/committer line - zero-padded date");
267-
*ident += strspn(*ident, "0123456789");
268-
if (**ident != ' ')
269+
if (strtoul(*ident, &end, 10) == ULONG_MAX)
270+
return error_func(obj, FSCK_ERROR, "invalid author/committer line - date causes integer overflow");
271+
if (end == *ident || *end != ' ')
269272
return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad date");
270-
(*ident)++;
273+
*ident = end + 1;
271274
if ((**ident != '+' && **ident != '-') ||
272275
!isdigit((*ident)[1]) ||
273276
!isdigit((*ident)[2]) ||
@@ -287,9 +290,6 @@ static int fsck_commit(struct commit *commit, fsck_error error_func)
287290
int parents = 0;
288291
int err;
289292

290-
if (commit->date == ULONG_MAX)
291-
return error_func(&commit->object, FSCK_ERROR, "invalid author/committer line");
292-
293293
if (memcmp(buffer, "tree ", 5))
294294
return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
295295
if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')

t/t1450-fsck.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ test_expect_success '> in name is reported' '
142142
grep "error in commit $new" out
143143
'
144144

145+
# date is 2^64 + 1
146+
test_expect_success 'integer overflow in timestamps is reported' '
147+
git cat-file commit HEAD >basis &&
148+
sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
149+
<basis >bad-timestamp &&
150+
new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
151+
test_when_finished "remove_object $new" &&
152+
git update-ref refs/heads/bogus "$new" &&
153+
test_when_finished "git update-ref -d refs/heads/bogus" &&
154+
git fsck 2>out &&
155+
cat out &&
156+
grep "error in commit $new.*integer overflow" out
157+
'
158+
145159
test_expect_success 'tag pointing to nonexistent' '
146160
cat >invalid-tag <<-\EOF &&
147161
object ffffffffffffffffffffffffffffffffffffffff

0 commit comments

Comments
 (0)