[njs] Replacing vsprintf with nxt_vsprintf in exceptions.

Dmitry Volyntsev xeioex at nginx.com
Wed Jan 30 15:56:39 UTC 2019


details:   https://hg.nginx.org/njs/rev/dbffb3031c77
branches:  
changeset: 746:dbffb3031c77
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Jan 30 18:49:33 2019 +0300
description:
Replacing vsprintf with nxt_vsprintf in exceptions.

diffstat:

 njs/njs_crypto.c    |   6 +---
 njs/njs_error.c     |   6 +++-
 njs/njs_fs.c        |  24 +++++++--------------
 njs/njs_generator.c |   7 ++---
 njs/njs_json.c      |   2 +-
 njs/njs_module.c    |   3 +-
 njs/njs_object.c    |   7 ++---
 njs/njs_parser.c    |  57 +++++++++++++++++++++-------------------------------
 njs/njs_regexp.c    |   9 +++----
 njs/njs_string.c    |   5 +--
 njs/njs_variable.c  |   3 +-
 njs/njs_vm.c        |  18 ++++++----------
 12 files changed, 59 insertions(+), 88 deletions(-)

diffs (465 lines):

diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_crypto.c
--- a/njs/njs_crypto.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_crypto.c	Wed Jan 30 18:49:33 2019 +0300
@@ -691,8 +691,7 @@ njs_crypto_alg(njs_vm_t *vm, const nxt_s
         }
     }
 
-    njs_type_error(vm, "not supported algorithm: '%.*s'",
-                   (int) name->length, name->start);
+    njs_type_error(vm, "not supported algorithm: '%V'", name);
 
     return NULL;
 }
@@ -709,8 +708,7 @@ njs_crypto_encoding(njs_vm_t *vm, const 
         }
     }
 
-    njs_type_error(vm, "Unknown digest encoding: '%.*s'",
-                   (int) name->length, name->start);
+    njs_type_error(vm, "Unknown digest encoding: '%V'", name);
 
     return NULL;
 }
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_error.c
--- a/njs/njs_error.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_error.c	Wed Jan 30 18:49:33 2019 +0300
@@ -23,13 +23,15 @@ njs_exception_error_create(njs_vm_t *vm,
     nxt_int_t     ret;
     njs_value_t   string;
     njs_object_t  *error;
-    char          buf[256];
+    u_char        buf[256], *p;
 
     if (fmt != NULL) {
         va_start(args, fmt);
-        size = vsnprintf(buf, sizeof(buf), fmt, args);
+        p = nxt_vsprintf(buf, buf + sizeof(buf), fmt, args);
         va_end(args);
 
+        size = p - buf;
+
     } else {
         size = 0;
     }
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_fs.c
--- a/njs/njs_fs.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_fs.c	Wed Jan 30 18:49:33 2019 +0300
@@ -165,8 +165,7 @@ njs_fs_read_file(njs_vm_t *vm, njs_value
 
     flags = njs_fs_flags(&flag);
     if (nxt_slow_path(flags == -1)) {
-        njs_type_error(vm, "Unknown file open flags: '%.*s'",
-                       (int) flag.length, flag.start);
+        njs_type_error(vm, "Unknown file open flags: '%V'", &flag);
         return NJS_ERROR;
     }
 
@@ -178,8 +177,7 @@ njs_fs_read_file(njs_vm_t *vm, njs_value
     if (encoding.length != 0
         && (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
     {
-        njs_type_error(vm, "Unknown encoding: '%.*s'",
-                       (int) encoding.length, encoding.start);
+        njs_type_error(vm, "Unknown encoding: '%V'", &encoding);
         return NJS_ERROR;
     }
 
@@ -362,8 +360,7 @@ njs_fs_read_file_sync(njs_vm_t *vm, njs_
 
     flags = njs_fs_flags(&flag);
     if (nxt_slow_path(flags == -1)) {
-        njs_type_error(vm, "Unknown file open flags: '%.*s'",
-                       (int) flag.length, flag.start);
+        njs_type_error(vm, "Unknown file open flags: '%V'", &flag);
         return NJS_ERROR;
     }
 
@@ -375,8 +372,7 @@ njs_fs_read_file_sync(njs_vm_t *vm, njs_
     if (encoding.length != 0
         && (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
     {
-        njs_type_error(vm, "Unknown encoding: '%.*s'",
-                       (int) encoding.length, encoding.start);
+        njs_type_error(vm, "Unknown encoding: '%V'", &encoding);
         return NJS_ERROR;
     }
 
@@ -611,8 +607,7 @@ static njs_ret_t njs_fs_write_file_inter
     if (flag.start != NULL) {
         flags = njs_fs_flags(&flag);
         if (nxt_slow_path(flags == -1)) {
-            njs_type_error(vm, "Unknown file open flags: '%.*s'",
-                           (int) flag.length, flag.start);
+            njs_type_error(vm, "Unknown file open flags: '%V'", &flag);
             return NJS_ERROR;
         }
 
@@ -635,8 +630,7 @@ static njs_ret_t njs_fs_write_file_inter
     if (encoding.length != 0
         && (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
     {
-        njs_type_error(vm, "Unknown encoding: '%.*s'",
-                       (int) encoding.length, encoding.start);
+        njs_type_error(vm, "Unknown encoding: '%V'", &encoding);
         return NJS_ERROR;
     }
 
@@ -785,8 +779,7 @@ njs_fs_write_file_sync_internal(njs_vm_t
     if (flag.start != NULL) {
         flags = njs_fs_flags(&flag);
         if (nxt_slow_path(flags == -1)) {
-            njs_type_error(vm, "Unknown file open flags: '%.*s'",
-                           (int) flag.length, flag.start);
+            njs_type_error(vm, "Unknown file open flags: '%V'", &flag);
             return NJS_ERROR;
         }
 
@@ -809,8 +802,7 @@ njs_fs_write_file_sync_internal(njs_vm_t
     if (encoding.length != 0
         && (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
     {
-        njs_type_error(vm, "Unknown encoding: '%.*s'",
-                       (int) encoding.length, encoding.start);
+        njs_type_error(vm, "Unknown encoding: '%V'", &encoding);
         return NJS_ERROR;
     }
 
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_generator.c
--- a/njs/njs_generator.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_generator.c	Wed Jan 30 18:49:33 2019 +0300
@@ -3166,12 +3166,11 @@ njs_generate_syntax_error(njs_vm_t *vm, 
     const char* fmt, ...)
 {
     va_list  args;
-
-    static char  buf[256];
+    u_char   buf[256], *end;
 
     va_start(args, fmt);
-    (void) vsnprintf(buf, sizeof(buf), fmt, args);
+    end = nxt_vsprintf(buf, buf + sizeof(buf), fmt, args);
     va_end(args);
 
-    njs_syntax_error(vm, "%s in %u", buf, token_line);
+    njs_syntax_error(vm, "%*s in %uD", end - buf, buf, token_line);
 }
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_json.c
--- a/njs/njs_json.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_json.c	Wed Jan 30 18:49:33 2019 +0300
@@ -1131,7 +1131,7 @@ njs_json_parse_exception(njs_json_parse_
         length = 0;
     }
 
-    njs_syntax_error(ctx->vm, "%s at position %zu", msg, length);
+    njs_syntax_error(ctx->vm, "%s at position %z", msg, length);
 }
 
 
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_module.c
--- a/njs/njs_module.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_module.c	Wed Jan 30 18:49:33 2019 +0300
@@ -62,8 +62,7 @@ njs_ret_t njs_module_require(njs_vm_t *v
         return NXT_OK;
     }
 
-    njs_error(vm, "Cannot find module '%.*s'",
-              (int) lhq.key.length, lhq.key.start);
+    njs_error(vm, "Cannot find module '%V'", &lhq.key);
 
     return NJS_ERROR;
 }
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_object.c
--- a/njs/njs_object.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_object.c	Wed Jan 30 18:49:33 2019 +0300
@@ -364,8 +364,8 @@ njs_property_query(njs_vm_t *vm, njs_pro
 
         if (nxt_fast_path(ret == NXT_OK)) {
             njs_string_get(&pq->value, &pq->lhq.key);
-            njs_type_error(vm, "cannot get property '%.*s' of undefined",
-                           (int) pq->lhq.key.length, pq->lhq.key.start);
+            njs_type_error(vm, "cannot get property '%V' of undefined",
+                           &pq->lhq.key);
             return NXT_ERROR;
         }
 
@@ -1535,8 +1535,7 @@ njs_define_property(njs_vm_t *vm, njs_va
 
 exception:
 
-    njs_type_error(vm, "Cannot redefine property: '%.*s'",
-                   (int) pq.lhq.key.length, pq.lhq.key.start);
+    njs_type_error(vm, "Cannot redefine property: '%V'", &pq.lhq.key);
 
     return NXT_ERROR;
 }
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_parser.c
--- a/njs/njs_parser.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_parser.c	Wed Jan 30 18:49:33 2019 +0300
@@ -510,10 +510,9 @@ njs_parser_function_declaration(njs_vm_t
 
     if (token != NJS_TOKEN_NAME) {
         if (token == NJS_TOKEN_ARGUMENTS || token ==  NJS_TOKEN_EVAL) {
-            njs_parser_syntax_error(vm, parser, "Identifier \"%.*s\" "
+            njs_parser_syntax_error(vm, parser, "Identifier \"%V\" "
                                     "is forbidden in function declaration",
-                                    (int) parser->lexer->text.length,
-                                    parser->lexer->text.start);
+                                    &parser->lexer->text);
         }
 
         return NJS_TOKEN_ILLEGAL;
@@ -854,10 +853,9 @@ njs_parser_var_statement(njs_vm_t *vm, n
 
         if (token != NJS_TOKEN_NAME) {
             if (token == NJS_TOKEN_ARGUMENTS || token ==  NJS_TOKEN_EVAL) {
-                njs_parser_syntax_error(vm, parser, "Identifier \"%.*s\" "
+                njs_parser_syntax_error(vm, parser, "Identifier \"%V\" "
                                         "is forbidden in var declaration",
-                                        (int) parser->lexer->text.length,
-                                        parser->lexer->text.start);
+                                        &parser->lexer->text);
             }
 
             return NJS_TOKEN_ILLEGAL;
@@ -1317,10 +1315,9 @@ njs_parser_for_var_statement(njs_vm_t *v
 
         if (token != NJS_TOKEN_NAME) {
             if (token == NJS_TOKEN_ARGUMENTS || token ==  NJS_TOKEN_EVAL) {
-                njs_parser_syntax_error(vm, parser, "Identifier \"%.*s\" "
+                njs_parser_syntax_error(vm, parser, "Identifier \"%V\" "
                                        "is forbidden in for-in var declaration",
-                                       (int) parser->lexer->text.length,
-                                       parser->lexer->text.start);
+                                       &parser->lexer->text);
             }
 
             return NJS_TOKEN_ILLEGAL;
@@ -1457,9 +1454,8 @@ njs_parser_for_in_statement(njs_vm_t *vm
     node = parser->node->left;
 
     if (node->token != NJS_TOKEN_NAME) {
-        njs_parser_ref_error(vm, parser, "Invalid left-hand side \"%.*s\" "
-                             "in for-in statement", (int) name->length,
-                             name->start);
+        njs_parser_ref_error(vm, parser, "Invalid left-hand side \"%V\" "
+                             "in for-in statement", name);
 
         return NJS_TOKEN_ILLEGAL;
     }
@@ -1936,9 +1932,8 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
         break;
 
     case NJS_TOKEN_UNTERMINATED_STRING:
-        njs_parser_syntax_error(vm, parser, "Unterminated string \"%.*s\"",
-                                (int) parser->lexer->text.length,
-                                parser->lexer->text.start);
+        njs_parser_syntax_error(vm, parser, "Unterminated string \"%V\"",
+                                &parser->lexer->text);
 
         return NJS_TOKEN_ILLEGAL;
 
@@ -2007,10 +2002,8 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
         nxt_thread_log_debug("JS: arguments");
 
         if (parser->scope->type <= NJS_SCOPE_GLOBAL) {
-            njs_parser_syntax_error(vm, parser, "\"%.*s\" object "
-                                    "in global scope",
-                                    (int) parser->lexer->text.length,
-                                    parser->lexer->text.start);
+            njs_parser_syntax_error(vm, parser, "\"%V\" object "
+                                    "in global scope", &parser->lexer->text);
 
             return NJS_TOKEN_ILLEGAL;
         }
@@ -2591,9 +2584,8 @@ njs_parser_escape_string_create(njs_vm_t
 
 invalid:
 
-    njs_parser_syntax_error(vm, parser, "Invalid Unicode code point \"%.*s\"",
-                            (int) parser->lexer->text.length,
-                            parser->lexer->text.start);
+    njs_parser_syntax_error(vm, parser, "Invalid Unicode code point \"%V\"",
+                            &parser->lexer->text);
 
     return NJS_TOKEN_ILLEGAL;
 }
@@ -2635,9 +2627,8 @@ njs_parser_unexpected_token(njs_vm_t *vm
     njs_token_t token)
 {
     if (token != NJS_TOKEN_END) {
-        njs_parser_syntax_error(vm, parser, "Unexpected token \"%.*s\"",
-                                (int) parser->lexer->text.length,
-                                parser->lexer->text.start);
+        njs_parser_syntax_error(vm, parser, "Unexpected token \"%V\"",
+                                &parser->lexer->text);
 
     } else {
         njs_parser_syntax_error(vm, parser, "Unexpected end of input");
@@ -2665,7 +2656,7 @@ njs_parser_trace_handler(nxt_trace_t *tr
     p = trace->handler(trace, td, p);
 
     if (vm->parser != NULL) {
-        njs_internal_error(vm, "%s in %u", start, vm->parser->lexer->line);
+        njs_internal_error(vm, "%s in %uD", start, vm->parser->lexer->line);
     } else {
         njs_internal_error(vm, "%s", start);
     }
@@ -2679,14 +2670,13 @@ njs_parser_syntax_error(njs_vm_t *vm, nj
     ...)
 {
     va_list  args;
-
-    static char  buf[256];
+    u_char   buf[256], *end;
 
     va_start(args, fmt);
-    (void) vsnprintf(buf, sizeof(buf), fmt, args);
+    end = nxt_vsprintf(buf, buf + sizeof(buf), fmt, args);
     va_end(args);
 
-    njs_syntax_error(vm, "%s in %u", buf, parser->lexer->line);
+    njs_syntax_error(vm, "%*s in %uD", end - buf, buf, parser->lexer->line);
 }
 
 
@@ -2695,12 +2685,11 @@ njs_parser_ref_error(njs_vm_t *vm, njs_p
     ...)
 {
     va_list  args;
-
-    static char  buf[256];
+    u_char   buf[256], *end;
 
     va_start(args, fmt);
-    (void) vsnprintf(buf, sizeof(buf), fmt, args);
+    end = nxt_vsprintf(buf, buf + sizeof(buf), fmt, args);
     va_end(args);
 
-    njs_reference_error(vm, "%s in %u", buf, parser->lexer->line);
+    njs_reference_error(vm, "%*s in %uD", end - buf, buf, parser->lexer->line);
 }
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_regexp.c
--- a/njs/njs_regexp.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_regexp.c	Wed Jan 30 18:49:33 2019 +0300
@@ -88,8 +88,7 @@ njs_regexp_constructor(njs_vm_t *vm, njs
 
         flags = njs_regexp_flags(&start, start + string.length, 1);
         if (nxt_slow_path(flags < 0)) {
-            njs_syntax_error(vm, "Invalid RegExp flags \"%.*s\"",
-                             (int) string.length, string.start);
+            njs_syntax_error(vm, "Invalid RegExp flags \"%V\"", &string);
             return NXT_ERROR;
         }
 
@@ -163,7 +162,7 @@ njs_regexp_literal(njs_vm_t *vm, njs_par
 
             if (nxt_slow_path(flags < 0)) {
                 njs_parser_syntax_error(vm, parser,
-                                        "Invalid RegExp flags \"%.*s\"",
+                                        "Invalid RegExp flags \"%*s\"",
                                         p - lexer->start, lexer->start);
 
                 return NJS_TOKEN_ILLEGAL;
@@ -183,7 +182,7 @@ njs_regexp_literal(njs_vm_t *vm, njs_par
         }
     }
 
-    njs_parser_syntax_error(vm, parser, "Unterminated RegExp \"%.*s\"",
+    njs_parser_syntax_error(vm, parser, "Unterminated RegExp \"%*s\"",
                             p - (lexer->start - 1), lexer->start - 1);
 
     return NJS_TOKEN_ILLEGAL;
@@ -379,7 +378,7 @@ njs_regexp_compile_trace_handler(nxt_tra
     p = trace->handler(trace, td, start);
 
     if (vm->parser != NULL) {
-        njs_syntax_error(vm, "%s in %u", start, vm->parser->lexer->line);
+        njs_syntax_error(vm, "%s in %uD", start, vm->parser->lexer->line);
 
     } else {
         njs_syntax_error(vm, "%s", start);
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_string.c
--- a/njs/njs_string.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_string.c	Wed Jan 30 18:49:33 2019 +0300
@@ -808,7 +808,7 @@ njs_string_prototype_to_string(njs_vm_t 
         return njs_string_base64url(vm, &vm->retval, &str);
     }
 
-    njs_type_error(vm, "Unknown encoding: '%.*s'", (int) enc.length, enc.start);
+    njs_type_error(vm, "Unknown encoding: '%V'", &enc);
 
     return NJS_ERROR;
 }
@@ -1486,8 +1486,7 @@ njs_string_bytes_from_string(njs_vm_t *v
         return njs_string_decode_base64url(vm, &vm->retval, &str);
     }
 
-    njs_type_error(vm, "Unknown encoding: '%.*s'", (int) enc.length,
-                   enc.start);
+    njs_type_error(vm, "Unknown encoding: '%V'", &enc);
 
     return NJS_ERROR;
 }
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_variable.c
--- a/njs/njs_variable.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_variable.c	Wed Jan 30 18:49:33 2019 +0300
@@ -345,8 +345,7 @@ njs_variable_resolve(njs_vm_t *vm, njs_p
 
 not_found:
 
-    njs_parser_ref_error(vm, vm->parser, "\"%.*s\" is not defined",
-                         (int) vr->name.length, vr->name.start);
+    njs_parser_ref_error(vm, vm->parser, "\"%V\" is not defined", &vr->name);
 
     return NULL;
 }
diff -r d99f8ee4255c -r dbffb3031c77 njs/njs_vm.c
--- a/njs/njs_vm.c	Wed Jan 30 18:49:16 2019 +0300
+++ b/njs/njs_vm.c	Wed Jan 30 18:49:33 2019 +0300
@@ -566,9 +566,8 @@ njs_vmcode_property_set(njs_vm_t *vm, nj
 
     case NXT_DECLINED:
         if (nxt_slow_path(!object->data.u.object->extensible)) {
-            njs_type_error(vm, "Cannot add property '%.*s', "
-                           "object is not extensible", pq.lhq.key.length,
-                           pq.lhq.key.start);
+            njs_type_error(vm, "Cannot add property '%V', "
+                           "object is not extensible", &pq.lhq.key);
             return NXT_ERROR;
         }
 
@@ -610,9 +609,8 @@ njs_vmcode_property_set(njs_vm_t *vm, nj
     }
 
     if (nxt_slow_path(!prop->writable)) {
-        njs_type_error(vm, "Cannot assign to read-only property '%.*s' of %s",
-                       pq.lhq.key.length, pq.lhq.key.start,
-                       njs_type_string(object->type));
+        njs_type_error(vm, "Cannot assign to read-only property '%V' of %s",
+                       &pq.lhq.key, njs_type_string(object->type));
         return NXT_ERROR;
     }
 
@@ -722,9 +720,8 @@ njs_vmcode_property_delete(njs_vm_t *vm,
         }
 
         if (nxt_slow_path(!prop->configurable)) {
-            njs_type_error(vm, "Cannot delete property '%.*s' of %s",
-                           pq.lhq.key.length, pq.lhq.key.start,
-                           njs_type_string(object->type));
+            njs_type_error(vm, "Cannot delete property '%V' of %s",
+                           &pq.lhq.key, njs_type_string(object->type));
             return NXT_ERROR;
         }
 
@@ -1994,8 +1991,7 @@ njs_vmcode_method_frame(njs_vm_t *vm, nj
 
     if (value == NULL || !njs_is_function(value)) {
         njs_string_get(name, &string);
-        njs_type_error(vm, "'%.*s' is not a function", (int) string.length,
-                       string.start);
+        njs_type_error(vm, "'%V' is not a function", &string);
         return NXT_ERROR;
     }
 


More information about the nginx-devel mailing list