Skip to content

Commit 53a0f9f

Browse files
committed
Merge branch 'jk/fast-import-cleanup'
Code clean-up. * jk/fast-import-cleanup: pack.h: define largest possible encoded object size encode_in_pack_object_header: respect output buffer length fast-import: use xsnprintf for formatting headers fast-import: use xsnprintf for writing sha1s
2 parents f494890 + 2c5e286 commit 53a0f9f

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

builtin/pack-objects.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
239239
unsigned long limit, int usable_delta)
240240
{
241241
unsigned long size, datalen;
242-
unsigned char header[10], dheader[10];
242+
unsigned char header[MAX_PACK_OBJECT_HEADER],
243+
dheader[MAX_PACK_OBJECT_HEADER];
243244
unsigned hdrlen;
244245
enum object_type type;
245246
void *buf;
@@ -286,7 +287,8 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
286287
* The object header is a byte of 'type' followed by zero or
287288
* more bytes of length.
288289
*/
289-
hdrlen = encode_in_pack_object_header(type, size, header);
290+
hdrlen = encode_in_pack_object_header(header, sizeof(header),
291+
type, size);
290292

291293
if (type == OBJ_OFS_DELTA) {
292294
/*
@@ -352,13 +354,15 @@ static off_t write_reuse_object(struct sha1file *f, struct object_entry *entry,
352354
off_t offset;
353355
enum object_type type = entry->type;
354356
off_t datalen;
355-
unsigned char header[10], dheader[10];
357+
unsigned char header[MAX_PACK_OBJECT_HEADER],
358+
dheader[MAX_PACK_OBJECT_HEADER];
356359
unsigned hdrlen;
357360

358361
if (entry->delta)
359362
type = (allow_ofs_delta && entry->delta->idx.offset) ?
360363
OBJ_OFS_DELTA : OBJ_REF_DELTA;
361-
hdrlen = encode_in_pack_object_header(type, entry->size, header);
364+
hdrlen = encode_in_pack_object_header(header, sizeof(header),
365+
type, entry->size);
362366

363367
offset = entry->in_pack_offset;
364368
revidx = find_pack_revindex(p, offset);

bulk-checkin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static int stream_to_pack(struct bulk_checkin_state *state,
105105

106106
git_deflate_init(&s, pack_compression_level);
107107

108-
hdrlen = encode_in_pack_object_header(type, size, obuf);
108+
hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), type, size);
109109
s.next_out = obuf + hdrlen;
110110
s.avail_out = sizeof(obuf) - hdrlen;
111111

fast-import.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,8 @@ static int store_object(
11731173
delta_count_by_type[type]++;
11741174
e->depth = last->depth + 1;
11751175

1176-
hdrlen = encode_in_pack_object_header(OBJ_OFS_DELTA, deltalen, hdr);
1176+
hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
1177+
OBJ_OFS_DELTA, deltalen);
11771178
sha1write(pack_file, hdr, hdrlen);
11781179
pack_size += hdrlen;
11791180

@@ -1184,7 +1185,8 @@ static int store_object(
11841185
pack_size += sizeof(hdr) - pos;
11851186
} else {
11861187
e->depth = 0;
1187-
hdrlen = encode_in_pack_object_header(type, dat->len, hdr);
1188+
hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
1189+
type, dat->len);
11881190
sha1write(pack_file, hdr, hdrlen);
11891191
pack_size += hdrlen;
11901192
}
@@ -1237,9 +1239,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
12371239
sha1file_checkpoint(pack_file, &checkpoint);
12381240
offset = checkpoint.offset;
12391241

1240-
hdrlen = snprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1;
1241-
if (out_sz <= hdrlen)
1242-
die("impossibly large object header");
1242+
hdrlen = xsnprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1;
12431243

12441244
git_SHA1_Init(&c);
12451245
git_SHA1_Update(&c, out_buf, hdrlen);
@@ -1248,9 +1248,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
12481248

12491249
git_deflate_init(&s, pack_compression_level);
12501250

1251-
hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
1252-
if (out_sz <= hdrlen)
1253-
die("impossibly large object header");
1251+
hdrlen = encode_in_pack_object_header(out_buf, out_sz, OBJ_BLOB, len);
12541252

12551253
s.next_out = out_buf + hdrlen;
12561254
s.avail_out = out_sz - hdrlen;
@@ -3003,7 +3001,7 @@ static void parse_get_mark(const char *p)
30033001
if (!oe)
30043002
die("Unknown mark: %s", command_buf.buf);
30053003

3006-
snprintf(output, sizeof(output), "%s\n", sha1_to_hex(oe->idx.sha1));
3004+
xsnprintf(output, sizeof(output), "%s\n", sha1_to_hex(oe->idx.sha1));
30073005
cat_blob_write(output, 41);
30083006
}
30093007

pack-write.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ char *index_pack_lockfile(int ip_out)
304304
* - each byte afterwards: low seven bits are size continuation,
305305
* with the high bit being "size continues"
306306
*/
307-
int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
307+
int encode_in_pack_object_header(unsigned char *hdr, int hdr_len,
308+
enum object_type type, uintmax_t size)
308309
{
309310
int n = 1;
310311
unsigned char c;
@@ -315,6 +316,8 @@ int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned
315316
c = (type << 4) | (size & 15);
316317
size >>= 4;
317318
while (size) {
319+
if (n == hdr_len)
320+
die("object size is too enormous to format");
318321
*hdr++ = c | 0x80;
319322
c = size & 0x7f;
320323
size >>= 7;

pack.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ extern int verify_pack(struct packed_git *, verify_fn fn, struct progress *, uin
8484
extern off_t write_pack_header(struct sha1file *f, uint32_t);
8585
extern void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t, unsigned char *, off_t);
8686
extern char *index_pack_lockfile(int fd);
87-
extern int encode_in_pack_object_header(enum object_type, uintmax_t, unsigned char *);
87+
88+
/*
89+
* The "hdr" output buffer should be at least this big, which will handle sizes
90+
* up to 2^67.
91+
*/
92+
#define MAX_PACK_OBJECT_HEADER 10
93+
extern int encode_in_pack_object_header(unsigned char *hdr, int hdr_len,
94+
enum object_type, uintmax_t);
8895

8996
#define PH_ERROR_EOF (-1)
9097
#define PH_ERROR_PACK_SIGNATURE (-2)

0 commit comments

Comments
 (0)