[njs] Fixed using of uninitialized value in String.prototype.padStart().

Dmitry Volyntsev xeioex at nginx.com
Mon Jul 1 16:25:34 UTC 2019


details:   https://hg.nginx.org/njs/rev/e123c7406264
branches:  
changeset: 1021:e123c7406264
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jul 01 19:24:10 2019 +0300
description:
Fixed using of uninitialized value in String.prototype.padStart().

diffstat:

 njs/njs_string.c |  49 +++++++++++++++++++++++++------------------------
 1 files changed, 25 insertions(+), 24 deletions(-)

diffs (75 lines):

diff -r 40f26bb516a6 -r e123c7406264 njs/njs_string.c
--- a/njs/njs_string.c	Thu Jun 27 18:55:34 2019 +0300
+++ b/njs/njs_string.c	Mon Jul 01 19:24:10 2019 +0300
@@ -2463,8 +2463,11 @@ njs_string_prototype_pad(njs_vm_t *vm, n
     int32_t            length, new_length;
     uint32_t           n, pad_length;
     const u_char       *end;
+    const njs_value_t  *pad;
     njs_string_prop_t  string, pad_string;
 
+    static const njs_value_t  string_space = njs_string(" ");
+
     length = njs_string_prop(&string, &args[0]);
     new_length = nargs > 1 ? args[1].data.u.number : 0;
 
@@ -2484,26 +2487,27 @@ njs_string_prototype_pad(njs_vm_t *vm, n
     n = 0;
     trunc = 0;
 
-    if (nargs > 2) {
-        pad_length = njs_string_prop(&pad_string, &args[2]);
-
-        if (pad_string.size == 0) {
-            vm->retval = args[0];
-            return NJS_OK;
-        }
-
-        if (pad_string.size > 1) {
-            n = padding / pad_length;
-            trunc = padding % pad_length;
-
-            if (pad_string.size != (size_t) pad_length) {
-                /* UTF-8 string. */
-                end = pad_string.start + pad_string.size;
-                end = njs_string_offset(pad_string.start, end, trunc);
-
-                trunc = end - pad_string.start;
-                padding = pad_string.size * n + trunc;
-            }
+    pad = njs_arg(args, nargs, 2);
+    pad = njs_is_undefined(pad) ? &string_space : pad;
+
+    pad_length = njs_string_prop(&pad_string, pad);
+
+    if (pad_string.size == 0) {
+        vm->retval = args[0];
+        return NJS_OK;
+    }
+
+    if (pad_string.size > 1) {
+        n = padding / pad_length;
+        trunc = padding % pad_length;
+
+        if (pad_string.size != (size_t) pad_length) {
+            /* UTF-8 string. */
+            end = pad_string.start + pad_string.size;
+            end = njs_string_offset(pad_string.start, end, trunc);
+
+            trunc = end - pad_string.start;
+            padding = pad_string.size * n + trunc;
         }
     }
 
@@ -2525,10 +2529,7 @@ njs_string_prototype_pad(njs_vm_t *vm, n
 
     memcpy(start, string.start, string.size);
 
-    if (nargs == 2) {
-        nxt_memset(p, ' ', padding);
-
-    } else if (pad_string.size == 1) {
+    if (pad_string.size == 1) {
         nxt_memset(p, pad_string.start[0], padding);
 
     } else {


More information about the nginx-devel mailing list