git-2.22.0/refs/packed-backend.c
/*
 * We want to be able to compare mmapped reference records quickly,
 * without totally parsing them. We can do so because the records are
 * LF-terminated, and the refname should start exactly (GIT_SHA1_HEXSZ
 * + 1) bytes past the beginning of the record.
 *
 * But what if the `packed-refs` file contains garbage? We're willing
 * to tolerate not detecting the problem, as long as we don't produce
 * totally garbled output (we can't afford to check the integrity of
 * the whole file during every Git invocation). But we do want to be
 * sure that we never read past the end of the buffer in memory and
 * perform an illegal memory access.
 *
 * Guarantee that minimum level of safety by verifying that the last
 * record in the file is LF-terminated, and that it has at least
 * (GIT_SHA1_HEXSZ + 1) characters before the LF. Die if either of
 * these checks fails.
 */
We want to be able to compare mmapped reference records quickly,
without totally parsing them. We can do so because the records are
LF-terminated, and the refname should start exactly (GIT_SHA1_HEXSZ
+ 1) bytes past the beginning of the record.

But what if the `packed-refs` file contains garbage. We're willing
to tolerate not detecting the problem, as long as we don't produce
totally garbled output (we can't afford to check the integrity of
the whole file during every Git invocation). But we do want to be
sure that we never read past the end of the buffer in memory and
perform an illegal memory access.

Guarantee that minimum level of safety by verifying that the last
record in the file is LF-terminated, and that it has at least
(GIT_SHA1_HEXSZ + 1) characters before the LF. Die if either of
these checks fails.
We want to be able to compare mmapped reference records quickly without totally parsing them . We can do so because the records are LF - terminated and the refname should start exactly ( GIT_SHA1_HEXSZ + 1 ) bytes past the beginning of the record . But what if the ` packed - refs ` file contains garbage . We ' re willing to tolerate not detecting the problem as long as we don ' t produce totally garbled output ( we can ' t afford to check the integrity of the whole file during every Git invocation ) . But we do want to be sure that we never read past the end of the buffer in memory and perform an illegal memory access . Guarantee that minimum level of safety by verifying that the last record in the file is LF - terminated and that it has at least ( GIT_SHA1_HEXSZ + 1 ) characters before the LF . Die if either of these checks fails .
static void verify_buffer_safe(struct snapshot *snapshot)
{
	const char *start = snapshot->start;
	const char *eof = snapshot->eof;
	const char *last_line;
	if (start == eof)
		return;
	last_line = find_start_of_record(start, eof - 1);
	if (*(eof - 1) != '\n' || eof - last_line < the_hash_algo->hexsz + 2)
		die_invalid_line(snapshot->refs->path,
				 last_line, eof - last_line);
}
static void verify_buffer_safe ( struct snapshot * snapshot ) { const char * start = snapshot -> start ; const char * eof = snapshot -> eof ; const char * last_line ; if ( start == eof ) return ; last_line = find_start_of_record ( start , eof - 1 ) ; if ( * ( eof - 1 ) != ' \n ' || eof - last_line < the_hash_algo -> hexsz + 2 ) die_invalid_line ( snapshot -> refs -> path , last_line , eof - last_line ) ; }
We want to be able to compare mmapped reference records quickly,
 without totally parsing them.
We want to be able to compare mmapped reference records quickly without totally parsing them .