Skip to content

Commit ef1286d

Browse files
peffgitster
authored andcommitted
use xsnprintf for generating git object headers
We generally use 32-byte buffers to format git's "type size" header fields. These should not generally overflow unless you can produce some truly gigantic objects (and our types come from our internal array of constant strings). But it is a good idea to use xsnprintf to make sure this is the case. Note that we slightly modify the interface to write_sha1_file_prepare, which nows uses "hdrlen" as an "in" parameter as well as an "out" (on the way in it stores the allocated size of the header, and on the way out it returns the ultimate size of the header). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f2f0267 commit ef1286d

File tree

5 files changed

+13
-12
lines changed

5 files changed

+13
-12
lines changed

builtin/index-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ static void *unpack_entry_data(unsigned long offset, unsigned long size,
441441
int hdrlen;
442442

443443
if (!is_delta_type(type)) {
444-
hdrlen = sprintf(hdr, "%s %lu", typename(type), size) + 1;
444+
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), size) + 1;
445445
git_SHA1_Init(&c);
446446
git_SHA1_Update(&c, hdr, hdrlen);
447447
} else

bulk-checkin.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ static int deflate_to_pack(struct bulk_checkin_state *state,
200200
if (seekback == (off_t) -1)
201201
return error("cannot find the current offset");
202202

203-
header_len = sprintf((char *)obuf, "%s %" PRIuMAX,
204-
typename(type), (uintmax_t)size) + 1;
203+
header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
204+
typename(type), (uintmax_t)size) + 1;
205205
git_SHA1_Init(&ctx);
206206
git_SHA1_Update(&ctx, obuf, header_len);
207207

fast-import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,8 @@ static int store_object(
10351035
git_SHA_CTX c;
10361036
git_zstream s;
10371037

1038-
hdrlen = sprintf((char *)hdr,"%s %lu", typename(type),
1039-
(unsigned long)dat->len) + 1;
1038+
hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu",
1039+
typename(type), (unsigned long)dat->len) + 1;
10401040
git_SHA1_Init(&c);
10411041
git_SHA1_Update(&c, hdr, hdrlen);
10421042
git_SHA1_Update(&c, dat->buf, dat->len);

http-push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ static void start_put(struct transfer_request *request)
361361
git_zstream stream;
362362

363363
unpacked = read_sha1_file(request->obj->sha1, &type, &len);
364-
hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
364+
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
365365

366366
/* Set it up */
367367
git_deflate_init(&stream, zlib_compression_level);

sha1_file.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
14641464
return -1;
14651465

14661466
/* Generate the header */
1467-
hdrlen = sprintf(hdr, "%s %lu", typename(obj_type), size) + 1;
1467+
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
14681468

14691469
/* Sha1.. */
14701470
git_SHA1_Init(&c);
@@ -2930,7 +2930,7 @@ static void write_sha1_file_prepare(const void *buf, unsigned long len,
29302930
git_SHA_CTX c;
29312931

29322932
/* Generate the header */
2933-
*hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
2933+
*hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
29342934

29352935
/* Sha1.. */
29362936
git_SHA1_Init(&c);
@@ -2993,7 +2993,7 @@ int hash_sha1_file(const void *buf, unsigned long len, const char *type,
29932993
unsigned char *sha1)
29942994
{
29952995
char hdr[32];
2996-
int hdrlen;
2996+
int hdrlen = sizeof(hdr);
29972997
write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
29982998
return 0;
29992999
}
@@ -3139,7 +3139,7 @@ static int freshen_packed_object(const unsigned char *sha1)
31393139
int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1)
31403140
{
31413141
char hdr[32];
3142-
int hdrlen;
3142+
int hdrlen = sizeof(hdr);
31433143

31443144
/* Normally if we have it in the pack then we do not bother writing
31453145
* it out into .git/objects/??/?{38} file.
@@ -3157,7 +3157,8 @@ int hash_sha1_file_literally(const void *buf, unsigned long len, const char *typ
31573157
int hdrlen, status = 0;
31583158

31593159
/* type string, SP, %lu of the length plus NUL must fit this */
3160-
header = xmalloc(strlen(type) + 32);
3160+
hdrlen = strlen(type) + 32;
3161+
header = xmalloc(hdrlen);
31613162
write_sha1_file_prepare(buf, len, type, sha1, header, &hdrlen);
31623163

31633164
if (!(flags & HASH_WRITE_OBJECT))
@@ -3185,7 +3186,7 @@ int force_object_loose(const unsigned char *sha1, time_t mtime)
31853186
buf = read_packed_sha1(sha1, &type, &len);
31863187
if (!buf)
31873188
return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
3188-
hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
3189+
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
31893190
ret = write_loose_object(sha1, hdr, hdrlen, buf, len, mtime);
31903191
free(buf);
31913192

0 commit comments

Comments
 (0)