Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined behavior in bf_set_si #358

Open
cryptocode opened this issue Oct 7, 2024 · 1 comment
Open

Undefined behavior in bf_set_si #358

cryptocode opened this issue Oct 7, 2024 · 1 comment

Comments

@cryptocode
Copy link

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:

quickjs/libbf.c

Line 274 in 6e2e68f

ret = bf_set_ui(r, -a);

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;
}
cryptocode added a commit to cryptocode/zbf that referenced this issue Oct 7, 2024
@Emill
Copy link

Emill commented Oct 12, 2024

Changing to bf_set_ui(r, -(uint64_t)a) would be simpler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants