chg: use calloc

This commit is contained in:
iceman1001 2019-01-30 21:53:10 +01:00
parent ad72a424ef
commit 5c231e772c

View file

@ -38,7 +38,7 @@ static int build_segs_from_phdrs(flash_file_t *ctx, FILE *fd, Elf32_Phdr *phdrs,
flash_seg_t *seg; flash_seg_t *seg;
uint32_t last_end = 0; uint32_t last_end = 0;
ctx->segments = malloc(sizeof(flash_seg_t) * num_phdrs); ctx->segments = calloc(sizeof(flash_seg_t) * num_phdrs, sizeof(uint8_t));
if (!ctx->segments) { if (!ctx->segments) {
fprintf(stderr, "Out of memory\n"); fprintf(stderr, "Out of memory\n");
return -1; return -1;
@ -88,7 +88,7 @@ static int build_segs_from_phdrs(flash_file_t *ctx, FILE *fd, Elf32_Phdr *phdrs,
uint8_t *data; uint8_t *data;
// make extra space if we need to move the data forward // make extra space if we need to move the data forward
data = malloc(filesz + BLOCK_SIZE); data = calloc(filesz + BLOCK_SIZE, sizeof(uint8_t));
if (!data) { if (!data) {
fprintf(stderr, "Error: Out of memory\n"); fprintf(stderr, "Error: Out of memory\n");
return -1; return -1;
@ -111,7 +111,7 @@ static int build_segs_from_phdrs(flash_file_t *ctx, FILE *fd, Elf32_Phdr *phdrs,
uint32_t new_length = this_end - prev_seg->start; uint32_t new_length = this_end - prev_seg->start;
uint32_t this_offset = paddr - prev_seg->start; uint32_t this_offset = paddr - prev_seg->start;
uint32_t hole = this_offset - prev_seg->length; uint32_t hole = this_offset - prev_seg->length;
uint8_t *new_data = malloc(new_length); uint8_t *new_data = calloc(new_length, sizeof(uint8_t));
if (!new_data) { if (!new_data) {
fprintf(stderr, "Error: Out of memory\n"); fprintf(stderr, "Error: Out of memory\n");
free(data); free(data);
@ -223,7 +223,7 @@ int flash_load(flash_file_t *ctx, const char *name, int can_write_bl) {
} }
num_phdrs = le16(ehdr.e_phnum); num_phdrs = le16(ehdr.e_phnum);
phdrs = malloc(le16(ehdr.e_phnum) * sizeof(Elf32_Phdr)); phdrs = calloc(le16(ehdr.e_phnum) * sizeof(Elf32_Phdr), sizeof(uint8_t));
if (!phdrs) { if (!phdrs) {
fprintf(stderr, "Out of memory\n"); fprintf(stderr, "Out of memory\n");
goto fail; goto fail;