Skip to content

Commit c329898

Browse files
committed
Merge branch 'il/maint-xmallocz' into maint
* il/maint-xmallocz: Fix integer overflow in unpack_compressed_entry() Fix integer overflow in unpack_sha1_rest() Fix integer overflow in patch_delta() Add xmallocz()
2 parents b0e67ff + 4ab07e4 commit c329898

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ extern void release_pack_memory(size_t, int);
345345

346346
extern char *xstrdup(const char *str);
347347
extern void *xmalloc(size_t size);
348+
extern void *xmallocz(size_t size);
348349
extern void *xmemdupz(const void *data, size_t len);
349350
extern char *xstrndup(const char *str, size_t len);
350351
extern void *xrealloc(void *ptr, size_t size);

patch-delta.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ void *patch_delta(const void *src_buf, unsigned long src_size,
3333

3434
/* now the result size */
3535
size = get_delta_hdr_size(&data, top);
36-
dst_buf = xmalloc(size + 1);
37-
dst_buf[size] = 0;
36+
dst_buf = xmallocz(size);
3837

3938
out = dst_buf;
4039
while (data < top) {

sha1_file.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
12321232
static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
12331233
{
12341234
int bytes = strlen(buffer) + 1;
1235-
unsigned char *buf = xmalloc(1+size);
1235+
unsigned char *buf = xmallocz(size);
12361236
unsigned long n;
12371237
int status = Z_OK;
12381238

@@ -1260,7 +1260,6 @@ static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size
12601260
while (status == Z_OK)
12611261
status = git_inflate(stream, Z_FINISH);
12621262
}
1263-
buf[size] = 0;
12641263
if (status == Z_STREAM_END && !stream->avail_in) {
12651264
git_inflate_end(stream);
12661265
return buf;
@@ -1583,8 +1582,7 @@ static void *unpack_compressed_entry(struct packed_git *p,
15831582
z_stream stream;
15841583
unsigned char *buffer, *in;
15851584

1586-
buffer = xmalloc(size + 1);
1587-
buffer[size] = 0;
1585+
buffer = xmallocz(size);
15881586
memset(&stream, 0, sizeof(stream));
15891587
stream.next_out = buffer;
15901588
stream.avail_out = size + 1;

wrapper.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ void *xmalloc(size_t size)
3434
return ret;
3535
}
3636

37+
void *xmallocz(size_t size)
38+
{
39+
void *ret;
40+
if (size + 1 < size)
41+
die("Data too large to fit into virtual memory space.");
42+
ret = xmalloc(size + 1);
43+
((char*)ret)[size] = 0;
44+
return ret;
45+
}
46+
3747
/*
3848
* xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
3949
* "data" to the allocated memory, zero terminates the allocated memory,
@@ -42,10 +52,7 @@ void *xmalloc(size_t size)
4252
*/
4353
void *xmemdupz(const void *data, size_t len)
4454
{
45-
char *p = xmalloc(len + 1);
46-
memcpy(p, data, len);
47-
p[len] = '\0';
48-
return p;
55+
return memcpy(xmallocz(len), data, len);
4956
}
5057

5158
char *xstrndup(const char *str, size_t len)

0 commit comments

Comments
 (0)