Skip to content

Commit 8fbe19b

Browse files
committed
mp_obj_instance_make_new: avoid undefined behavior
If kw_args is NULL then memcpy() gets a NULL source argument. This is undefined behavior under the C standard, even if 0 bytes are being copied. This problem was found using clang 7's scan-build static analyzer.
1 parent 85f0048 commit 8fbe19b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

py/objtype.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,10 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, cons
336336
mp_obj_t *args2 = m_new(mp_obj_t, 1 + n_args + 2 * n_kw);
337337
args2[0] = MP_OBJ_FROM_PTR(self);
338338
memcpy(args2 + 1, args, n_args * sizeof(mp_obj_t));
339-
// copy in kwargs
340-
memcpy(args2 + 1 + n_args, kw_args->table, 2 * n_kw * sizeof(mp_obj_t));
339+
if (n_kw) {
340+
// copy in kwargs
341+
memcpy(args2 + 1 + n_args, kw_args->table, 2 * n_kw * sizeof(mp_obj_t));
342+
}
341343
new_ret = mp_call_function_n_kw(init_fn[0], n_args + 1, n_kw, args2);
342344
m_del(mp_obj_t, args2, 1 + n_args + 2 * n_kw);
343345
}

0 commit comments

Comments
 (0)