quickjs
quickjs copied to clipboard
Undefined behavior in bf_set_si
When passing INT64_MIN to bf_set_si I experience a sigtrap on my system. I believe this is due to undefined behavior on this line: https://github.com/bellard/quickjs/blob/6e2e68fd0896957f92eb6c242a2e048c1ef3cae0/libbf.c#L274
as this will attempt to negate INT64_MIN
Might need a special case, possibly something like this:
int bf_set_si(bf_t *r, int64_t a)
{
int ret;
// Special case as -INT64_MIN is undefined
if (a == INT64_MIN) {
ret = bf_set_ui(r, (uint64_t)INT64_MAX + 1);
r->sign = 1;
} else if (a < 0) {
ret = bf_set_ui(r, -a);
r->sign = 1;
} else {
ret = bf_set_ui(r, a);
}
return ret;
}
Changing to bf_set_ui(r, -(uint64_t)a) would be simpler.
bf_set_ui() no longer exists.