Skip to content

Commit 7ab3ebc

Browse files
committed
Fix a crash when a module has no line information
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent db7fa16 commit 7ab3ebc

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/libAtomVM/module.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,21 @@ Module *module_new_from_iff_binary(GlobalContext *global, const void *iff_binary
323323
num_offsets++;
324324
item = item->next;
325325
}
326-
mod->line_refs_offsets = malloc(num_offsets * sizeof(unsigned int));
327-
if (IS_NULL_PTR(mod->line_refs_offsets)) {
328-
fprintf(stderr, "Warning: Unable to allocate space for line refs offset, module has %zu offsets. Line information in stacktraces may be missing\n", num_offsets);
329-
} else {
330-
size_t index = 0;
331-
item = line_refs.next;
332-
while (item != &line_refs) {
333-
struct LineRefOffset *offset = CONTAINER_OF(item, struct LineRefOffset, head);
334-
mod->line_refs_offsets[index] = offset->offset;
335-
index++;
336-
item = item->next;
326+
if (num_offsets > 0) {
327+
mod->line_refs_offsets = malloc(num_offsets * sizeof(unsigned int));
328+
if (IS_NULL_PTR(mod->line_refs_offsets)) {
329+
fprintf(stderr, "Warning: Unable to allocate space for line refs offset, module has %zu offsets. Line information in stacktraces may be missing\n", num_offsets);
330+
} else {
331+
size_t index = 0;
332+
item = line_refs.next;
333+
while (item != &line_refs) {
334+
struct LineRefOffset *offset = CONTAINER_OF(item, struct LineRefOffset, head);
335+
mod->line_refs_offsets[index] = offset->offset;
336+
index++;
337+
item = item->next;
338+
}
339+
mod->line_refs_offsets_count = num_offsets;
337340
}
338-
mod->line_refs_offsets_count = num_offsets;
339341
}
340342
}
341343
// Empty the list

0 commit comments

Comments
 (0)