[njs] Fixed String.prototype.trimEnd() with unicode string.

Dmitry Volyntsev xeioex at nginx.com
Fri Sep 2 00:50:35 UTC 2022


details:   https://hg.nginx.org/njs/rev/dfe0eeff0568
branches:  
changeset: 1944:dfe0eeff0568
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Aug 31 18:35:58 2022 -0700
description:
Fixed String.prototype.trimEnd() with unicode string.

Previously, when the method was invoked with a string consisting of space
characters and at least one of them was a Unicode space separator (code
point above 127) it returned invalid string value with non-zero size
but zero length.

The fix is to update the size of the resulting string appropriately.

This closes #569 issue on Github.

diffstat:

 src/njs_string.c         |  1 +
 src/test/njs_unit_test.c |  8 ++++++++
 2 files changed, 9 insertions(+), 0 deletions(-)

diffs (29 lines):

diff -r 26b8f0c2ef94 -r dfe0eeff0568 src/njs_string.c
--- a/src/njs_string.c	Wed Aug 31 16:52:16 2022 -0700
+++ b/src/njs_string.c	Wed Aug 31 18:35:58 2022 -0700
@@ -2849,6 +2849,7 @@ njs_string_trim(const njs_value_t *value
 
             for ( ;; ) {
                 if (start == prev) {
+                    end = prev;
                     break;
                 }
 
diff -r 26b8f0c2ef94 -r dfe0eeff0568 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed Aug 31 16:52:16 2022 -0700
+++ b/src/test/njs_unit_test.c	Wed Aug 31 18:35:58 2022 -0700
@@ -8450,6 +8450,14 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("'   абв  '.trimStart().trimEnd()"),
       njs_str("абв") },
 
+    { njs_str("["
+              " String.fromCodePoint(0x2028),"
+              " String.fromCodePoint(0x20, 0x2028),"
+              " String.fromCodePoint(0x0009, 0x20, 0x2028),"
+              " String.fromCodePoint(0xFEFF),"
+              "].every(v => v.trimEnd() == '')"),
+      njs_str("true") },
+
     { njs_str("'\\u2029abc\\uFEFF\\u2028'.trim()"),
       njs_str("abc") },
 


More information about the nginx-devel mailing list