Skip to content

Commit

Permalink
Passing NULL to memcpy is UB.
Browse files Browse the repository at this point in the history
UBSan warns when passing a NULL as the src
argument to memcpy. Avoid doing that.
  • Loading branch information
amcn committed Apr 20, 2023
1 parent d5e09c1 commit bcc1c3e
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/compiler/codegen_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,13 @@ int fb_copy_scope(fb_scope_t *scope, char *buf)
buf[0] = '\0';
return -1;
}

len = (size_t)scope->prefix.len;
memcpy(buf, scope->prefix.s, len);
if (scope->prefix.s != NULL) {
/* Passing NULL as the 2nd argument to memcpy yields undefined behaviour. */
memcpy(buf, scope->prefix.s, len);
}

for (name = scope->name; name; name = name->link) {
n = (size_t)name->ident->len;
memcpy(buf + len, name->ident->text, n);
Expand Down

0 comments on commit bcc1c3e

Please sign in to comment.