[njs] Fixed comparison of Byte and UTF8 strings.

Dmitry Volyntsev xeioex at nginx.com
Fri Nov 30 15:11:38 UTC 2018


details:   https://hg.nginx.org/njs/rev/e713f648ef71
branches:  
changeset: 680:e713f648ef71
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Nov 30 17:52:02 2018 +0300
description:
Fixed comparison of Byte and UTF8 strings.

diffstat:

 njs/njs_vm.c             |  22 +++++++++++++++++-----
 njs/test/njs_unit_test.c |  21 ++++++++++++++++++++-
 2 files changed, 37 insertions(+), 6 deletions(-)

diffs (98 lines):

diff -r 83b64328fbb2 -r e713f648ef71 njs/njs_vm.c
--- a/njs/njs_vm.c	Thu Nov 29 21:01:59 2018 +0300
+++ b/njs/njs_vm.c	Fri Nov 30 17:52:02 2018 +0300
@@ -1712,7 +1712,7 @@ njs_vmcode_strict_not_equal(njs_vm_t *vm
 nxt_noinline nxt_bool_t
 njs_values_strict_equal(const njs_value_t *val1, const njs_value_t *val2)
 {
-    size_t        size;
+    size_t        size, length1, length2;
     const u_char  *start1, *start2;
 
     if (val1->type != val2->type) {
@@ -1737,7 +1737,14 @@ njs_values_strict_equal(const njs_value_
         }
 
         if (size != NJS_STRING_LONG) {
-            if (val1->short_string.length != val2->short_string.length) {
+            length1 = val1->short_string.length;
+            length2 = val2->short_string.length;
+
+            /*
+             * Using full memcmp() comparison if at least one string
+             * is a Byte string.
+             */
+            if (length1 != 0 && length2 != 0 && length1 != length2) {
                 return 0;
             }
 
@@ -1751,9 +1758,14 @@ njs_values_strict_equal(const njs_value_
                 return 0;
             }
 
-            if (val1->long_string.data->length
-                != val2->long_string.data->length)
-            {
+            length1 = val1->long_string.data->length;
+            length2 = val2->long_string.data->length;
+
+            /*
+             * Using full memcmp() comparison if at least one string
+             * is a Byte string.
+             */
+            if (length1 != 0 && length2 != 0 && length1 != length2) {
                 return 0;
             }
 
diff -r 83b64328fbb2 -r e713f648ef71 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Nov 29 21:01:59 2018 +0300
+++ b/njs/test/njs_unit_test.c	Fri Nov 30 17:52:02 2018 +0300
@@ -2337,6 +2337,16 @@ static njs_unit_test_t  njs_test[] =
                  "} a"),
       nxt_string("A123DT") },
 
+    { nxt_string("var t; "
+                 "switch ($r3.uri) {"
+                 "case 'abc': "
+                 "  t='A'; "
+                 "  break; "
+                 "default: "
+                 "  t='F'; "
+                 "}; t"),
+      nxt_string("A") },
+
     /* continue. */
 
     { nxt_string("continue"),
@@ -4045,7 +4055,7 @@ static njs_unit_test_t  njs_test[] =
       nxt_string("true") },
 
     { nxt_string("'\\u00CE\\u00B1'.toBytes() === 'α'"),
-      nxt_string("false") },
+      nxt_string("true") },
 
     { nxt_string("var b = '\\u00C2\\u00B6'.toBytes(), u = b.fromUTF8();"
                  "b.length +' '+ b +' '+ u.length +' '+ u"),
@@ -4087,6 +4097,12 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var a = '\\xB5\\xA7\\xB1\\xAE'.toBytes(); a.fromBytes(1, 3)"),
       nxt_string("§±") },
 
+    { nxt_string("'A'.repeat(8).toBytes() === 'A'.repeat(8)"),
+      nxt_string("true") },
+
+    { nxt_string("'A'.repeat(16).toBytes() === 'A'.repeat(16)"),
+      nxt_string("true") },
+
     { nxt_string("var a = 'abcdefgh'; a.substr(3, 15)"),
       nxt_string("defgh") },
 
@@ -4476,6 +4492,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var o = {b:$r.props.b}; o.b"),
       nxt_string("42") },
 
+    { nxt_string("$r2.uri == 'αβγ' && $r2.uri === 'αβγ'"),
+      nxt_string("true") },
+
     /**/
 
     { nxt_string("'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'.charCodeAt(5)"),


More information about the nginx-devel mailing list