Skip to content

Undefined behavior when converting NaN to unsigned int in tinyexpr #133

Closed

Description

@dreamlike181029-ux

Description

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.

Environment

OS: Ubuntu 22.04
Compiler: clang 14 (-fsanitize=undefined -g)
Tool: UBSan, fuzzing with libFuzzer

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.

Activity

  1. 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
  2. codeplea commented on Dec 29, 2025

    @codeplea
    Owner

    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.

  3. SouravVerma-art commented on Feb 11, 2026

    @SouravVerma-art

    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.

  4. added a commit that references this issue on Aug 5, 2026
    9108148
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions