[3/3] jscript: Allocate string of correct size in Date toTimeString method.

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


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

I've decided to modify the tests so that they catch \0 characters at all
positions - this should be more reliable and also detect other mistakes than
off-by-one errors.

 dlls/jscript/date.c       |   15 +++++++--------
 dlls/jscript/tests/api.js |    6 ++++--
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/dlls/jscript/date.c b/dlls/jscript/date.c
index b6997ab..fdc3051 100644
--- a/dlls/jscript/date.c
+++ b/dlls/jscript/date.c
@@ -850,6 +850,7 @@ static HRESULT Date_toTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
         ':','%','0','2','d',' ','U','T','C',0 };
     DateInstance *date;
     jsstr_t *date_str;
+    WCHAR buf[32];
     DOUBLE time;
     WCHAR sign;
     int offset;
@@ -868,12 +869,6 @@ static HRESULT Date_toTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
     time = local_time(date->time, date);
 
     if(r) {
-        WCHAR *ptr;
-
-        date_str = jsstr_alloc_buf(17, &ptr);
-        if(!date_str)
-            return E_OUTOFMEMORY;
-
         offset = date->bias +
             daylight_saving_ta(time, date);
 
@@ -884,13 +879,17 @@ static HRESULT Date_toTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
         else sign = '-';
 
         if(offset)
-            sprintfW(ptr, formatW, (int)hour_from_time(time),
+            sprintfW(buf, formatW, (int)hour_from_time(time),
                     (int)min_from_time(time), (int)sec_from_time(time),
                     sign, offset/60, offset%60);
         else
-            sprintfW(ptr, formatUTCW, (int)hour_from_time(time),
+            sprintfW(buf, formatUTCW, (int)hour_from_time(time),
                     (int)min_from_time(time), (int)sec_from_time(time));
 
+        date_str = jsstr_alloc(buf);
+        if(!date_str)
+            return E_OUTOFMEMORY;
+
         *r = jsval_string(date_str);
     }
     return S_OK;
diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js
index 43f7a19..083da93 100644
--- a/dlls/jscript/tests/api.js
+++ b/dlls/jscript/tests/api.js
@@ -2147,9 +2147,11 @@ ok(Date.parse("Tue, 22 Mar 2016 09:57:55 +0400") === Date.parse("Tue, 22 Mar 201
         "Date.parse(\"Tue, 22 Mar 2016 09:57:55 +0400\") = " + Date.parse("Tue, 22 Mar 2016 09:57:55 +0400"));
 
 tmp = (new Date()).toLocaleDateString();
-ok(tmp.charCodeAt(tmp.length-1) != 0, "invalid null byte");
+ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
 tmp = (new Date()).toLocaleTimeString();
-ok(tmp.charCodeAt(tmp.length-1) != 0, "invalid null byte");
+ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
+tmp = (new Date()).toTimeString();
+ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
 
 ok(typeof(Math.PI) === "number", "typeof(Math.PI) = " + typeof(Math.PI));
 ok(Math.floor(Math.PI*100) === 314, "Math.PI = " + Math.PI);
-- 
2.9.0



More information about the wine-patches mailing list