Skip to content

Commit 97f98e9

Browse files
authored
Merge pull request #307 from ChinYikMing/memory_alloc_check
Consolidate memory allocation
2 parents 48ed780 + 9193105 commit 97f98e9

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

src/cache.c

+12
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,30 @@ cache_t *cache_create(int size_bits)
195195
cache_size = 1 << size_bits;
196196
for (int i = 0; i < THRESHOLD; i++) {
197197
cache->lists[i] = malloc(sizeof(struct list_head));
198+
if (!cache->lists[i]) {
199+
for (int j = 0; j < i; j++) {
200+
free(cache->lists[j]);
201+
}
202+
return NULL;
203+
}
198204
INIT_LIST_HEAD(cache->lists[i]);
199205
}
200206

201207
cache->map = malloc(sizeof(hashtable_t));
202208
if (!cache->map) {
209+
for (int i = 0; i < THRESHOLD; i++) {
210+
free(cache->lists[i]);
211+
}
203212
free(cache->lists);
204213
free(cache);
205214
return NULL;
206215
}
207216
cache->map->ht_list_head = malloc(cache_size * sizeof(struct hlist_head));
208217
if (!cache->map->ht_list_head) {
209218
free(cache->map);
219+
for (int i = 0; i < THRESHOLD; i++) {
220+
free(cache->lists[i]);
221+
}
210222
free(cache->lists);
211223
free(cache);
212224
return NULL;

src/elf.c

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* "LICENSE" for information on usage and redistribution of this file.
44
*/
55

6+
#include <assert.h>
67
#include <stdlib.h>
78
#include <string.h>
89

@@ -70,6 +71,7 @@ struct elf_internal {
7071
elf_t *elf_new(void)
7172
{
7273
elf_t *e = malloc(sizeof(elf_t));
74+
assert(e);
7375
e->hdr = NULL;
7476
e->raw_size = 0;
7577
e->symbols = map_init(int, char *, map_cmp_uint);
@@ -342,6 +344,7 @@ bool elf_open(elf_t *e, const char *input)
342344
/* allocate memory */
343345
free(e->raw_data);
344346
e->raw_data = malloc(e->raw_size);
347+
assert(e->raw_data);
345348

346349
/* read data into memory */
347350
const size_t r = fread(e->raw_data, 1, e->raw_size, f);

src/emulate.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,10 @@ static void block_translate(riscv_t *rv, block_t *block)
632632
#if RV32_HAS(EXT_C)
633633
|| ir->opcode == rv_insn_cjalr || ir->opcode == rv_insn_cjr
634634
#endif
635-
)
635+
) {
636636
ir->branch_table = calloc(1, sizeof(branch_history_table_t));
637+
assert(ir->branch_table);
638+
}
637639
break;
638640
}
639641

@@ -659,6 +661,7 @@ static void block_translate(riscv_t *rv, block_t *block)
659661
if (count > 1) { \
660662
ir->opcode = IIF(RW)(rv_insn_fuse4, rv_insn_fuse3); \
661663
ir->fuse = malloc(count * sizeof(opcode_fuse_t)); \
664+
assert(ir->fuse); \
662665
ir->imm2 = count; \
663666
memcpy(ir->fuse, ir, sizeof(opcode_fuse_t)); \
664667
ir->impl = dispatch_table[ir->opcode]; \
@@ -849,6 +852,7 @@ static void match_pattern(riscv_t *rv, block_t *block)
849852
if (count > 1) {
850853
ir->opcode = rv_insn_fuse1;
851854
ir->fuse = malloc(count * sizeof(opcode_fuse_t));
855+
assert(ir->fuse);
852856
ir->imm2 = count;
853857
memcpy(ir->fuse, ir, sizeof(opcode_fuse_t));
854858
ir->impl = dispatch_table[ir->opcode];
@@ -887,6 +891,7 @@ static void match_pattern(riscv_t *rv, block_t *block)
887891
}
888892
if (count > 1) {
889893
ir->fuse = malloc(count * sizeof(opcode_fuse_t));
894+
assert(ir->fuse);
890895
memcpy(ir->fuse, ir, sizeof(opcode_fuse_t));
891896
ir->opcode = rv_insn_fuse7;
892897
ir->imm2 = count;

src/mpool.c

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ static void *mpool_extend(mpool_t *mp)
8282
if (!p)
8383
return NULL;
8484
area_t *new_area = malloc(sizeof(area_t));
85+
if (!new_area)
86+
return NULL;
8587
new_area->mapped = p;
8688
new_area->next = NULL;
8789
size_t chunk_count = pool_size / (sizeof(memchunk_t) + mp->chunk_size);

src/riscv.c

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static void block_map_init(block_map_t *map, const uint8_t bits)
2727
map->block_capacity = 1 << bits;
2828
map->size = 0;
2929
map->map = calloc(map->block_capacity, sizeof(struct block *));
30+
assert(map->map);
3031
}
3132

3233
/* clear all block in the block map */
@@ -113,6 +114,7 @@ riscv_t *rv_create(const riscv_io_t *io,
113114
assert(io);
114115

115116
riscv_t *rv = calloc(1, sizeof(riscv_t));
117+
assert(rv);
116118

117119
/* copy over the IO interface */
118120
memcpy(&rv->io, io, sizeof(riscv_io_t));

0 commit comments

Comments
 (0)