Skip to content

Commit cf83809

Browse files
authored
Merge pull request libgit2#4883 from pks-t/pks/signature-tz-oob
signature: fix out-of-bounds read when parsing timezone offset
2 parents 20cb30b + 52f859f commit cf83809

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/signature.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ int git_signature__parse(git_signature *sig, const char **buffer_out,
248248

249249
if ((tz_start[0] != '-' && tz_start[0] != '+') ||
250250
git__strntol32(&offset, tz_start + 1,
251-
buffer_end - tz_start + 1, &tz_end, 10) < 0) {
251+
buffer_end - tz_start - 1, &tz_end, 10) < 0) {
252252
/* malformed timezone, just assume it's zero */
253253
offset = 0;
254254
}

tests/commit/signature.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ void test_commit_signature__leading_and_trailing_crud_is_trimmed(void)
4343
assert_name_and_email("nulltoken \xe2\x98\xba", "emeric.fermas@gmail.com", "nulltoken \xe2\x98\xba", "emeric.fermas@gmail.com");
4444
}
4545

46+
void test_commit_signature__timezone_does_not_read_oob(void)
47+
{
48+
const char *header = "A <a@example.com> 1461698487 +1234", *header_end;
49+
git_signature *sig;
50+
51+
/* Let the buffer end midway between the timezone offeset's "+12" and "34" */
52+
header_end = header + strlen(header) - 2;
53+
54+
sig = git__calloc(1, sizeof(git_signature));
55+
cl_assert(sig);
56+
57+
cl_git_pass(git_signature__parse(sig, &header, header_end, NULL, '\0'));
58+
cl_assert_equal_s(sig->name, "A");
59+
cl_assert_equal_s(sig->email, "a@example.com");
60+
cl_assert_equal_i(sig->when.time, 1461698487);
61+
cl_assert_equal_i(sig->when.offset, 12);
62+
63+
git_signature_free(sig);
64+
}
65+
4666
void test_commit_signature__angle_brackets_in_names_are_not_supported(void)
4767
{
4868
cl_git_fail(try_build_signature("<Phil Haack", "phil@haack", 1234567890, 60));

0 commit comments

Comments
 (0)