[njs] Using nxt_sprintf() instead of sprintf() in backtraces.

Dmitry Volyntsev xeioex at nginx.com
Tue Feb 5 13:30:34 UTC 2019


details:   https://hg.nginx.org/njs/rev/1568a0d46b0d
branches:  
changeset: 759:1568a0d46b0d
user:      hongzhidao <hongzhidao at gmail.com>
date:      Mon Feb 04 23:00:25 2019 +0800
description:
Using nxt_sprintf() instead of sprintf() in backtraces.

diffstat:

 njs/njs_builtin.c |  83 ++++++++++++++++--------------------------------------
 njs/njs_extern.c  |   7 +--
 njs/njs_vm.c      |  26 ++++++++--------
 3 files changed, 41 insertions(+), 75 deletions(-)

diffs (218 lines):

diff -r e8cd6be6d57e -r 1568a0d46b0d njs/njs_builtin.c
--- a/njs/njs_builtin.c	Mon Feb 04 16:30:27 2019 +0300
+++ b/njs/njs_builtin.c	Mon Feb 04 23:00:25 2019 +0800
@@ -978,69 +978,32 @@ nxt_int_t
 njs_builtin_match_native_function(njs_vm_t *vm, njs_function_t *function,
     nxt_str_t *name)
 {
-    char                       *buf;
     size_t                     len;
-    nxt_str_t                  string;
+    nxt_str_t                  string, middle;
     nxt_int_t                  rc;
     const njs_object_init_t    *obj, **p;
     const njs_object_prop_t    *prop;
     const njs_function_init_t  *fun;
 
+    middle = nxt_string_value(".");
+
     rc = njs_builtin_match(njs_object_init, function, &prop, &obj);
 
     if (rc == NXT_OK) {
-        njs_string_get(&prop->name, &string);
-        len = obj->name.length + string.length + sizeof(".");
-
-        buf = nxt_mp_zalloc(vm->mem_pool, len);
-        if (buf == NULL) {
-            return NXT_ERROR;
-        }
-
-        snprintf(buf, len, "%s.%s", obj->name.start, string.start);
-
-        name->length = len;
-        name->start = (u_char *) buf;
-
-        return NXT_OK;
+        goto found;
     }
 
     rc = njs_builtin_match(njs_prototype_init, function, &prop, &obj);
 
     if (rc == NXT_OK) {
-        njs_string_get(&prop->name, &string);
-        len = obj->name.length + string.length + sizeof(".prototype.");
-
-        buf = nxt_mp_zalloc(vm->mem_pool, len);
-        if (buf == NULL) {
-            return NXT_ERROR;
-        }
-
-        snprintf(buf, len, "%s.prototype.%s", obj->name.start, string.start);
-
-        name->length = len;
-        name->start = (u_char *) buf;
-
-        return NXT_OK;
+        middle = nxt_string_value(".prototype.");
+        goto found;
     }
 
     rc = njs_builtin_match(njs_constructor_init, function, &prop, &obj);
 
     if (rc == NXT_OK) {
-        njs_string_get(&prop->name, &string);
-        len = obj->name.length + string.length + sizeof(".");
-
-        buf = nxt_mp_zalloc(vm->mem_pool, len);
-        if (buf == NULL) {
-            return NXT_ERROR;
-        }
-
-        snprintf(buf, len, "%s.%s", obj->name.start, string.start);
-
-        name->length = len;
-        name->start = (u_char *) buf;
-
-        return NXT_OK;
+        goto found;
     }
 
     fun = njs_native_functions;
@@ -1056,23 +1019,27 @@ njs_builtin_match_native_function(njs_vm
     rc = njs_builtin_match(njs_module_init, function, &prop, &obj);
 
     if (rc == NXT_OK) {
-        njs_string_get(&prop->name, &string);
-        len = obj->name.length + string.length + sizeof(".");
-
-        buf = nxt_mp_zalloc(vm->mem_pool, len);
-        if (buf == NULL) {
-            return NXT_ERROR;
-        }
-
-        snprintf(buf, len, "%s.%s", obj->name.start, string.start);
-
-        name->length = len;
-        name->start = (u_char *) buf;
-
-        return NXT_OK;
+        goto found;
     }
 
     return NXT_DECLINED;
+
+found:
+
+    njs_string_get(&prop->name, &string);
+
+    len = obj->name.length + middle.length + string.length;
+
+    name->length = len;
+    name->start = nxt_mp_zalloc(vm->mem_pool, len);
+    if (name->start == NULL) {
+        return NXT_ERROR;
+    }
+
+    nxt_sprintf(name->start, name->start + len,
+                "%V%V%V", &obj->name, &middle, &string);
+
+    return NXT_OK;
 }
 
 
diff -r e8cd6be6d57e -r 1568a0d46b0d njs/njs_extern.c
--- a/njs/njs_extern.c	Mon Feb 04 16:30:27 2019 +0300
+++ b/njs/njs_extern.c	Mon Feb 04 23:00:25 2019 +0800
@@ -326,7 +326,7 @@ static nxt_int_t
 njs_external_match(njs_vm_t *vm, njs_function_native_t func, njs_extern_t *ext,
     nxt_str_t *name, njs_extern_part_t *head, njs_extern_part_t *ppart)
 {
-    char               *buf, *p;
+    u_char             *buf, *p;
     size_t             len;
     nxt_int_t          ret;
     njs_extern_t       *prop;
@@ -374,12 +374,11 @@ found:
     p = buf;
 
     for (pr = head; pr != NULL; pr = pr->next) {
-        p += snprintf(p, buf + len - p, "%.*s.", (int) pr->str.length,
-                      pr->str.start);
+        p = nxt_sprintf(p, buf + len, "%V.", &pr->str);
     }
 
     name->start = (u_char *) buf;
-    name->length = len;
+    name->length = len - 1;
 
     return NXT_OK;
 }
diff -r e8cd6be6d57e -r 1568a0d46b0d njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Feb 04 16:30:27 2019 +0300
+++ b/njs/njs_vm.c	Mon Feb 04 23:00:25 2019 +0800
@@ -3049,7 +3049,7 @@ njs_ret_t
 njs_vm_value_to_ext_string(njs_vm_t *vm, nxt_str_t *dst, const njs_value_t *src,
     nxt_uint_t handle_exception)
 {
-    u_char                 *p, *start;
+    u_char                 *p, *start, *end;
     size_t                 len, size, count;
     njs_ret_t              ret;
     nxt_uint_t             i, exception;
@@ -3135,16 +3135,17 @@ again:
                     } else {
 
                         if (count != 0) {
-                            len += sizeof("      repeats  times\n") + 10;
+                            len += nxt_length("      repeats  times\n")
+                                   + NXT_INT_T_LEN;
                             count = 0;
                         }
 
                         if (be[i].line != 0) {
-                            len += sizeof("    at  (:)\n") + 10
+                            len += nxt_length("    at  (:)\n") + NXT_INT_T_LEN
                                    + be[i].name.length;
 
                         } else {
-                            len += sizeof("    at  (native)\n")
+                            len += nxt_length("    at  (native)\n")
                                    + be[i].name.length;
                         }
                     }
@@ -3159,6 +3160,7 @@ again:
                 }
 
                 start = p;
+                end = start + len;
 
                 p = nxt_cpymem(p, dst->start, dst->length);
                 *p++ = '\n';
@@ -3174,20 +3176,18 @@ again:
 
                     } else {
                         if (count != 0) {
-                            p += sprintf((char *) p,
-                                         "      repeats %zu times\n", count);
-                            count =0;
+                            p = nxt_sprintf(p, end,
+                                            "      repeats %uz times\n", count);
+                            count = 0;
                         }
 
                         if (be[i].line != 0) {
-                            p += sprintf((char *) p, "    at %.*s (:%u)\n",
-                                         (int) be[i].name.length,
-                                         be[i].name.start, be[i].line);
+                            p = nxt_sprintf(p, end, "    at %V (:%uD)\n",
+                                            &be[i].name, be[i].line);
 
                         } else {
-                            p += sprintf((char *) p, "    at %.*s (native)\n",
-                                         (int) be[i].name.length,
-                                         be[i].name.start);
+                            p = nxt_sprintf(p, end, "    at %V (native)\n",
+                                            &be[i].name);
                         }
                     }
 


More information about the nginx-devel mailing list