[2/3] jscript: Properly handle \0 characters in String indexOf method.

Sebastian Lackner sebastian at fds-team.de
Thu Sep 8 16:53:06 CDT 2016


Signed-off-by: Sebastian Lackner <sebastian at fds-team.de>
---

This also synchronizes the code with lastIndexOf which already supports
embedded \0 characters.

 dlls/jscript/string.c     |   20 ++++++++++++--------
 dlls/jscript/tests/api.js |   12 ++++++++++++
 2 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/dlls/jscript/string.c b/dlls/jscript/string.c
index 40aa552..b5c0197 100644
--- a/dlls/jscript/string.c
+++ b/dlls/jscript/string.c
@@ -486,9 +486,9 @@ static HRESULT String_fontsize(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, u
 static HRESULT String_indexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
         jsval_t *r)
 {
+    unsigned pos = 0, search_len, length;
     jsstr_t *search_jsstr, *jsstr;
     const WCHAR *search_str, *str;
-    int length, pos = 0;
     INT ret = -1;
     HRESULT hres;
 
@@ -498,7 +498,6 @@ static HRESULT String_indexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, un
     if(FAILED(hres))
         return hres;
 
-    length = jsstr_length(jsstr);
     if(!argc) {
         if(r)
             *r = jsval_number(-1);
@@ -512,6 +511,9 @@ static HRESULT String_indexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, un
         return hres;
     }
 
+    search_len = jsstr_length(search_jsstr);
+    length = jsstr_length(jsstr);
+
     if(argc >= 2) {
         double d;
 
@@ -520,14 +522,16 @@ static HRESULT String_indexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, un
             pos = is_int32(d) ? min(length, d) : length;
     }
 
-    if(SUCCEEDED(hres)) {
+    if(SUCCEEDED(hres) && length >= search_len) {
+        const WCHAR *end = str+length-search_len;
         const WCHAR *ptr;
 
-        ptr = strstrW(str+pos, search_str);
-        if(ptr)
-            ret = ptr - str;
-        else
-            ret = -1;
+        for(ptr = str+pos; ptr <= end; ptr++) {
+            if(!memcmp(ptr, search_str, search_len*sizeof(WCHAR))) {
+                ret = ptr-str;
+                break;
+            }
+        }
     }
 
     jsstr_release(search_jsstr);
diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js
index e7b55fc..43f7a19 100644
--- a/dlls/jscript/tests/api.js
+++ b/dlls/jscript/tests/api.js
@@ -629,6 +629,12 @@ tmp = "abcd".indexOf();
 ok(tmp == -1, "indexOf = " + tmp);
 tmp = "abcd".indexOf("b", bigInt);
 ok(tmp == -1, "indexOf = " + tmp);
+tmp = "abcd".indexOf("abcd",0);
+ok(tmp === 0, "indexOf = " + tmp);
+tmp = "abcd".indexOf("abcd",1);
+ok(tmp === -1, "indexOf = " + tmp);
+tmp = ("ab" + String.fromCharCode(0) + "cd").indexOf(String.fromCharCode(0));
+ok(tmp === 2, "indexOf = " + tmp);
 
 tmp = "abcd".lastIndexOf("bc",1);
 ok(tmp === 1, "lastIndexOf = " + tmp);
@@ -650,6 +656,12 @@ tmp = strObj.lastIndexOf("b");
 ok(tmp === 1, "lastIndexOf = " + tmp);
 tmp = "bbb".lastIndexOf("b", bigInt);
 ok(tmp === 2, "lastIndexOf = " + tmp);
+tmp = "abcd".lastIndexOf("abcd",4);
+ok(tmp === 0, "lastIndexOf = " + tmp);
+tmp = "abcd".lastIndexOf("abcd",0);
+ok(tmp === 0, "lastIndexOf = " + tmp);
+tmp = ("ab" + String.fromCharCode(0) + "cd").lastIndexOf(String.fromCharCode(0));
+ok(tmp === 2, "lastIndexOf = " + tmp);
 
 tmp = "".toLowerCase();
 ok(tmp === "", "''.toLowerCase() = " + tmp);
-- 
2.9.0



More information about the wine-patches mailing list