]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable: avoid undefined behaviour breaking t0032
authorCarlo Marcelo Arenas Belón <carenas@gmail.com>
Fri, 15 Apr 2022 08:30:59 +0000 (01:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 15 Apr 2022 16:24:02 +0000 (09:24 -0700)
1214aa841bc (reftable: add blocksource, an abstraction for random
access reads, 2021-10-07), makes the assumption that it is ok to
free a reftable_block pointing to NULL if the size is also set to
0, but implements that using a memset call that at least in glibc
based system will trigger a runtime exception if called with a
NULL pointer as its first parameter.

Avoid doing so by adding a conditional to check for the size in all
three identically looking functions that were affected, and therefore,
still allow memset to help catch callers that might incorrectly pass
a NULL pointer with a non zero size, but avoiding the exception for
the valid cases.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
reftable/blocksource.c

index 0044eecd9aa39afa476dcfb8e6eeaaa89fb08b86..db1b7dc966f99f737565f754cfc1b45725cb0b19 100644 (file)
@@ -15,7 +15,8 @@ https://developers.google.com/open-source/licenses/bsd
 
 static void strbuf_return_block(void *b, struct reftable_block *dest)
 {
-       memset(dest->data, 0xff, dest->len);
+       if (dest->len)
+               memset(dest->data, 0xff, dest->len);
        reftable_free(dest->data);
 }
 
@@ -56,7 +57,8 @@ void block_source_from_strbuf(struct reftable_block_source *bs,
 
 static void malloc_return_block(void *b, struct reftable_block *dest)
 {
-       memset(dest->data, 0xff, dest->len);
+       if (dest->len)
+               memset(dest->data, 0xff, dest->len);
        reftable_free(dest->data);
 }
 
@@ -85,7 +87,8 @@ static uint64_t file_size(void *b)
 
 static void file_return_block(void *b, struct reftable_block *dest)
 {
-       memset(dest->data, 0xff, dest->len);
+       if (dest->len)
+               memset(dest->data, 0xff, dest->len);
        reftable_free(dest->data);
 }