Skip to content

Commit

Permalink
Add ubsan handling for double cast
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed May 28, 2024
1 parent bc2c582 commit 885d20d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,16 @@ static js_force_inline JSValue JS_NewUint32(JSContext *ctx, uint32_t val)
JSValue JS_NewBigInt64(JSContext *ctx, int64_t v);
JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v);

#ifdef STRICT_R_HEADERS
// QJS tries to cast to int32_t to see if the value can be represented with
// less memory, but this results in undefined-beheaviour sanitizer errors.
// As this is intentional, disable the sanitizer for this function.
__attribute__((no_sanitize("undefined")))
static inline int32_t try_double_to_int32(double d) {
return (int32_t)d;
}
#endif

static js_force_inline JSValue JS_NewFloat64(JSContext *ctx, double d)
{
JSValue v;
Expand All @@ -577,7 +587,11 @@ static js_force_inline JSValue JS_NewFloat64(JSContext *ctx, double d)
uint64_t u;
} u, t;
u.d = d;
#ifdef STRICT_R_HEADERS
val = try_double_to_int32(d);
#else
val = (int32_t)d;
#endif
t.d = val;
/* -0 cannot be represented as integer, so we compare the bit
representation */
Expand Down

0 comments on commit 885d20d

Please sign in to comment.