From: Jacek Caban Subject: jscript: Properly test if double may be converted to int32 Message-Id: <4FA1488B.5030704@codeweavers.com> Date: Wed, 02 May 2012 16:45:31 +0200 --- dlls/jscript/jscript.h | 15 ++++++++++++++- dlls/jscript/regexp.c | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/dlls/jscript/jscript.h b/dlls/jscript/jscript.h index ad39b45..dc1d7fe 100644 --- a/dlls/jscript/jscript.h +++ b/dlls/jscript/jscript.h @@ -405,6 +405,19 @@ static inline DOUBLE num_val(const VARIANT *v) return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v); } +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif + +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif + +static inline BOOL is_int32(double d) +{ + return INT32_MIN <= d && d <= INT32_MAX && (int)(double)d == d; +} + static inline void num_set_int(VARIANT *v, INT i) { V_VT(v) = VT_I4; @@ -413,7 +426,7 @@ static inline void num_set_int(VARIANT *v, INT i) static inline void num_set_val(VARIANT *v, DOUBLE d) { - if(d == (DOUBLE)(INT)d) { + if(is_int32(d)) { V_VT(v) = VT_I4; V_I4(v) = d; }else { diff --git a/dlls/jscript/regexp.c b/dlls/jscript/regexp.c index 0cf1270..0f6b0e2 100644 --- a/dlls/jscript/regexp.c +++ b/dlls/jscript/regexp.c @@ -3531,7 +3531,7 @@ static INT index_from_var(script_ctx_t *ctx, VARIANT *v) } n = floor(n); - return (double)(INT)n == n ? n : 0; + return is_int32(n) ? n : 0; } static HRESULT RegExp_lastIndex(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,