Skip to content

Commit 3a197ea

Browse files
committed
Make the tests pass cleanly with MemorySanitizer
This change: * Initializes a few variables that were being read before being initialized. * Includes madler/zlib#393. As such, it only works reliably with `-DUSE_BUNDLED_ZLIB=ON`.
1 parent d6c6285 commit 3a197ea

File tree

9 files changed

+21
-13
lines changed

9 files changed

+21
-13
lines changed

deps/zlib/deflate.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
320320

321321
s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
322322
s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
323+
memset(s->prev, 0, s->w_size * sizeof(Pos));
323324
s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
324325

325326
s->high_water = 0; /* nothing written to s->window yet */

src/diff_file.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ int git_diff_file_content__init_from_src(
160160

161161
fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
162162
} else {
163+
int error;
164+
if ((error = git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB)) < 0)
165+
return error;
163166
fc->file->size = src->buflen;
164-
git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB);
165167
fc->file->id_abbrev = GIT_OID_HEXSZ;
166168

167169
fc->map.len = src->buflen;

src/indexer.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,10 @@ static int store_object(git_indexer *idx)
427427
pentry = git__calloc(1, sizeof(struct git_pack_entry));
428428
GIT_ERROR_CHECK_ALLOC(pentry);
429429

430-
git_hash_final(&oid, &idx->hash_ctx);
430+
if (git_hash_final(&oid, &idx->hash_ctx)) {
431+
git__free(pentry);
432+
goto on_error;
433+
}
431434
entry_size = idx->off - entry_start;
432435
if (entry_start > UINT31_MAX) {
433436
entry->offset = UINT32_MAX;

src/object.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ int git_object__from_raw(
8686
GIT_ERROR_CHECK_ALLOC(object);
8787
object->cached.flags = GIT_CACHE_STORE_PARSED;
8888
object->cached.type = type;
89-
git_odb_hash(&object->cached.oid, data, size, type);
89+
if ((error = git_odb_hash(&object->cached.oid, data, size, type)) < 0)
90+
return error;
9091

9192
/* Parse raw object data */
9293
def = &git_objects_table[type];

src/odb.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,20 +1283,21 @@ int git_odb_write(
12831283
git_oid *oid, git_odb *db, const void *data, size_t len, git_object_t type)
12841284
{
12851285
size_t i;
1286-
int error = GIT_ERROR;
1286+
int error;
12871287
git_odb_stream *stream;
12881288

12891289
assert(oid && db);
12901290

1291-
git_odb_hash(oid, data, len, type);
1291+
if ((error = git_odb_hash(oid, data, len, type)) < 0)
1292+
return error;
12921293

12931294
if (git_oid_is_zero(oid))
12941295
return error_null_oid(GIT_EINVALID, "cannot write object");
12951296

12961297
if (git_odb__freshen(db, oid))
12971298
return 0;
12981299

1299-
for (i = 0; i < db->backends.length && error < 0; ++i) {
1300+
for (i = 0, error = GIT_ERROR; i < db->backends.length && error < 0; ++i) {
13001301
backend_internal *internal = git_vector_get(&db->backends, i);
13011302
git_odb_backend *b = internal->backend;
13021303

src/odb_loose.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static int read_loose_standard(git_rawobj *out, git_buf *obj)
304304
* (including the initial sequence in the head buffer).
305305
*/
306306
if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, hdr.size, 1) ||
307-
(body = git__malloc(alloc_size)) == NULL) {
307+
(body = git__calloc(1, alloc_size)) == NULL) {
308308
error = -1;
309309
goto done;
310310
}
@@ -386,8 +386,8 @@ static int read_header_loose_standard(
386386
git_rawobj *out, const unsigned char *data, size_t len)
387387
{
388388
git_zstream zs = GIT_ZSTREAM_INIT;
389-
obj_hdr hdr;
390-
unsigned char inflated[MAX_HEADER_LEN];
389+
obj_hdr hdr = {0};
390+
unsigned char inflated[MAX_HEADER_LEN] = {0};
391391
size_t header_len, inflated_len = sizeof(inflated);
392392
int error;
393393

src/regexp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
int git_regexp_compile(git_regexp *r, const char *pattern, int flags)
1313
{
1414
int erroffset, cflags = 0;
15-
const char *error;
15+
const char *error = NULL;
1616

1717
if (flags & GIT_REGEXP_ICASE)
1818
cflags |= PCRE_CASELESS;
@@ -41,7 +41,7 @@ int git_regexp_match(const git_regexp *r, const char *string)
4141

4242
int git_regexp_search(const git_regexp *r, const char *string, size_t nmatches, git_regmatch *matches)
4343
{
44-
int static_ovec[9], *ovec;
44+
int static_ovec[9] = {0}, *ovec;
4545
int error;
4646
size_t i;
4747

tests/checkout/tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ void test_checkout_tree__extremely_long_file_name(void)
917917
{
918918
/* A utf-8 string with 83 characters, but 249 bytes. */
919919
const char *longname = "\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97";
920-
char path[1024];
920+
char path[1024] = {0};
921921

922922
g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
923923
cl_git_pass(git_revparse_single(&g_object, g_repo, "long-file-name"));

tests/status/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ void test_status_worktree__sorting_by_case(void)
949949

950950
void test_status_worktree__long_filenames(void)
951951
{
952-
char path[260*4+1];
952+
char path[260*4+1] = {0};
953953
const char *expected_paths[] = {path};
954954
const unsigned int expected_statuses[] = {GIT_STATUS_WT_NEW};
955955

0 commit comments

Comments
 (0)