Skip to content

Commit 59e4e34

Browse files
peffgitster
authored andcommitted
get_short_sha1: NUL-terminate hex prefix
We store the hex prefix in a 40-byte buffer with the prefix itself followed by 40-minus-len "x" characters. These x's serve no purpose, and the lack of NUL termination makes the prefix string annoying to use. Let's just terminate it. Note that this is in contrast to the binary prefix, which _must_ be zero-padded, because we look at the whole thing during a binary search to find the first potential match in each pack index. The loose-object hex search cannot use the same trick because it has to do a linear walk through the unsorted results of readdir() (and even if it could, you'd want zeroes instead of x's). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0016043 commit 59e4e34

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

sha1_name.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ typedef int (*disambiguate_hint_fn)(const unsigned char *, void *);
1414

1515
struct disambiguate_state {
1616
int len; /* length of prefix in hex chars */
17-
char hex_pfx[GIT_SHA1_HEXSZ];
17+
char hex_pfx[GIT_SHA1_HEXSZ + 1];
1818
unsigned char bin_pfx[GIT_SHA1_RAWSZ];
1919

2020
disambiguate_hint_fn fn;
@@ -291,7 +291,6 @@ static int init_object_disambiguation(const char *name, int len,
291291
return -1;
292292

293293
memset(ds, 0, sizeof(*ds));
294-
memset(ds->hex_pfx, 'x', GIT_SHA1_HEXSZ);
295294

296295
for (i = 0; i < len ;i++) {
297296
unsigned char c = name[i];
@@ -313,6 +312,7 @@ static int init_object_disambiguation(const char *name, int len,
313312
}
314313

315314
ds->len = len;
315+
ds->hex_pfx[len] = '\0';
316316
prepare_alt_odb();
317317
return 0;
318318
}
@@ -346,7 +346,7 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
346346
status = finish_object_disambiguation(&ds, sha1);
347347

348348
if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
349-
return error("short SHA1 %.*s is ambiguous.", ds.len, ds.hex_pfx);
349+
return error("short SHA1 %s is ambiguous.", ds.hex_pfx);
350350
return status;
351351
}
352352

0 commit comments

Comments
 (0)