[njs] Replacing vsprintf with nxt_vsprintf in modules' exceptions API.

Dmitry Volyntsev xeioex at nginx.com
Tue Feb 12 15:56:16 UTC 2019


details:   https://hg.nginx.org/njs/rev/1ab2658c7a28
branches:  
changeset: 775:1ab2658c7a28
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Feb 12 18:37:59 2019 +0300
description:
Replacing vsprintf with nxt_vsprintf in modules' exceptions API.

diffstat:

 nginx/ngx_http_js_module.c   |   3 +--
 nginx/ngx_stream_js_module.c |   6 ++----
 njs/njs_error.c              |  27 +++++++++++++++++----------
 njs/njs_error.h              |  30 ++++++++++++++++++------------
 njs/njs_generator.c          |   2 +-
 njs/njs_parser.c             |   2 +-
 njs/njs_vm.c                 |  35 ++++++-----------------------------
 7 files changed, 46 insertions(+), 59 deletions(-)

diffs (239 lines):

diff -r eb099f4faf40 -r 1ab2658c7a28 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Mon Feb 11 18:15:43 2019 +0300
+++ b/nginx/ngx_http_js_module.c	Tue Feb 12 18:37:59 2019 +0300
@@ -1751,8 +1751,7 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
             }
 
             if (method == n) {
-                njs_vm_error(vm, "unknown method \"%.*s\"",
-                             (int) method_name.length, method_name.start);
+                njs_vm_error(vm, "unknown method \"%V\"", &method_name);
                 return NJS_ERROR;
             }
         }
diff -r eb099f4faf40 -r 1ab2658c7a28 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Mon Feb 11 18:15:43 2019 +0300
+++ b/nginx/ngx_stream_js_module.c	Tue Feb 12 18:37:59 2019 +0300
@@ -862,8 +862,7 @@ ngx_stream_js_event(ngx_stream_session_t
     }
 
     if (i == n) {
-        njs_vm_error(ctx->vm, "unknown event \"%.*s\"", (int) event->length,
-                     event->start);
+        njs_vm_error(ctx->vm, "unknown event \"%V\"", event);
         return NULL;
     }
 
@@ -1053,8 +1052,7 @@ ngx_stream_js_ext_on(njs_vm_t *vm, njs_v
     }
 
     if (*event != NULL) {
-        njs_vm_error(vm, "event handler \"%.*s\" is already set",
-                     (int) name.length, name.start);
+        njs_vm_error(vm, "event handler \"%V\" is already set", &name);
         return NJS_ERROR;
     }
 
diff -r eb099f4faf40 -r 1ab2658c7a28 njs/njs_error.c
--- a/njs/njs_error.c	Mon Feb 11 18:15:43 2019 +0300
+++ b/njs/njs_error.c	Tue Feb 12 18:37:59 2019 +0300
@@ -15,7 +15,8 @@ static const njs_value_t  njs_error_name
 
 
 void
-njs_error_new(njs_vm_t *vm, njs_value_type_t type, u_char *start, size_t size)
+njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_value_type_t type,
+    u_char *start, size_t size)
 {
     nxt_int_t     ret;
     njs_value_t   string;
@@ -29,15 +30,16 @@ njs_error_new(njs_vm_t *vm, njs_value_ty
     error = njs_error_alloc(vm, type, NULL, &string);
 
     if (nxt_fast_path(error != NULL)) {
-        vm->retval.data.u.object = error;
-        vm->retval.type = type;
-        vm->retval.data.truth = 1;
+        dst->data.u.object = error;
+        dst->type = type;
+        dst->data.truth = 1;
     }
 }
 
 
 void
-njs_error_fmt_new(njs_vm_t *vm, njs_value_type_t type, const char* fmt, ...)
+njs_error_fmt_new(njs_vm_t *vm, njs_value_t *dst, njs_value_type_t type,
+    const char* fmt, ...)
 {
     va_list  args;
     u_char   buf[NXT_MAX_ERROR_STR], *p;
@@ -50,7 +52,7 @@ njs_error_fmt_new(njs_vm_t *vm, njs_valu
         va_end(args);
     }
 
-    njs_error_new(vm, type, buf, p - buf);
+    njs_error_new(vm, dst, type, buf, p - buf);
 }
 
 
@@ -65,8 +67,7 @@ njs_error_alloc(njs_vm_t *vm, njs_value_
 
     error = nxt_mp_alloc(vm->mem_pool, sizeof(njs_object_t));
     if (nxt_slow_path(error == NULL)) {
-        njs_memory_error(vm);
-        return NULL;
+        goto memory_error;
     }
 
     nxt_lvlhsh_init(&error->hash);
@@ -86,7 +87,7 @@ njs_error_alloc(njs_vm_t *vm, njs_value_
 
         prop = njs_object_prop_alloc(vm, &njs_error_name_string, name, 1);
         if (nxt_slow_path(prop == NULL)) {
-            return NULL;
+            goto memory_error;
         }
 
         lhq.value = prop;
@@ -105,7 +106,7 @@ njs_error_alloc(njs_vm_t *vm, njs_value_
 
         prop = njs_object_prop_alloc(vm, &njs_error_message_string, message, 1);
         if (nxt_slow_path(prop == NULL)) {
-            return NULL;
+            goto memory_error;
         }
 
         prop->enumerable = 0;
@@ -120,6 +121,12 @@ njs_error_alloc(njs_vm_t *vm, njs_value_
     }
 
     return error;
+
+memory_error:
+
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
diff -r eb099f4faf40 -r 1ab2658c7a28 njs/njs_error.h
--- a/njs/njs_error.h	Mon Feb 11 18:15:43 2019 +0300
+++ b/njs/njs_error.h	Tue Feb 12 18:37:59 2019 +0300
@@ -9,26 +9,32 @@
 
 
 #define njs_error(vm, fmt, ...)                                               \
-    njs_error_fmt_new(vm, NJS_OBJECT_ERROR, fmt, ##__VA_ARGS__)
+    njs_error_fmt_new(vm, &vm->retval, NJS_OBJECT_ERROR, fmt, ##__VA_ARGS__)
 #define njs_eval_error(vm, fmt, ...)                                          \
-    njs_error_fmt_new(vm, NJS_OBJECT_EVAL_ERROR, fmt, ##__VA_ARGS__)
+    njs_error_fmt_new(vm, &vm->retval, NJS_OBJECT_EVAL_ERROR, fmt,            \
+                      ##__VA_ARGS__)
 #define njs_internal_error(vm, fmt, ...)                                      \
-    njs_error_fmt_new(vm, NJS_OBJECT_INTERNAL_ERROR, fmt,            \
-                               ##__VA_ARGS__)
+    njs_error_fmt_new(vm, &vm->retval, NJS_OBJECT_INTERNAL_ERROR, fmt,        \
+                      ##__VA_ARGS__)
 #define njs_range_error(vm, fmt, ...)                                         \
-    njs_error_fmt_new(vm, NJS_OBJECT_RANGE_ERROR, fmt, ##__VA_ARGS__)
+    njs_error_fmt_new(vm, &vm->retval, NJS_OBJECT_RANGE_ERROR, fmt,           \
+                      ##__VA_ARGS__)
 #define njs_reference_error(vm, fmt, ...)                                     \
-    njs_error_fmt_new(vm, NJS_OBJECT_REF_ERROR, fmt, ##__VA_ARGS__)
+    njs_error_fmt_new(vm, &vm->retval, NJS_OBJECT_REF_ERROR, fmt,             \
+                      ##__VA_ARGS__)
 #define njs_syntax_error(vm, fmt, ...)                                        \
-    njs_error_fmt_new(vm, NJS_OBJECT_SYNTAX_ERROR, fmt, ##__VA_ARGS__)
+    njs_error_fmt_new(vm, &vm->retval, NJS_OBJECT_SYNTAX_ERROR, fmt,          \
+                      ##__VA_ARGS__)
 #define njs_type_error(vm, fmt, ...)                                          \
-    njs_error_fmt_new(vm, NJS_OBJECT_TYPE_ERROR, fmt, ##__VA_ARGS__)
+    njs_error_fmt_new(vm, &vm->retval, NJS_OBJECT_TYPE_ERROR, fmt,            \
+                      ##__VA_ARGS__)
 #define njs_uri_error(vm, fmt, ...)                                           \
-    njs_error_fmt_new(vm, NJS_OBJECT_URI_ERROR, fmt, ##__VA_ARGS__)
+    njs_error_fmt_new(vm, &vm->retval, NJS_OBJECT_URI_ERROR, fmt,             \
+                      ##__VA_ARGS__)
 
-void njs_error_new(njs_vm_t *vm, njs_value_type_t type, u_char *start,
-    size_t size);
-void njs_error_fmt_new(njs_vm_t *vm, njs_value_type_t type,
+void njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_value_type_t type,
+    u_char *start, size_t size);
+void njs_error_fmt_new(njs_vm_t *vm, njs_value_t *dst, njs_value_type_t type,
     const char* fmt, ...);
 
 void njs_memory_error(njs_vm_t *vm);
diff -r eb099f4faf40 -r 1ab2658c7a28 njs/njs_generator.c
--- a/njs/njs_generator.c	Mon Feb 11 18:15:43 2019 +0300
+++ b/njs/njs_generator.c	Tue Feb 12 18:37:59 2019 +0300
@@ -3191,5 +3191,5 @@ njs_generate_syntax_error(njs_vm_t *vm, 
         p = nxt_sprintf(p, end, " in %uD", node->token_line);
     }
 
-    njs_error_new(vm, NJS_OBJECT_SYNTAX_ERROR, msg, p - msg);
+    njs_error_new(vm, &vm->retval, NJS_OBJECT_SYNTAX_ERROR, msg, p - msg);
 }
diff -r eb099f4faf40 -r 1ab2658c7a28 njs/njs_parser.c
--- a/njs/njs_parser.c	Mon Feb 11 18:15:43 2019 +0300
+++ b/njs/njs_parser.c	Tue Feb 12 18:37:59 2019 +0300
@@ -2759,5 +2759,5 @@ njs_parser_error(njs_vm_t *vm, njs_parse
         p = nxt_sprintf(p, end, " in %uD", lexer->line);
     }
 
-    njs_error_new(vm, type, msg, p - msg);
+    njs_error_new(vm, &vm->retval, type, msg, p - msg);
  }
diff -r eb099f4faf40 -r 1ab2658c7a28 njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Feb 11 18:15:43 2019 +0300
+++ b/njs/njs_vm.c	Tue Feb 12 18:37:59 2019 +0300
@@ -3329,41 +3329,18 @@ njs_value_data_set(njs_value_t *value, v
 void
 njs_value_error_set(njs_vm_t *vm, njs_value_t *value, const char *fmt, ...)
 {
-    size_t        size;
-    va_list       args;
-    nxt_int_t     ret;
-    njs_value_t   string;
-    njs_object_t  *error;
-    char          buf[256];
+    va_list  args;
+    u_char   buf[NXT_MAX_ERROR_STR], *p;
+
+    p = buf;
 
     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);
-
-    } else {
-        size = 0;
-    }
-
-    ret = njs_string_new(vm, &string, (u_char *) buf, size, size);
-    if (nxt_slow_path(ret != NXT_OK)) {
-        goto memory_error;
     }
 
-    error = njs_error_alloc(vm, NJS_OBJECT_ERROR, NULL, &string);
-    if (nxt_slow_path(error == NULL)) {
-        goto memory_error;
-    }
-
-    value->data.u.object = error;
-    value->type = NJS_OBJECT_ERROR;
-    value->data.truth = 1;
-
-    return;
-
-memory_error:
-
-    njs_memory_error_set(vm, value);
+    njs_error_new(vm, value, NJS_OBJECT_ERROR, buf, p - buf);
 }
 
 


More information about the nginx-devel mailing list