[njs] Fixed njs_string_truncate() function for non-byte strings.

Alexander Borisov alexander.borisov at nginx.com
Wed Jul 22 12:21:57 UTC 2020


details:   https://hg.nginx.org/njs/rev/b96ff1d52647
branches:  
changeset: 1474:b96ff1d52647
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Wed Jul 22 15:21:13 2020 +0300
description:
Fixed njs_string_truncate() function for non-byte strings.

diffstat:

 src/njs_fs.c     |   2 +-
 src/njs_string.c |  25 ++++++++++++++-----------
 src/njs_string.h |   2 +-
 3 files changed, 16 insertions(+), 13 deletions(-)

diffs (88 lines):

diff -r a9e536ca9b69 -r b96ff1d52647 src/njs_fs.c
--- a/src/njs_fs.c	Wed Jul 22 11:27:18 2020 +0000
+++ b/src/njs_fs.c	Wed Jul 22 15:21:13 2020 +0300
@@ -262,7 +262,7 @@ njs_fs_read_file(njs_vm_t *vm, njs_value
 
         if (njs_slow_path(data.length < size)) {
             /* Pseudo-files may return less data than declared by st_size. */
-            njs_string_truncate(&retval, data.length);
+            njs_string_truncate(&retval, data.length, length);
         }
 
         size = data.length;
diff -r a9e536ca9b69 -r b96ff1d52647 src/njs_string.c
--- a/src/njs_string.c	Wed Jul 22 11:27:18 2020 +0000
+++ b/src/njs_string.c	Wed Jul 22 15:21:13 2020 +0300
@@ -172,30 +172,33 @@ njs_string_alloc(njs_vm_t *vm, njs_value
 
 
 void
-njs_string_truncate(njs_value_t *value, uint32_t size)
+njs_string_truncate(njs_value_t *value, uint32_t size, uint32_t length)
 {
-    u_char  *dst, *src;
+    u_char    *dst, *src;
+    uint32_t  n;
 
     if (size <= NJS_STRING_SHORT) {
-        if (value->short_string.size != NJS_STRING_LONG) {
-            value->short_string.size = size;
-
-        } else {
-            value->short_string.size = size;
+        if (value->short_string.size == NJS_STRING_LONG) {
             dst = value->short_string.start;
             src = value->long_string.data->start;
 
-            while (size != 0) {
+            n = size;
+
+            while (n != 0) {
                 /* The maximum size is just 14 bytes. */
                 njs_pragma_loop_disable_vectorization;
 
                 *dst++ = *src++;
-                size--;
+                n--;
             }
         }
 
+        value->short_string.size = size;
+        value->short_string.length = length;
+
     } else {
         value->long_string.size = size;
+        value->long_string.data->length = length;
     }
 }
 
@@ -1708,7 +1711,7 @@ njs_string_decode_hex(njs_vm_t *vm, njs_
     }
 
     if (njs_slow_path((size_t) (p - dst) != (len / 2))) {
-        njs_string_truncate(value, p - dst);
+        njs_string_truncate(value, p - dst, 0);
     }
 
     return NJS_OK;
@@ -1825,7 +1828,7 @@ njs_decode_base64_core(njs_vm_t *vm, njs
     }
 
     if (njs_slow_path((size_t) (d - dst) != dst_len)) {
-        njs_string_truncate(value, d - dst);
+        njs_string_truncate(value, d - dst, 0);
     }
 
     return NJS_OK;
diff -r a9e536ca9b69 -r b96ff1d52647 src/njs_string.h
--- a/src/njs_string.h	Wed Jul 22 11:27:18 2020 +0000
+++ b/src/njs_string.h	Wed Jul 22 15:21:13 2020 +0300
@@ -173,7 +173,7 @@ njs_int_t njs_string_decode_base64(njs_v
     const njs_str_t *src);
 njs_int_t njs_string_decode_base64url(njs_vm_t *vm, njs_value_t *value,
     const njs_str_t *src);
-void njs_string_truncate(njs_value_t *value, uint32_t size);
+void njs_string_truncate(njs_value_t *value, uint32_t size, uint32_t length);
 void njs_string_copy(njs_value_t *dst, njs_value_t *src);
 njs_int_t njs_string_validate(njs_vm_t *vm, njs_string_prop_t *string,
     njs_value_t *value);


More information about the nginx-devel mailing list