Skip to content

Commit 84d18c0

Browse files
committed
fsck: it is OK for a tag and a commit to lack the body
When fsck validates a commit or a tag, it scans each line in the header of the object using helper functions such as "start_with()", etc. that work on a NUL terminated buffer, but before a1e920a (index-pack: terminate object buffers with NUL, 2014-12-08), the validation functions were fed the object data in a piece of memory that is not necessarily terminated with a NUL. We added a helper function require_end_of_header() to be called at the beginning of these validation functions to insist that the object data contains an empty line before its end. The theory is that the validating functions will notice and stop when it hits an empty line as a normal end of header (or a required header line that is missing) without scanning past the end of potentially not NUL-terminated buffer. But the theory forgot that in the older days, Git itself happily created objects with only the header lines without a body. This caused Git 2.2 and later to issue an unnecessary warning in some existing repositories. With a1e920a, we do not need to require an empty line (or the body) in these objects to safely parse and validate them. Drop the offending "must have an empty line" check from this helper function, while keeping the other check to make sure that there is no NUL in the header part of the object, and adjust the name of the helper to what it does accordingly. Noticed-by: Wolfgang Denk <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a1e920a commit 84d18c0

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

fsck.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
238238
return retval;
239239
}
240240

241-
static int require_end_of_header(const void *data, unsigned long size,
242-
struct object *obj, fsck_error error_func)
241+
static int verify_headers(const void *data, unsigned long size,
242+
struct object *obj, fsck_error error_func)
243243
{
244244
const char *buffer = (const char *)data;
245245
unsigned long i;
@@ -255,6 +255,15 @@ static int require_end_of_header(const void *data, unsigned long size,
255255
}
256256
}
257257

258+
/*
259+
* We did not find double-LF that separates the header
260+
* and the body. Not having a body is not a crime but
261+
* we do want to see the terminating LF for the last header
262+
* line.
263+
*/
264+
if (size && buffer[size - 1] == '\n')
265+
return 0;
266+
258267
return error_func(obj, FSCK_ERROR, "unterminated header");
259268
}
260269

@@ -305,7 +314,7 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
305314
unsigned parent_count, parent_line_count = 0;
306315
int err;
307316

308-
if (require_end_of_header(buffer, size, &commit->object, error_func))
317+
if (verify_headers(buffer, size, &commit->object, error_func))
309318
return -1;
310319

311320
if (!skip_prefix(buffer, "tree ", &buffer))
@@ -384,7 +393,7 @@ static int fsck_tag_buffer(struct tag *tag, const char *data,
384393
}
385394
}
386395

387-
if (require_end_of_header(buffer, size, &tag->object, error_func))
396+
if (verify_headers(buffer, size, &tag->object, error_func))
388397
goto done;
389398

390399
if (!skip_prefix(buffer, "object ", &buffer)) {

0 commit comments

Comments
 (0)