Skip to content

Commit

Permalink
Fix UB in JS_NewFloat64 and JS_ToArrayLengthFree
Browse files Browse the repository at this point in the history
  • Loading branch information
chqrlie committed Mar 3, 2024
1 parent 1cd34b7 commit b289b81
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 2 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11078,6 +11078,8 @@ static __exception int JS_ToArrayLengthFree(JSContext *ctx, uint32_t *plen,
if (JS_TAG_IS_FLOAT64(tag)) {
double d;
d = JS_VALUE_GET_FLOAT64(val);
if (!(d >= 0 && d <= UINT32_MAX))
goto fail;
len = (uint32_t)d;
if (len != d)
goto fail;
Expand Down
20 changes: 9 additions & 11 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,23 +550,21 @@ JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v);

static js_force_inline JSValue JS_NewFloat64(JSContext *ctx, double d)
{
JSValue v;
int32_t val;
union {
double d;
uint64_t u;
} u, t;
u.d = d;
val = (int32_t)d;
t.d = val;
/* -0 cannot be represented as integer, so we compare the bit
representation */
if (u.u == t.u) {
v = JS_MKVAL(JS_TAG_INT, val);
} else {
v = __JS_NewFloat64(ctx, d);
if (d >= INT32_MIN && d <= INT32_MAX) {
u.d = d;
val = (int32_t)d;
t.d = val;
/* -0 cannot be represented as integer, so we compare the bit
representation */
if (u.u == t.u)
return JS_MKVAL(JS_TAG_INT, val);
}
return v;
return __JS_NewFloat64(ctx, d);
}

static inline JS_BOOL JS_IsNumber(JSValueConst v)
Expand Down

0 comments on commit b289b81

Please sign in to comment.