Skip to content

Commit bc6c0b2

Browse files
jeplerdpgeorge
authored andcommitted
py/emitbc: Avoid undefined behavior calling memset() with NULL 1st arg.
Calling memset(NULL, value, 0) is not standards compliant so we must add an explicit check that emit->label_offsets is indeed not NULL before calling memset (this pointer will be NULL on the first pass of the parse tree and it's more logical / safer to check this pointer rather than check that the pass is not the first one). Code sanitizers will warn if NULL is passed as the first value to memset, and compilers may optimise the code based on the knowledge that any pointer passed to memset is guaranteed not to be NULL.
1 parent afd0701 commit bc6c0b2

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

py/emitbc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
315315
emit->last_source_line = 1;
316316
#ifndef NDEBUG
317317
// With debugging enabled labels are checked for unique assignment
318-
if (pass < MP_PASS_EMIT) {
318+
if (pass < MP_PASS_EMIT && emit->label_offsets != NULL) {
319319
memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
320320
}
321321
#endif

0 commit comments

Comments
 (0)