Skip to content

Commit 4d0d897

Browse files
dschogitster
authored andcommitted
Make sure fsck_commit_buffer() does not run out of the buffer
So far, we assumed that the buffer is NUL terminated, but this is not a safe assumption, now that we opened the fsck_object() API to pass a buffer directly. So let's make sure that there is at least an empty line in the buffer. That way, our checks would fail if the empty line was encountered prematurely, and consequently we can get away with the current string comparisons even with non-NUL-terminated buffers are passed to fsck_object(). Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90a398b commit 4d0d897

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

fsck.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,26 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
237237
return retval;
238238
}
239239

240+
static int require_end_of_header(const void *data, unsigned long size,
241+
struct object *obj, fsck_error error_func)
242+
{
243+
const char *buffer = (const char *)data;
244+
unsigned long i;
245+
246+
for (i = 0; i < size; i++) {
247+
switch (buffer[i]) {
248+
case '\0':
249+
return error_func(obj, FSCK_ERROR,
250+
"unterminated header: NUL at offset %d", i);
251+
case '\n':
252+
if (i + 1 < size && buffer[i + 1] == '\n')
253+
return 0;
254+
}
255+
}
256+
257+
return error_func(obj, FSCK_ERROR, "unterminated header");
258+
}
259+
240260
static int fsck_ident(const char **ident, struct object *obj, fsck_error error_func)
241261
{
242262
char *end;
@@ -284,6 +304,9 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
284304
unsigned parent_count, parent_line_count = 0;
285305
int err;
286306

307+
if (require_end_of_header(buffer, size, &commit->object, error_func))
308+
return -1;
309+
287310
if (!skip_prefix(buffer, "tree ", &buffer))
288311
return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
289312
if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n')

0 commit comments

Comments
 (0)