Skip to content

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