[njs] Added information about illegal token in number parsing.

Igor Sysoev igor at sysoev.ru
Tue Jul 3 12:48:46 UTC 2018


details:   http://hg.nginx.org/njs/rev/3aef480987d6
branches:  
changeset: 552:3aef480987d6
user:      Igor Sysoev <igor at sysoev.ru>
date:      Tue Jul 03 15:48:03 2018 +0300
description:
Added information about illegal token in number parsing.

diffstat:

 njs/njs_lexer.c          |  61 +++++++++++++++++++++++++----------------------
 njs/test/njs_unit_test.c |  26 ++++++++++----------
 2 files changed, 46 insertions(+), 41 deletions(-)

diffs (195 lines):

diff -r f20bf1b5db20 -r 3aef480987d6 njs/njs_lexer.c
--- a/njs/njs_lexer.c	Tue Jul 03 14:15:29 2018 +0300
+++ b/njs/njs_lexer.c	Tue Jul 03 15:48:03 2018 +0300
@@ -21,7 +21,7 @@ struct njs_lexer_multi_s {
 static njs_token_t njs_lexer_next_token(njs_lexer_t *lexer);
 static njs_token_t njs_lexer_word(njs_lexer_t *lexer, u_char c);
 static njs_token_t njs_lexer_string(njs_lexer_t *lexer, u_char quote);
-static njs_token_t njs_lexer_number(njs_lexer_t *lexer);
+static njs_token_t njs_lexer_number(njs_lexer_t *lexer, u_char c);
 static njs_token_t njs_lexer_multi(njs_lexer_t *lexer,
     njs_token_t token, nxt_uint_t n, const njs_lexer_multi_t *multi);
 static njs_token_t njs_lexer_division(njs_lexer_t *lexer,
@@ -316,7 +316,7 @@ njs_lexer_next_token(njs_lexer_t *lexer)
             return njs_lexer_string(lexer, c);
 
         case NJS_TOKEN_DIGIT:
-            return njs_lexer_number(lexer);
+            return njs_lexer_number(lexer, c);
 
         case NJS_TOKEN_ASSIGNMENT:
             n = nxt_nitems(njs_assignment_token),
@@ -523,12 +523,11 @@ njs_lexer_string(njs_lexer_t *lexer, u_c
 
 
 static njs_token_t
-njs_lexer_number(njs_lexer_t *lexer)
+njs_lexer_number(njs_lexer_t *lexer, u_char c)
 {
-    u_char  c, *p;
+    const u_char  *p;
 
     p = lexer->start;
-    c = p[-1];
 
     if (c == '0' && p != lexer->end) {
 
@@ -538,14 +537,12 @@ njs_lexer_number(njs_lexer_t *lexer)
             p++;
 
             if (p == lexer->end) {
-                return NJS_TOKEN_ILLEGAL;
+                goto illegal_token;
             }
 
-            lexer->start = p;
-            lexer->number = njs_number_hex_parse((const u_char **) &lexer->start,
-                                                 lexer->end);
+            lexer->number = njs_number_hex_parse(&p, lexer->end);
 
-            return NJS_TOKEN_NUMBER;
+            goto done;
         }
 
         /* Octal literal values. */
@@ -554,19 +551,16 @@ njs_lexer_number(njs_lexer_t *lexer)
             p++;
 
             if (p == lexer->end) {
-                return NJS_TOKEN_ILLEGAL;
+                goto illegal_token;
             }
 
-            lexer->start = p;
-            lexer->number = njs_number_oct_parse((const u_char **) &lexer->start,
-                                                 lexer->end);
-            p = lexer->start;
+            lexer->number = njs_number_oct_parse(&p, lexer->end);
 
             if (p < lexer->end && (*p == '8' || *p == '9')) {
-                return NJS_TOKEN_ILLEGAL;
+                goto illegal_trailer;
             }
 
-            return NJS_TOKEN_NUMBER;
+            goto done;
         }
 
         /* Binary literal values. */
@@ -575,33 +569,44 @@ njs_lexer_number(njs_lexer_t *lexer)
             p++;
 
             if (p == lexer->end) {
-                return NJS_TOKEN_ILLEGAL;
+                goto illegal_token;
             }
 
-            lexer->start = p;
-            lexer->number = njs_number_bin_parse((const u_char **) &lexer->start,
-                                                 lexer->end);
-            p = lexer->start;
+            lexer->number = njs_number_bin_parse(&p, lexer->end);
 
             if (p < lexer->end && (*p >= '2' && *p <= '9')) {
-                return NJS_TOKEN_ILLEGAL;
+                goto illegal_trailer;
             }
 
-            return NJS_TOKEN_NUMBER;
+            goto done;
         }
 
         /* Legacy Octal literals are deprecated. */
 
         if (*p >= '0' && *p <= '9') {
-            return NJS_TOKEN_ILLEGAL;
+            goto illegal_trailer;
         }
     }
 
-    lexer->start = p - 1;
-    lexer->number = njs_number_dec_parse((const u_char **) &lexer->start,
-                                         lexer->end);
+    p--;
+    lexer->number = njs_number_dec_parse(&p, lexer->end);
+
+done:
+
+    lexer->start = (u_char *) p;
 
     return NJS_TOKEN_NUMBER;
+
+illegal_trailer:
+
+    p++;
+
+illegal_token:
+
+    lexer->text.start = lexer->start - 1;
+    lexer->text.length = p - lexer->text.start;
+
+    return NJS_TOKEN_ILLEGAL;
 }
 
 
diff -r f20bf1b5db20 -r 3aef480987d6 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Jul 03 14:15:29 2018 +0300
+++ b/njs/test/njs_unit_test.c	Tue Jul 03 15:48:03 2018 +0300
@@ -127,25 +127,25 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("-0O777"),
       nxt_string("-511") },
 
+    { nxt_string("0o"),
+      nxt_string("SyntaxError: Unexpected token \"0o\" in 1") },
+
+    { nxt_string("0O778"),
+      nxt_string("SyntaxError: Unexpected token \"0O778\" in 1") },
+
     /* Legacy Octal Numbers are deprecated. */
 
     { nxt_string("00"),
-      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+      nxt_string("SyntaxError: Unexpected token \"00\" in 1") },
 
     { nxt_string("08"),
-      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+      nxt_string("SyntaxError: Unexpected token \"08\" in 1") },
 
     { nxt_string("09"),
-      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+      nxt_string("SyntaxError: Unexpected token \"09\" in 1") },
 
     { nxt_string("0011"),
-      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
-
-    { nxt_string("0o"),
-      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
-
-    { nxt_string("0O778"),
-      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+      nxt_string("SyntaxError: Unexpected token \"00\" in 1") },
 
     /* Binary Numbers. */
 
@@ -162,10 +162,10 @@ static njs_unit_test_t  njs_test[] =
       nxt_string("-255") },
 
     { nxt_string("0b"),
-      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+      nxt_string("SyntaxError: Unexpected token \"0b\" in 1") },
 
     { nxt_string("0B12"),
-      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+      nxt_string("SyntaxError: Unexpected token \"0B12\" in 1") },
 
     /* Hex Numbers. */
 
@@ -182,7 +182,7 @@ static njs_unit_test_t  njs_test[] =
       nxt_string("48879") },
 
     { nxt_string("0x"),
-      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+      nxt_string("SyntaxError: Unexpected token \"0x\" in 1") },
 
     { nxt_string("0xffff."),
       nxt_string("SyntaxError: Unexpected token \"\" in 1") },


More information about the nginx-devel mailing list