From: Dmitry Volyntsev Date: Fri, 30 Nov 2018 14:52:02 +0000 (+0300) Subject: Fixed comparison of Byte and UTF8 strings. X-Git-Tag: 0.2.7~13 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=f0dc7105c37f656cb2c5ce073998420e98eff3b4;p=njs.git Fixed comparison of Byte and UTF8 strings. --- diff --git a/njs/njs_vm.c b/njs/njs_vm.c index 760c51a4..56737b4c 100644 --- a/njs/njs_vm.c +++ b/njs/njs_vm.c @@ -1712,7 +1712,7 @@ njs_vmcode_strict_not_equal(njs_vm_t *vm, njs_value_t *val1, njs_value_t *val2) 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_t *val1, const njs_value_t *val2) } 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_t *val1, const njs_value_t *val2) 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 --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index b6011a4e..af1b1303 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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)"),