[njs] Fixed maybe-uninitialized warning in error creation.

noreply at nginx.com noreply at nginx.com
Sat Jun 29 04:57:03 UTC 2024


details:   https://hg.nginx.org/njs/rev/9f59e7e13899
branches:  
changeset: 2365:9f59e7e13899
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Jun 20 17:11:24 2024 -0700
description:
Fixed maybe-uninitialized warning in error creation.

Ensuring that buf is always initialized in njs_throw_error_va()
and njs_error_fmt_new(), by requiring fmt to always be non NULL.

This fixes GCC warnings like:
169 | njs_unicode_decode_t ctx;
| ^
In function ‘njs_utf8_length’,
   inlined from ‘njs_error_new’ at src/njs_error.c:39:14,
   inlined from ‘njs_throw_error_va’ at src/njs_error.c:69:5:
   src/njs_utf8.h:141:12: error: ‘buf’ may be used uninitialized
   [-Werror=maybe-uninitialized]
   141 | return njs_utf8_stream_length(&ctx, p, len, 1, 1, NULL);

diffstat:

 src/njs_error.c          |  16 ++++------------
 src/njs_number.c         |   2 +-
 src/njs_string.c         |   8 ++++----
 src/test/njs_unit_test.c |  40 ++++++++++++++++++++--------------------
 4 files changed, 29 insertions(+), 37 deletions(-)

diffs (215 lines):

diff -r 818ab94037d7 -r 9f59e7e13899 src/njs_error.c
--- a/src/njs_error.c	Tue Jun 18 23:47:31 2024 -0700
+++ b/src/njs_error.c	Thu Jun 20 17:11:24 2024 -0700
@@ -60,11 +60,7 @@ njs_throw_error_va(njs_vm_t *vm, njs_obj
 {
     u_char   buf[NJS_MAX_ERROR_STR], *p;
 
-    p = buf;
-
-    if (fmt != NULL) {
-        p = njs_vsprintf(buf, buf + sizeof(buf), fmt, args);
-    }
+    p = njs_vsprintf(buf, buf + sizeof(buf), fmt, args);
 
     njs_error_new(vm, &vm->exception, proto, buf, p - buf);
 }
@@ -88,13 +84,9 @@ njs_error_fmt_new(njs_vm_t *vm, njs_valu
     va_list  args;
     u_char   buf[NJS_MAX_ERROR_STR], *p;
 
-    p = buf;
-
-    if (fmt != NULL) {
-        va_start(args, fmt);
-        p = njs_vsprintf(buf, buf + sizeof(buf), fmt, args);
-        va_end(args);
-    }
+    va_start(args, fmt);
+    p = njs_vsprintf(buf, buf + sizeof(buf), fmt, args);
+    va_end(args);
 
     njs_error_new(vm, dst, njs_vm_proto(vm, type), buf, p - buf);
 }
diff -r 818ab94037d7 -r 9f59e7e13899 src/njs_number.c
--- a/src/njs_number.c	Tue Jun 18 23:47:31 2024 -0700
+++ b/src/njs_number.c	Thu Jun 20 17:11:24 2024 -0700
@@ -543,7 +543,7 @@ njs_number_prototype_to_string(njs_vm_t 
         }
 
         if (radix < 2 || radix > 36 || radix != (int) radix) {
-            njs_range_error(vm, NULL);
+            njs_range_error(vm, "radix argument must be between 2 and 36");
             return NJS_ERROR;
         }
 
diff -r 818ab94037d7 -r 9f59e7e13899 src/njs_string.c
--- a/src/njs_string.c	Tue Jun 18 23:47:31 2024 -0700
+++ b/src/njs_string.c	Thu Jun 20 17:11:24 2024 -0700
@@ -1695,7 +1695,7 @@ njs_string_from_char_code(njs_vm_t *vm, 
 
 range_error:
 
-    njs_range_error(vm, NULL);
+    njs_range_error(vm, "invalid code point");
 
     return NJS_ERROR;
 }
@@ -2503,7 +2503,7 @@ njs_string_prototype_repeat(njs_vm_t *vm
     }
 
     if (njs_slow_path(n < 0 || n == INT64_MAX)) {
-        njs_range_error(vm, NULL);
+        njs_range_error(vm, "invalid count value");
         return NJS_ERROR;
     }
 
@@ -2517,7 +2517,7 @@ njs_string_prototype_repeat(njs_vm_t *vm
     max = NJS_STRING_MAX_LENGTH / string.size;
 
     if (njs_slow_path(n >= max)) {
-        njs_range_error(vm, NULL);
+        njs_range_error(vm, "invalid string length");
         return NJS_ERROR;
     }
 
@@ -2583,7 +2583,7 @@ njs_string_prototype_pad(njs_vm_t *vm, n
     }
 
     if (njs_slow_path(new_length >= NJS_STRING_MAX_LENGTH)) {
-        njs_range_error(vm, NULL);
+        njs_range_error(vm, "invalid string length");
         return NJS_ERROR;
     }
 
diff -r 818ab94037d7 -r 9f59e7e13899 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Tue Jun 18 23:47:31 2024 -0700
+++ b/src/test/njs_unit_test.c	Thu Jun 20 17:11:24 2024 -0700
@@ -630,13 +630,13 @@ static njs_unit_test_t  njs_test[] =
       njs_str("Infinity") },
 
     { njs_str("Infinity.toString(NaN)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: radix argument must be between 2 and 36") },
 
     { njs_str("Infinity.toString({})"),
-      njs_str("RangeError") },
+      njs_str("RangeError: radix argument must be between 2 and 36") },
 
     { njs_str("Infinity.toString(Infinity)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: radix argument must be between 2 and 36") },
 
     { njs_str("NaN.toString()"),
       njs_str("NaN") },
@@ -648,13 +648,13 @@ static njs_unit_test_t  njs_test[] =
       njs_str("NaN") },
 
     { njs_str("NaN.toString(Infinity)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: radix argument must be between 2 and 36") },
 
     { njs_str("NaN.toString({})"),
-      njs_str("RangeError") },
+      njs_str("RangeError: radix argument must be between 2 and 36") },
 
     { njs_str("NaN.toString(NaN)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: radix argument must be between 2 and 36") },
 
     { njs_str("1.2312313132.toString(14)"),
       njs_str("1.3346da6d5d455c") },
@@ -8383,13 +8383,13 @@ static njs_unit_test_t  njs_test[] =
       njs_str("0") },
 
     { njs_str("String.fromCodePoint('_')"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid code point") },
 
     { njs_str("String.fromCharCode(65.14)"),
       njs_str("A") },
 
     { njs_str("String.fromCodePoint(3.14)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid code point") },
 
     { njs_str("String.fromCharCode(65.14 + 65536)"),
       njs_str("A") },
@@ -8428,7 +8428,7 @@ static njs_unit_test_t  njs_test[] =
       njs_str("\n") },
 
     { njs_str("String.fromCodePoint(1114111 + 1)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid code point") },
 
     { njs_str("String.fromCharCode(65, 90) + String.fromCodePoint(65, 90)"),
       njs_str("AZAZ") },
@@ -9809,22 +9809,22 @@ static njs_unit_test_t  njs_test[] =
       njs_str("") },
 
     { njs_str("'abc'.repeat(Infinity)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid count value") },
 
     { njs_str("'abc'.repeat(-1)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid count value") },
 
     { njs_str("''.repeat(-1)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid count value") },
 
     { njs_str("'a'.repeat(2147483647)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid string length") },
 
     { njs_str("'a'.repeat(2147483648)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid string length") },
 
     { njs_str("'a'.repeat(Infinity)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid count value") },
 
     { njs_str("'a'.repeat(NaN)"),
       njs_str("") },
@@ -9839,10 +9839,10 @@ static njs_unit_test_t  njs_test[] =
       njs_str("") },
 
     { njs_str("'aaaaaaaa'.repeat(2**64+1)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid count value") },
 
     { njs_str("''.repeat(Infinity)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid count value") },
 
     { njs_str("''.repeat(NaN)"),
       njs_str("") },
@@ -9866,7 +9866,7 @@ static njs_unit_test_t  njs_test[] =
       njs_str("abc") },
 
     { njs_str("'abc'.padStart(2147483647)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid string length") },
 
     { njs_str("'abc'.padStart(2147483646, '')"),
       njs_str("abc") },
@@ -9920,7 +9920,7 @@ static njs_unit_test_t  njs_test[] =
       njs_str("я     ") },
 
     { njs_str("'я'.padEnd(2147483647)"),
-      njs_str("RangeError") },
+      njs_str("RangeError: invalid string length") },
 
     { njs_str("'я'.padEnd(2147483646, '')"),
       njs_str("я") },
@@ -23050,7 +23050,7 @@ static njs_unit_test_t  njs_backtraces_t
               "    at main (:1)\n") },
 
     { njs_str("''.repeat(-1)"),
-      njs_str("RangeError\n"
+      njs_str("RangeError: invalid count value\n"
               "    at String.prototype.repeat (native)\n"
               "    at main (:1)\n") },
 


More information about the nginx-devel mailing list