Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Undefined-Behavior in function cJSON_SetNumberHelper

Open Beginner friendly
#1,100 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
tooling

Research direction

The bug is in cJSON_SetNumberHelper in cJSON.c. NaN fails both range comparisons, so it reaches the else branch and the (int)number cast is undefined behaviour. Read cJSON_CreateNumber, which the issue cites as the similar case, to see how NaN is handled there. Done when a NaN input no longer casts to int and the valueint result is defined, with a test added under tests/ covering NaN.

Written by the indexing model from the issue text.

Description

CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
{
    if (object == NULL)
    {
        return (double)NAN;
    }

    if (number >= INT_MAX)
    {
        object->valueint = INT_MAX;
    }
    else if (number <= (double)INT_MIN)
    {
        object->valueint = INT_MIN;
    }
    else
    {
        object->valueint = (int)number;
    }

    return object->valuedouble = number;
}

this UB is similar to that UB: cJSON_CreateNumber when input number is NaN
then cause NAN cast : int(NaN)

Dominant language
C
Stars
13k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DaveGamble/cJSON

All issues in DaveGamble/cJSON

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.