Invalid floating point exception with cJSON_CreateNumber(NAN)

未关闭
#891 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
c
领域
backend

调研方向

从 cJSON_CreateNumber 入口点开始,重现提供的 FE_INVALID/NAN 示例。检查现有的数字转换行为,然后验证 NaN 处理能够避免异常,并在保留报告的 double 值的同时,为 valueint 提供一个有定义的值。

由索引模型根据 Issue 内容生成。

描述

Calling cJSON_CreateNumber(NAN) raises a FE_INVALID floating point exception.

Here is a sample code that shows the problem:

#define _GNU_SOURCE

#include "cJSON.h"

#include <fenv.h>
#include <math.h>
#include <stdio.h>

int main()
{
    feenableexcept(FE_INVALID); // Crashes on FE_INVALID.
    cJSON *n = cJSON_CreateNumber(NAN);
    printf("n: %d %f\n", n->valueint, n->valuedouble);
    cJSON_Delete(n);
}

While I guess this is technically not a bug, I think it would be nice if the library tested for NAN in cJSON_CreateNumber to avoid this, and also set the int value to a constant. Maybe something like:

        item->type = cJSON_Number;
        item->valuedouble = num;

        if (isnan(num)) {
            item->valueint = 0;
        }
        /* use saturation in case of overflow */
        else if (num >= INT_MAX)
       ...
主要语言
C
星标
13.1k
派生
3.6k
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

DaveGamble/cJSON 的其他 Issue

查看 DaveGamble/cJSON 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。