Skip to content

Fix undefined behavior in ncr() when casting NaN/Inf to unsigned int#135

Open
SouravVerma-art wants to merge 1 commit intocodeplea:masterfrom
SouravVerma-art:fix-ub-ncr-nan-cast
Open

Fix undefined behavior in ncr() when casting NaN/Inf to unsigned int#135
SouravVerma-art wants to merge 1 commit intocodeplea:masterfrom
SouravVerma-art:fix-ub-ncr-nan-cast

Conversation

@SouravVerma-art
Copy link

Fix undefined behavior in ncr() when converting NaN/Inf to unsigned int

While testing tinyexpr with UBSan (-fsanitize=undefined),
I encountered undefined behavior when NaN values were cast
to unsigned int in ncr().

The issue occurs when expressions evaluate to NaN or Inf,
which then reach ncr() and are converted to unsigned types.
According to the C standard, this conversion is undefined behavior.

This patch adds a guard using isfinite() before casting
to prevent undefined behavior.

Tested with:
clang -fsanitize=undefined -g example.c tinyexpr.c -o example
Ubuntu 22.04

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.

1 participant