From bc6c0b28bf830a75c817fb498b713779c92b731b Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 19 May 2018 10:52:43 -0500 Subject: [PATCH] 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. --- py/emitbc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/emitbc.c b/py/emitbc.c index 32e8330006..b1b61ba67e 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -315,7 +315,7 @@ void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) { emit->last_source_line = 1; #ifndef NDEBUG // With debugging enabled labels are checked for unique assignment - if (pass < MP_PASS_EMIT) { + if (pass < MP_PASS_EMIT && emit->label_offsets != NULL) { memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t)); } #endif