Replace several uses of malloc with calloc

This commit is contained in:
Nathan Fisher 2023-08-14 23:42:09 -04:00
parent b0c2c0c8ac
commit bd820638b5
2 changed files with 5 additions and 4 deletions

1
.gitignore vendored
View file

@ -1,6 +1,7 @@
haggis haggis
config.mk config.mk
test/output/ test/output/
test/
*.o *.o
*.a *.a
*.so *.so

View file

@ -315,7 +315,7 @@ int haggis_file_init(char *path, haggis_file *hf, haggis_algorithm a) {
} }
hf->len.val = (uint64_t)len; hf->len.val = (uint64_t)len;
rewind(f); rewind(f);
hf->data = malloc((size_t)len); hf->data = calloc(1, (size_t)len);
if (fread(hf->data, 1, (size_t)len, f) != (size_t)len) { if (fread(hf->data, 1, (size_t)len, f) != (size_t)len) {
free(hf->data); free(hf->data);
fclose(f); fclose(f);
@ -346,7 +346,7 @@ int haggis_load_file(FILE *stream, haggis_file *f) {
return 1; return 1;
if (haggis_load_cksum(stream, &f->cksum) != 0) if (haggis_load_cksum(stream, &f->cksum) != 0)
return 1; return 1;
f->data = malloc((size_t)f->len.val); f->data = calloc(1, (size_t)f->len.val);
if (f->data == NULL) if (f->data == NULL)
return -1; return -1;
int res = fread(f->data, 1, (size_t)f->len.val, stream); int res = fread(f->data, 1, (size_t)f->len.val, stream);
@ -382,7 +382,7 @@ int haggis_load_filename(FILE *stream, haggis_filename *n) {
len.val = 0; len.val = 0;
if (fread(len.bytes, 1, 2, stream) != 2) return 2; if (fread(len.bytes, 1, 2, stream) != 2) return 2;
n->len = len; n->len = len;
name = malloc((size_t)len.val); name = calloc(1, (size_t)len.val);
if (name == NULL) return -1; if (name == NULL) return -1;
if (fread(name, 1, (size_t)len.val, stream) != (size_t)len.val) { if (fread(name, 1, (size_t)len.val, stream) != (size_t)len.val) {
free(name); free(name);
@ -528,7 +528,7 @@ haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_hardlink_
int res; int res;
haggis_node *node; haggis_node *node;
node = malloc(sizeof(haggis_node)); node = calloc(1, sizeof(haggis_node));
if (node == NULL) return NULL; if (node == NULL) return NULL;
if (stat(file, st) != 0) { if (stat(file, st) != 0) {
free(node); free(node);