Skip to content

Commit c3bb0ac

Browse files
peffgitster
authored andcommitted
find_short_object_filename: convert sprintf to xsnprintf
We use sprintf() to format some hex data into a buffer. The buffer is clearly long enough, and using snprintf here is not necessary. And in fact, it does not really make anything easier to audit, as the size we feed to snprintf accounts for the magic extra 42 bytes found in each alt->name field of struct alternate_object_database (which is there exactly to do this formatting). Still, it is nice to remove an sprintf call and replace it with an xsnprintf and explanatory comment, which makes it easier to audit the code base for overflows. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef1286d commit c3bb0ac

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

sha1_name.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disa
9696
}
9797
fakeent->next = alt_odb_list;
9898

99-
sprintf(hex, "%.2s", hex_pfx);
99+
xsnprintf(hex, sizeof(hex), "%.2s", hex_pfx);
100100
for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) {
101101
struct dirent *de;
102102
DIR *dir;
103-
sprintf(alt->name, "%.2s/", hex_pfx);
103+
/*
104+
* every alt_odb struct has 42 extra bytes after the base
105+
* for exactly this purpose
106+
*/
107+
xsnprintf(alt->name, 42, "%.2s/", hex_pfx);
104108
dir = opendir(alt->base);
105109
if (!dir)
106110
continue;

0 commit comments

Comments
 (0)