[njs] Fixed large non-decimal literals.
Dmitry Volyntsev
xeioex at nginx.com
Thu Jun 23 06:40:54 UTC 2022
details: https://hg.nginx.org/njs/rev/33b1637aad33
branches:
changeset: 1897:33b1637aad33
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Wed Jun 22 23:37:16 2022 -0700
description:
Fixed large non-decimal literals.
This closes #551 issue on Github.
diffstat:
src/njs_number.c | 27 +++++++++++----------------
src/njs_number.h | 8 +++-----
src/test/njs_unit_test.c | 21 +++++++++++++++++++++
3 files changed, 35 insertions(+), 21 deletions(-)
diffs (163 lines):
diff -r 9f544b8e28c5 -r 33b1637aad33 src/njs_number.c
--- a/src/njs_number.c Wed Jun 22 23:37:15 2022 -0700
+++ b/src/njs_number.c Wed Jun 22 23:37:16 2022 -0700
@@ -61,11 +61,11 @@ njs_number_dec_parse(const u_char **star
}
-uint64_t
+double
njs_number_oct_parse(const u_char **start, const u_char *end)
{
u_char c;
- uint64_t num;
+ double num;
const u_char *p, *_;
p = *start;
@@ -95,11 +95,11 @@ njs_number_oct_parse(const u_char **star
}
-uint64_t
+double
njs_number_bin_parse(const u_char **start, const u_char *end)
{
u_char c;
- uint64_t num;
+ double num;
const u_char *p, *_;
p = *start;
@@ -129,11 +129,11 @@ njs_number_bin_parse(const u_char **star
}
-uint64_t
+double
njs_number_hex_parse(const u_char **start, const u_char *end,
njs_bool_t literal)
{
- uint64_t num;
+ double num;
njs_int_t n;
const u_char *p, *_;
@@ -163,12 +163,11 @@ njs_number_hex_parse(const u_char **star
}
-int64_t
+static double
njs_number_radix_parse(const u_char **start, const u_char *end, uint8_t radix)
{
uint8_t d;
- int64_t num;
- uint64_t n;
+ double num, n;
const u_char *p;
static const int8_t digits[256]
@@ -192,7 +191,7 @@ njs_number_radix_parse(const u_char **st
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
- num = -1;
+ num = NAN;
n = 0;
for (p = *start; p < end; p++) {
@@ -1066,7 +1065,6 @@ njs_number_parse_int(njs_vm_t *vm, njs_v
njs_index_t unused)
{
double num;
- int64_t n;
int32_t radix;
njs_int_t ret;
njs_str_t string;
@@ -1142,12 +1140,9 @@ found:
radix = 16;
}
- n = njs_number_radix_parse(&p, end, radix);
+ num = njs_number_radix_parse(&p, end, radix);
- if (n >= 0) {
- num = n;
- num = minus ? -num : num;
- }
+ num = minus ? -num : num;
done:
diff -r 9f544b8e28c5 -r 33b1637aad33 src/njs_number.h
--- a/src/njs_number.h Wed Jun 22 23:37:15 2022 -0700
+++ b/src/njs_number.h Wed Jun 22 23:37:16 2022 -0700
@@ -16,12 +16,10 @@
double njs_key_to_index(const njs_value_t *value);
double njs_number_dec_parse(const u_char **start, const u_char *end,
njs_bool_t literal);
-uint64_t njs_number_oct_parse(const u_char **start, const u_char *end);
-uint64_t njs_number_bin_parse(const u_char **start, const u_char *end);
-uint64_t njs_number_hex_parse(const u_char **start, const u_char *end,
+double njs_number_oct_parse(const u_char **start, const u_char *end);
+double njs_number_bin_parse(const u_char **start, const u_char *end);
+double njs_number_hex_parse(const u_char **start, const u_char *end,
njs_bool_t literal);
-int64_t njs_number_radix_parse(const u_char **start, const u_char *end,
- uint8_t radix);
njs_int_t njs_number_to_string(njs_vm_t *vm, njs_value_t *string,
const njs_value_t *number);
njs_int_t njs_number_to_chain(njs_vm_t *vm, njs_chb_t *chain,
diff -r 9f544b8e28c5 -r 33b1637aad33 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Wed Jun 22 23:37:15 2022 -0700
+++ b/src/test/njs_unit_test.c Wed Jun 22 23:37:16 2022 -0700
@@ -338,6 +338,9 @@ static njs_unit_test_t njs_test[] =
{ njs_str("-0O7_7_7"),
njs_str("-511") },
+ { njs_str("0o7777777777777777777777777777777777700000000000000000000000000000000"),
+ njs_str("3.2138760885179806e+60") },
+
{ njs_str("0o"),
njs_str("SyntaxError: Unexpected token \"0o\" in 1") },
@@ -390,6 +393,9 @@ static njs_unit_test_t njs_test[] =
{ njs_str("-0B1111_1111"),
njs_str("-255") },
+ { njs_str("0b111111111111111111111111111111111111111111111111111111111111111111111111111110000000000000"),
+ njs_str("1.2379400392853803e+27") },
+
{ njs_str("0b"),
njs_str("SyntaxError: Unexpected token \"0b\" in 1") },
@@ -419,6 +425,12 @@ static njs_unit_test_t njs_test[] =
{ njs_str("0X00_00_BE_EF"),
njs_str("48879") },
+ { njs_str("0x21bc2b266d3a3600000000000000000000000000000000000000000000000000000"),
+ njs_str("6.25e+79") },
+
+ { njs_str("0x21bc2b266d3a36000000000000000000000000000000000000000000000000000000"),
+ njs_str("1e+81") },
+
{ njs_str("0x"),
njs_str("SyntaxError: Unexpected token \"0x\" in 1") },
@@ -16735,6 +16747,15 @@ static njs_unit_test_t njs_test[] =
{ njs_str("parseInt('1010', 2)"),
njs_str("10") },
+ { njs_str("parseInt('111111111111111111111111111111111111111111111111111111111111111111111111111110000000000000', 2)"),
+ njs_str("1.2379400392853803e+27") },
+
+ { njs_str("parseInt('7777777777777777777777777777777777700000000000000000000000000000000', 8)"),
+ njs_str("3.2138760885179806e+60") },
+
+ { njs_str("parseInt('0x21bc2b266d3a3600000000000000000000000000000000000000000000000000000')"),
+ njs_str("6.25e+79") },
+
{ njs_str("parseInt('aBc', 16)"),
njs_str("2748") },
More information about the nginx-devel
mailing list