]> git.kaiwu.me - njs.git/commitdiff
Avoiding arithmetic ops with NULL in %TypedArray%.prototype.sort().
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 9 Jan 2024 17:14:42 +0000 (09:14 -0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 9 Jan 2024 17:14:42 +0000 (09:14 -0800)
Found by UndefinedBehaviorSanitizer.

src/njs_typed_array.c

index 7b0abc633bfb9ef04d795bf2d309b83926e18610..c483ff5245ef6e3e581a439208085ec7c404313b 100644 (file)
@@ -2035,6 +2035,11 @@ njs_typed_array_prototype_sort(njs_vm_t *vm, njs_value_t *args,
     }
 
     njs_qsort(base, length, element_size, cmp, &ctx);
+
+    if (njs_slow_path(ctx.exception)) {
+        return NJS_ERROR;
+    }
+
     if (ctx.function != NULL) {
         if (&buffer->u.u8[array->offset * element_size] == orig) {
             memcpy(orig, base, length * element_size);
@@ -2043,10 +2048,6 @@ njs_typed_array_prototype_sort(njs_vm_t *vm, njs_value_t *args,
         njs_mp_free(vm->mem_pool, base);
     }
 
-    if (njs_slow_path(ctx.exception)) {
-        return NJS_ERROR;
-    }
-
     njs_set_typed_array(retval, array);
 
     return NJS_OK;