Skip to content

Commit 689a195

Browse files
committed
object-file: use xmalloc*z*() for index_core()
Follow-up a1e920a (index-pack: terminate object buffers with NUL, 2014-12-08) and make sure to return a \0-terminated "buf" from index_core(). This inconsistency dates back to e83c516 (Initial revision of "git", the information manager from hell, 2005-04-07) where read_sha1_file() would return a malloc()'d "strlen(buffer) + 1". This inconsistency makes functions such as parse_tag_buffer() in tag.c harder to reason about, some codepaths that call them will hand them a data/size pair where the "data" comes from xmallocz(), whereas others (being changed here) hand them the same data/size, but that "data" comes from malloc(). We therefore have to be more paranoid about parsing the data, to ensure that we don't run off the end of "size". By using xmallocsz() we can rely on a "\0" to stop any str*() function. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]>
1 parent bc8b424 commit 689a195

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

object-file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,7 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type,
16861686
co = &cached_objects[cached_object_nr++];
16871687
co->size = len;
16881688
co->type = type;
1689-
co->buf = xmalloc(len);
1689+
co->buf = xmallocz(len);
16901690
memcpy(co->buf, buf, len);
16911691
oidcpy(&co->oid, oid);
16921692
return 0;
@@ -2433,7 +2433,7 @@ static int index_core(struct index_state *istate,
24332433
if (!size) {
24342434
ret = index_mem(istate, oid, "", size, type, path, flags);
24352435
} else if (size <= SMALL_FILE_SIZE) {
2436-
char *buf = xmalloc(size);
2436+
char *buf = xmallocz(size);
24372437
ssize_t read_result = read_in_full(fd, buf, size);
24382438
if (read_result < 0)
24392439
ret = error_errno(_("read error while indexing %s"),

0 commit comments

Comments
 (0)