]> git.kaiwu.me - njs.git/commitdiff
Fixed potential undefined behavior in memcpy().
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 26 May 2020 19:02:57 +0000 (19:02 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 26 May 2020 19:02:57 +0000 (19:02 +0000)
The issue was introduced in 1d0825906438.

Found with Clang Static Analyzer.

src/njs_array.c

index baad4d36bac91b7c21b46027017588f784a83497..c1acc6b90c846ee22d8e3c44f73c028c47aa6566 100644 (file)
@@ -3318,11 +3318,13 @@ njs_array_prototype_sort(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
                     return NJS_ERROR;
                 }
 
-                p = (void *) njs_cpymem(nslots, slots,
-                                   sizeof(njs_array_sort_slot_t) * (p - slots));
-
                 if (slots != NULL) {
+                    p = (void *) njs_cpymem(nslots, slots,
+                                  sizeof(njs_array_sort_slot_t) * (p - slots));
                     njs_mp_free(vm->mem_pool, slots);
+
+                } else {
+                    p = nslots;
                 }
 
                 slots = nslots;