Skip to content

fix null - #381

Merged
qicosmos merged 4 commits into
masterfrom
fix_null
Apr 21, 2026
Merged

qicosmos merged 4 commits into
masterfrom
fix_null

Conversation

@qicosmos

Copy link
Copy Markdown
Owner

No description provided.

@qicosmos qicosmos left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码审查

正确性问题(高优先级)

json_reader.hpp 第 193–197 行,null 处理逻辑存在缺陷:

  1. match<'u', 'l', 'l'> 在匹配失败时的行为取决于其内部实现——若它抛异常或 assert,那么输入 "nope" 这类非法 JSON 会触发非预期错误,而不是给出清晰的解析错误。需确认 match 的错误处理语义是否与此场景兼容。

  2. 更严重的是:此修改只处理了 std::string(或类似有 clear() 的类型)的 null,但没有处理其他类型(如 int、bool、自定义结构体)的 null。如果 JSON 中其他字段也出现 null,会走到后续的 match<'"'> 导致解析错误。这个修复是局部的,缺乏整体设计。

安全性问题

第 193 行只检查了 *it == 'n',没有验证后续字符是完整的 null。虽然 match 会做验证,但若 match 实现是 UB-silent(如直接解引用而不检查 end),在输入截断时存在越界风险。建议在调用 match 前确认剩余长度 ≥ 3。

代码风格

  • 注释 // handle JSON null: leave value empty and skip "null" 是英文,而项目其他注释风格需保持一致,问题不大。
  • 测试文件末尾缺少换行符(diff 最后一行提示),应补上。

测试覆盖

测试用例未覆盖以下场景:

  • null 作为顶层值
  • 非 string 类型字段(如 int)遇到 null 的行为
  • 输入为 "nul"(截断)时的错误处理

CMakeLists.txt

改动合理,为 test_cpp20 显式指定 C++20 标准,避免依赖全局设置,无问题。

总结:核心逻辑修复思路正确,但覆盖范围不足,仅对 string 类型有效,其他类型的 null 处理仍是空白,建议在更上层(字段分发层)统一处理 null 跳过逻辑。

@qicosmos qicosmos left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码审查

正确性问题(高优先级)

  1. num_v<U> 处理 null 不修改值(第84-87行):数值类型遇到 null 时直接 return,原始值保持不变。测试用例(第370行)期望 obj.i == 99,这依赖调用方提前初始化,行为隐式且脆弱。建议至少文档注释说明"null 保留原值"的语义,或显式赋 0。

  2. bool 类型处理 null 同样不修改值(第185-188行):与上同理,obj.b == true 只因默认值恰好是 true,并非 null 语义的明确表达。

  3. 字符串 null 处理后多余的 value.clear()(第205-210行):null 分支已 return,但非 null 路径在 match<'"'> 之后第212行仍有 value.clear(),逻辑上重复清空。虽不影响正确性,但略显冗余。

安全性问题

  1. match<'u','l','l'> 越界风险:三处 null 处理都先 ++it 再调用 match,需确认 match 内部对 it == end 有充分检查。若输入为 "n" 截断字符串,行为取决于 match 实现,建议审查 match 是否抛异常而非 UB。

代码风格

  1. 测试用例注释(第370行):CHECK(obj.b == true) 建议写成 CHECK(obj.b),更符合 C++ 惯用法。

  2. 测试文件末尾缺少换行符(diff 最后一行提示),应补上 \n。

构建配置

  1. CMakeLists.txt 第110行:set_target_properties 放在 add_executable 之后紧跟,位置合理,但其他 target 未统一设置 C++ 标准,若项目全局已有 CMAKE_CXX_STANDARD,此处可能产生覆盖歧义,建议确认是否必要。

总结:核心逻辑可用,但 null 语义(保留原值 vs 置零)未在接口层明确约定,是潜在的语义陷阱,建议补充文档或统一行为。

@qicosmos qicosmos left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码审查

正确性问题(高优先级)

  1. 数值类型 null 处理不一致(json_reader.hpp ~84行):from_json_impl 对 num_v<U> 类型遇到 null 时直接 return,但 value 未被修改,保留了调用前的旧值。测试用例(mixed_t 的 i=99, b=true)依赖这个"保留默认值"的行为,但这是未定义的副作用,不是显式设计——应明确文档说明或将 value 置零。

  2. bool 类型走错分支(json_reader.hpp ~182行):bool 的 from_json_impl 是通过 switch(*it) 处理的,新增的 case 'n' 位置在 IGUANA_UNLIKELY default 之前,逻辑上正确。但 ++it 后调用 match<'u','l','l'> 若失败会抛异常,行为与数值分支一致,可以接受。

  3. 字符串 null 处理的 value.clear() 冗余(json_reader.hpp ~205行):null 分支调用 value.clear() 后 return,而非 null 路径在 match<'"'> 之后也会 value.clear()。逻辑正确,但 null 分支的 clear() 对于刚构造的对象是多余操作,影响不大。

安全性问题

  1. match 失败抛异常但迭代器已推进:三处 ++it 后调用 match,若 match 抛出异常,迭代器已被修改,调用方无法从异常中恢复并重试解析。这是现有框架的通病,本 PR 未引入新问题,但值得注意。

代码风格

  1. 测试文件末尾缺少换行(test_cpp20.cpp 最后一行):diff 明确标注 \ No newline at end of file,应补上,符合 POSIX 规范。

  2. 测试用例命名过长:"test null string field - json null to std::string yields empty" 描述了实现细节,建议改为 "test json null handling"。

CMakeLists.txt

  1. set_target_properties 放在 add_executable 之后紧跟,位置合理,修复了 test_cpp20 可能未启用 C++20 的问题,改动正确。

总结:主要风险是数值类型 null 时"静默保留旧值"的行为未文档化,建议在注释中明确说明这是有意为之的语义(null = no-op),否则容易引发误解。

@qicosmos qicosmos left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码审查

正确性问题(高优先级)

json_reader.hpp +84:数值类型遇到 null 时直接 return,value 未被赋任何值,保留了调用前的旧值。这与测试用例的预期(保留默认值)在语义上是"碰巧正确",但行为未明确定义,容易误导使用者。建议加注释说明"null 时保留原值"是有意为之。

json_reader.hpp +185:bool 类型处理 null 时同上,value 未被修改。测试 CHECK(obj.b == true) 能通过只是因为默认值恰好是 true,并非 null 被正确处理。

json_reader.hpp +202:字符串类型处理 null 时调用了 value.clear(),但 match<'"'> 在 null 分支已经 return,不会执行到后面的 value.clear()(第 207 行)。逻辑上没问题,但 value.clear() 重复出现两次(null 分支一次,正常路径一次),略显冗余,可读性稍差。

安全性

match<'u', 'l', 'l'> 内部应该会做边界检查,但三处 null 处理都依赖它抛异常来防御截断输入(如 "nu"),建议确认 match 在 it == end 时确实抛出,否则存在越界风险。

代码风格

  • 三处 null 处理逻辑高度重复,建议提取为 try_match_null(it, end) 内联函数,返回 bool,减少重复代码。
  • 测试文件末尾缺少换行符(diff 最后一行提示),建议补上,避免部分工具告警。

CMakeLists.txt

set_target_properties 放在 add_executable 之后是正确做法,无问题。

总结:核心逻辑可用,但 null 对数值/布尔类型"静默忽略"的语义需要文档或注释明确,重复代码建议重构。

@qicosmos
qicosmos merged commit 5e68e09 into master Apr 21, 2026
19 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
iguana 1.2.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* for windows compile by @qicosmos in qicosmos/iguana#375
* fix bug: heap-buffer-overflow by @bbbgan in qicosmos/iguana#378
* fix #362 ; support YLT_REFL_PB to replace YLT_REFL and get_members_impl by @bbbgan in qicosmos/iguana#379
* bugfix: fix parsing error with large field numbers; skip unknown fields by @bbbgan in qicosmos/iguana#380
* fix null by @qicosmos in qicosmos/iguana#381
* add c++26 reflection by @qicosmos in qicosmos/iguana#383
* rename and improve by @qicosmos in qicosmos/iguana#384
* Sync ylt reflection fixes by @qicosmos in qicosmos/iguana#385


**Full Changelog**: https://github.com/qicosmos/iguana/compare/1.1.0...1.2.0</pre>
  <p>View the full release notes at <a href="https://github.com/qicosmos/iguana/releases/tag/1.2.0">https://github.com/qicosmos/iguana/releases/tag/1.2.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!12400
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