Skip to content

Commit

Permalink
Fixed maybe-uninitialized warning in error creation.
Browse files Browse the repository at this point in the history
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);
  • Loading branch information
xeioex committed Jun 29, 2024
1 parent 184d2a3 commit ed631d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 37 deletions.
16 changes: 4 additions & 12 deletions src/njs_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ njs_throw_error_va(njs_vm_t *vm, njs_object_t *proto, const char *fmt,
{
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);
}
Expand All @@ -88,13 +84,9 @@ njs_error_fmt_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type,
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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/njs_number.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ njs_number_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
}

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;
}

Expand Down
8 changes: 4 additions & 4 deletions src/njs_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1695,7 +1695,7 @@ njs_string_from_char_code(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,

range_error:

njs_range_error(vm, NULL);
njs_range_error(vm, "invalid code point");

return NJS_ERROR;
}
Expand Down Expand Up @@ -2503,7 +2503,7 @@ njs_string_prototype_repeat(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
}

if (njs_slow_path(n < 0 || n == INT64_MAX)) {
njs_range_error(vm, NULL);
njs_range_error(vm, "invalid count value");
return NJS_ERROR;
}

Expand All @@ -2517,7 +2517,7 @@ njs_string_prototype_repeat(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
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;
}

Expand Down Expand Up @@ -2583,7 +2583,7 @@ njs_string_prototype_pad(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
}

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;
}

Expand Down
40 changes: 20 additions & 20 deletions src/test/njs_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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") },
Expand All @@ -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") },
Expand Down Expand Up @@ -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") },
Expand Down Expand Up @@ -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") },
Expand Down Expand Up @@ -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("") },
Expand All @@ -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("") },
Expand All @@ -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") },
Expand Down Expand Up @@ -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("я") },
Expand Down Expand Up @@ -23050,7 +23050,7 @@ static njs_unit_test_t njs_backtraces_test[] =
" 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") },

Expand Down

0 comments on commit ed631d2

Please sign in to comment.