You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Undefined behavior when converting NaN to unsigned int in tinyexpr #133
While testing tinyexpr with UndefinedBehaviorSanitizer (UBSan), I found an undefined behavior. Some expressions produce NaN, which is then converted to unsigned int. In C, this is undefined behavior and can lead to unpredictable results.
Steps to Reproduce
1.Compile tinyexpr with UBSan: clang -fsanitize=undefined -g example.c tinyexpr.c -o example
2.Run with an expression that gives NaN, e.g.: (1/0)-(1/0)
3.UBSan reports: tinyexpr.c:144:52: runtime error: -nan is outside the range of representable values of type 'unsigned int'
Expected Behavior
The program should avoid converting NaN to integers. Possible fixes: check isnan()/isfinite(), keep NaN as float, or return an error.
Impact
Undefined behavior may not crash every time but can cause incorrect results or platform-specific issues.
changed the title [-]Undefined behavior when converting NaN to unsigned int during expression evaluation (detected by UBSan)[/-][+]Undefined behavior when converting NaN to unsigned int in tinyexpr[/+]on Dec 29, 2025
Tested locally with clang -fsanitize undefined. The UBSan warning no longer appears for the reported case.
Please let me know if any changes are needed.
Activity
[-]Undefined behavior when converting NaN to unsigned int during expression evaluation (detected by UBSan)[/-][+]Undefined behavior when converting NaN to unsigned int in tinyexpr[/+]codeplea commentedon Dec 29, 2025
Step 2 is not clear from your write up. Are you changing the code? If you are, you should give the complete code that causes the issue.
SouravVerma-art commentedon Feb 11, 2026
Tested locally with clang -fsanitize undefined. The UBSan warning no longer appears for the reported case.
Please let me know if any changes are needed.
Handle NaN arguments in fac and ncr, fixes codeplea#133