[njs] Eliminating sprintf() and vsnprintf() usage.

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


details:   https://hg.nginx.org/njs/rev/03be823cd95b
branches:  
changeset: 776:03be823cd95b
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Feb 12 18:56:04 2019 +0300
description:
Eliminating sprintf() and vsnprintf() usage.

diffstat:

 njs/njs_builtin.c        |  21 ++++++++++-----------
 njs/njs_date.c           |  28 ++++++++++++----------------
 njs/njs_json.c           |   9 ++++-----
 njs/njs_number.h         |   9 +++++----
 njs/njs_regexp.c         |   5 +++--
 njs/njs_shell.c          |  12 ++++++------
 njs/njs_time.c           |   8 +++++---
 njs/test/njs_unit_test.c |   9 ++++-----
 nxt/nxt_string.h         |   3 +++
 nxt/nxt_trace.c          |   9 +++++----
 10 files changed, 57 insertions(+), 56 deletions(-)

diffs (335 lines):

diff -r 1ab2658c7a28 -r 03be823cd95b njs/njs_builtin.c
--- a/njs/njs_builtin.c	Tue Feb 12 18:37:59 2019 +0300
+++ b/njs/njs_builtin.c	Tue Feb 12 18:56:04 2019 +0300
@@ -595,7 +595,7 @@ njs_builtin_completions_size(njs_vm_t *v
 static nxt_array_t *
 njs_builtin_completions(njs_vm_t *vm, nxt_array_t *array)
 {
-    char                     *compl;
+    u_char                   *compl;
     size_t                   n, len;
     nxt_str_t                string, *completions;
     nxt_uint_t               i, k;
@@ -634,7 +634,8 @@ njs_builtin_completions(njs_vm_t *vm, nx
                 return NULL;
             }
 
-            snprintf(compl, len, "%s.%s", obj->name.start, string.start);
+            nxt_sprintf(compl, compl + len, "%s.%s%Z", obj->name.start,
+                        string.start);
 
             completions[n].length = len;
             completions[n++].start = (u_char *) compl;
@@ -654,12 +655,10 @@ njs_builtin_completions(njs_vm_t *vm, nx
                 return NULL;
             }
 
-            snprintf(compl, len, ".%s", string.start);
+            nxt_sprintf(compl, compl + len, ".%s%Z", string.start);
 
             for (k = 0; k < n; k++) {
-                if (strncmp((char *) completions[k].start, compl, len)
-                    == 0)
-                {
+                if (nxt_strncmp(completions[k].start, compl, len) == 0) {
                     break;
                 }
             }
@@ -684,7 +683,8 @@ njs_builtin_completions(njs_vm_t *vm, nx
                 return NULL;
             }
 
-            snprintf(compl, len, "%s.%s", obj->name.start, string.start);
+            nxt_sprintf(compl, compl + len, "%s.%s%Z", obj->name.start,
+                        string.start);
 
             completions[n].length = len;
             completions[n++].start = (u_char *) compl;
@@ -710,7 +710,7 @@ njs_builtin_completions(njs_vm_t *vm, nx
             return NULL;
         }
 
-        snprintf(compl, len, "%.*s", (int) ev->name.length, ev->name.start);
+        nxt_sprintf(compl, compl + len, "%V%Z", &ev->name);
 
         completions[n].length = len;
         completions[n++].start = (u_char *) compl;
@@ -728,9 +728,8 @@ njs_builtin_completions(njs_vm_t *vm, nx
                 return NULL;
             }
 
-            snprintf(compl, len, "%.*s.%.*s", (int) ev->name.length,
-                     ev->name.start, (int) ext_prop->name.length,
-                     ext_prop->name.start);
+            nxt_sprintf(compl, compl + len, "%V.%V%Z", &ev->name,
+                        &ext_prop->name);
 
             completions[n].length = len;
             completions[n++].start = (u_char *) compl;
diff -r 1ab2658c7a28 -r 03be823cd95b njs/njs_date.c
--- a/njs/njs_date.c	Tue Feb 12 18:37:59 2019 +0300
+++ b/njs/njs_date.c	Tue Feb 12 18:56:04 2019 +0300
@@ -984,9 +984,8 @@ njs_date_prototype_to_utc_string(njs_vm_
     nxt_uint_t nargs, njs_index_t unused)
 {
     double             time;
-    size_t             size;
     time_t             clock;
-    u_char             buf[NJS_DATE_TIME_LEN];
+    u_char             buf[NJS_DATE_TIME_LEN], *p;
     struct tm          tm;
 
     static const char  *week[] = { "Sun", "Mon", "Tue", "Wed",
@@ -1001,13 +1000,12 @@ njs_date_prototype_to_utc_string(njs_vm_
         clock = time / 1000;
         gmtime_r(&clock, &tm);
 
-        size = snprintf((char *) buf, NJS_DATE_TIME_LEN,
+        p = nxt_sprintf(buf, buf + NJS_DATE_TIME_LEN,
                         "%s %s %02d %4d %02d:%02d:%02d GMT",
-                        week[tm.tm_wday], month[tm.tm_mon],
-                        tm.tm_mday, tm.tm_year + 1900,
-                        tm.tm_hour, tm.tm_min, tm.tm_sec);
-
-        return njs_string_new(vm, &vm->retval, buf, size, size);
+                        week[tm.tm_wday], month[tm.tm_mon], tm.tm_mday,
+                        tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
+
+        return njs_string_new(vm, &vm->retval, buf, p - buf, p - buf);
     }
 
     vm->retval = njs_string_invalid_date;
@@ -1029,9 +1027,8 @@ njs_date_to_string(njs_vm_t *vm, njs_val
 {
     int32_t    year;
     double     time;
-    size_t     size;
     time_t     clock;
-    u_char     buf[NJS_ISO_DATE_TIME_LEN];
+    u_char     buf[NJS_ISO_DATE_TIME_LEN], *p;
     struct tm  tm;
 
     time = date->data.u.date->time;
@@ -1043,14 +1040,13 @@ njs_date_to_string(njs_vm_t *vm, njs_val
 
         year = tm.tm_year + 1900;
 
-        size = snprintf((char *) buf, NJS_ISO_DATE_TIME_LEN,
+        p = nxt_sprintf(buf, buf + NJS_ISO_DATE_TIME_LEN,
                         (year < 0) ? "%07d-%02d-%02dT%02d:%02d:%02d.%03dZ"
                                    : "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
-                        year, tm.tm_mon + 1, tm.tm_mday,
-                        tm.tm_hour, tm.tm_min, tm.tm_sec,
-                        (int) ((int64_t) time % 1000));
-
-        return njs_string_new(vm, retval, buf, size, size);
+                        year, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
+                        tm.tm_sec, (int) ((int64_t) time % 1000));
+
+        return njs_string_new(vm, retval, buf, p - buf, p - buf);
     }
 
     vm->retval = njs_string_invalid_date;
diff -r 1ab2658c7a28 -r 03be823cd95b njs/njs_json.c
--- a/njs/njs_json.c	Tue Feb 12 18:37:59 2019 +0300
+++ b/njs/njs_json.c	Tue Feb 12 18:56:04 2019 +0300
@@ -2069,13 +2069,12 @@ const njs_object_init_t  njs_json_object
 static nxt_int_t
 njs_dump_value(njs_json_stringify_t *stringify, const njs_value_t *value)
 {
-    size_t              len;
     njs_ret_t           ret;
     nxt_str_t           str;
     nxt_uint_t          written;
     njs_value_t         str_val;
     const njs_extern_t  *ext_proto;
-    char                buf[32];
+    u_char              buf[32], *p;
 
     njs_ret_t           (*to_string)(njs_vm_t *, njs_value_t *,
                                      const njs_value_t *);
@@ -2243,9 +2242,9 @@ njs_dump_value(njs_json_stringify_t *str
         return njs_json_buf_append(stringify, (char *) str.start, str.length);
 
     default:
-        len = snprintf(buf, sizeof(buf), "[Unknown value type:%d]",
-                       value->type);
-        return njs_json_buf_append(stringify, buf, len);
+        p = nxt_sprintf(buf, buf + nxt_length(buf), "[Unknown value type:%uD]",
+                        value->type);
+        return njs_json_buf_append(stringify, (char *) buf, p - buf);
     }
 
     return ret;
diff -r 1ab2658c7a28 -r 03be823cd95b njs/njs_number.h
--- a/njs/njs_number.h	Tue Feb 12 18:37:59 2019 +0300
+++ b/njs/njs_number.h	Tue Feb 12 18:56:04 2019 +0300
@@ -62,11 +62,12 @@ njs_char_to_hex(u_char c)
 nxt_inline void
 njs_uint32_to_string(njs_value_t *value, uint32_t u32)
 {
-    size_t  size;
+    u_char  *dst, *p;
 
-    size = snprintf((char *) njs_string_short_start(value),
-                    NJS_STRING_SHORT, "%u", u32);
-    njs_string_short_set(value, size, size);
+    dst = njs_string_short_start(value);
+    p = nxt_sprintf(dst, dst + NJS_STRING_SHORT, "%uD", u32);
+
+    njs_string_short_set(value, p - dst, p - dst);
 }
 
 
diff -r 1ab2658c7a28 -r 03be823cd95b njs/njs_regexp.c
--- a/njs/njs_regexp.c	Tue Feb 12 18:37:59 2019 +0300
+++ b/njs/njs_regexp.c	Tue Feb 12 18:56:04 2019 +0300
@@ -378,10 +378,11 @@ njs_regexp_compile_trace_handler(nxt_tra
     p = trace->handler(trace, td, start);
 
     if (vm->parser != NULL) {
-        njs_syntax_error(vm, "%s in %uD", start, vm->parser->lexer->line);
+        njs_syntax_error(vm, "%*s in %uD", (int) (p - start), start,
+                         vm->parser->lexer->line);
 
     } else {
-        njs_syntax_error(vm, "%s", start);
+        njs_syntax_error(vm, "%*s", (int) (p - start), start);
     }
 
     return p;
diff -r 1ab2658c7a28 -r 03be823cd95b njs/njs_shell.c
--- a/njs/njs_shell.c	Tue Feb 12 18:37:59 2019 +0300
+++ b/njs/njs_shell.c	Tue Feb 12 18:56:04 2019 +0300
@@ -719,7 +719,7 @@ next:
                 continue;
             }
 
-            if (strncmp(text, (char *) var->name.start, cmpl->length) == 0) {
+            if (nxt_strncmp(text, var->name.start, cmpl->length) == 0) {
                 return njs_editline(&var->name);
             }
         }
@@ -769,8 +769,8 @@ next:
 
             suffix = njs_completion(cmpl->suffix_completions, cmpl->index++);
 
-            if (len != 0 && strncmp((char *) suffix->start, p,
-                                    nxt_min(len, suffix->length)) != 0)
+            if (len != 0 && nxt_strncmp(suffix->start, p,
+                                        nxt_min(len, suffix->length)) != 0)
             {
                 continue;
             }
@@ -781,8 +781,8 @@ next:
                 return NULL;
             }
 
-            snprintf(completion, len, "%.*s%.*s", (int) (p - text), text,
-                     (int) suffix->length, suffix->start);
+            nxt_sprintf((u_char *) completion, (u_char *) completion + len,
+                        "%*s%V%Z", (int) (p - text), text, suffix);
             return completion;
         }
 
@@ -803,7 +803,7 @@ next:
                 continue;
             }
 
-            if (strncmp(text, (char *) suffix->start, cmpl->length) == 0) {
+            if (nxt_strncmp(text, suffix->start, cmpl->length) == 0) {
                 return njs_editline(suffix);
             }
         }
diff -r 1ab2658c7a28 -r 03be823cd95b njs/njs_time.c
--- a/njs/njs_time.c	Tue Feb 12 18:37:59 2019 +0300
+++ b/njs/njs_time.c	Tue Feb 12 18:56:04 2019 +0300
@@ -100,7 +100,7 @@ njs_ret_t
 njs_clear_timeout(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused)
 {
-    u_char              buf[16];
+    u_char              buf[16], *p;
     njs_ret_t           ret;
     njs_event_t         *event;
     nxt_lvlhsh_query_t  lhq;
@@ -110,9 +110,11 @@ njs_clear_timeout(njs_vm_t *vm, njs_valu
         return NJS_OK;
     }
 
+    p = nxt_sprintf(buf, buf + nxt_length(buf), "%uD",
+                    (unsigned) args[1].data.u.number);
+
     lhq.key.start = buf;
-    lhq.key.length = snprintf((char *) buf, sizeof(buf) - 1, "%u",
-                              (unsigned) args[1].data.u.number);
+    lhq.key.length = p - buf;
     lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length);
     lhq.proto = &njs_event_hash_proto;
     lhq.pool = vm->mem_pool;
diff -r 1ab2658c7a28 -r 03be823cd95b njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Feb 12 18:37:59 2019 +0300
+++ b/njs/test/njs_unit_test.c	Tue Feb 12 18:56:04 2019 +0300
@@ -11288,15 +11288,14 @@ static njs_ret_t
 njs_unit_test_r_get_a_external(njs_vm_t *vm, njs_value_t *value, void *obj,
     uintptr_t data)
 {
-    char                 buf[16];
-    size_t               len;
+    u_char               buf[16], *p;
     njs_unit_test_req_t  *r;
 
     r = (njs_unit_test_req_t *) obj;
 
-    len = sprintf(buf, "%u", r->a);
-
-    return njs_string_create(vm, value, (u_char *) buf, len, 0);
+    p = nxt_sprintf(buf, buf + nxt_length(buf), "%uD", r->a);
+
+    return njs_string_create(vm, value, buf, p - buf, 0);
 }
 
 
diff -r 1ab2658c7a28 -r 03be823cd95b nxt/nxt_string.h
--- a/nxt/nxt_string.h	Tue Feb 12 18:37:59 2019 +0300
+++ b/nxt/nxt_string.h	Tue Feb 12 18:56:04 2019 +0300
@@ -44,6 +44,9 @@ nxt_upper_case(u_char c)
 #define nxt_cpymem(dst, src, n)   (((u_char *) memcpy(dst, src, n)) + (n))
 
 
+#define nxt_strncmp(s1, s2, n)        strncmp((char *) s1, (char *) s2, n)
+
+
 #define nxt_memset(buf, c, length)   (void) (memset(buf, c, length))
 
 
diff -r 1ab2658c7a28 -r 03be823cd95b nxt/nxt_trace.c
--- a/nxt/nxt_trace.c	Tue Feb 12 18:37:59 2019 +0300
+++ b/nxt/nxt_trace.c	Tue Feb 12 18:56:04 2019 +0300
@@ -8,6 +8,7 @@
 #include <nxt_types.h>
 #include <nxt_clang.h>
 #include <nxt_malloc.h>
+#include <nxt_sprintf.h>
 #include <nxt_trace.h>
 #include <stdio.h>
 
@@ -15,14 +16,14 @@
 static u_char *
 nxt_last_handler(nxt_trace_t *trace, nxt_trace_data_t *td, u_char *start)
 {
-    int      n;
+    u_char   *p;
     ssize_t  size;
 
     size = td->end - start;
-    n = vsnprintf((char *) start, size, td->fmt, td->args);
+    p = nxt_vsprintf(start, start + size, td->fmt, td->args);
 
-    if (n < size) {
-        start += n;
+    if (p - start < size) {
+        start = p;
     }
 
     return start;


More information about the nginx-devel mailing list