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

Fix some minor undefined behaviours #1810

Merged
merged 3 commits into from
Jul 18, 2024
Merged

Conversation

jkbonfield
Copy link
Contributor

As reported by clang (gcc finds fewer).

Ie nasal demon avoidance.

The bit-wise AND, OR and XOR logic works on the overloaded double
(res->d), but casting this to int64_t for NAN is undefined behaviour.

Infact the current cast doesn't even yield the correct bit-wise layout
for NAN, as (long)d and *(long *)&d give different results for d==NAN.
However the result wasn't used anyway as it's promptly cleared again
in the subsequent `if (undef) hts_expr_val_undef(res)` code.  Hence
undefined or otherwise, it made no difference.
Obviously there can still be overflows if we attempt to parse numbers
which are too big, but for the legally accepted range of this, parsing
"-9,223,372,036,854,775,808" as tested in test/sam.c triggered a
problem as the positive version doesn't fit in "long long".

We parse as unsigned and only switch to signed via the implicit return
type conversion (and probably exploiting twos-complement, but that's a
fair assumption).
The pointer was never used, but the NULL+0 still triggers clang's ubsan.
@daviesrob daviesrob merged commit fbe5ff6 into samtools:develop Jul 18, 2024
9 checks passed
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

Successfully merging this pull request may close these issues.

2 participants