summaryrefslogtreecommitdiff
path: root/py/objtype.c
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2019-10-08 10:48:25 +0900
committerJeff Epler <jepler@gmail.com>2019-10-08 11:31:06 +0900
commit8fbe19b99335da7c9a17698908f709b02901d8fc (patch)
treed676da577f7320b602d04be62626605714bcb563 /py/objtype.c
parent85f0048d22a31f34d991e56757560f1b39c007b6 (diff)
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.
Diffstat (limited to 'py/objtype.c')
-rw-r--r--py/objtype.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/py/objtype.c b/py/objtype.c
index 5133d849f..52b38f013 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -336,8 +336,10 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, cons
mp_obj_t *args2 = m_new(mp_obj_t, 1 + n_args + 2 * n_kw);
args2[0] = MP_OBJ_FROM_PTR(self);
memcpy(args2 + 1, args, n_args * sizeof(mp_obj_t));
- // copy in kwargs
- memcpy(args2 + 1 + n_args, kw_args->table, 2 * n_kw * sizeof(mp_obj_t));
+ if (n_kw) {
+ // copy in kwargs
+ memcpy(args2 + 1 + n_args, kw_args->table, 2 * n_kw * sizeof(mp_obj_t));
+ }
new_ret = mp_call_function_n_kw(init_fn[0], n_args + 1, n_kw, args2);
m_del(mp_obj_t, args2, 1 + n_args + 2 * n_kw);
}