Rust codegen: RustEnumGenerator int enum deserialization silently accepts NaN, fractional, and out-of-range floats

未关闭 适合新手
#248 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
79/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust, swift
领域
backend, mobile

调研方向

阅读 compiler/compiler/Compiler/Sources/Generation/Rust/RustEnumGenerator.swift,尤其是 RustEnumGenerator.write() 及其 value.as_number()? as i32 转换。检查生成的 from_valdi 反序列化如何报告无效值,然后在强制转换前添加对有限性、整数值和 i32 范围的验证。完成标准是:NaN、无穷值、小数值和超出范围的浮点数会返回错误,同时有效的整数枚举值仍可正常反序列化。

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

描述

Found while reviewing https://github.com/Snapchat/Valdi/pull/211 (native Rust code generation). Not blocking that PR.

RustEnumGenerator int enum deserialization silently accepts NaN, fractional, and out-of-range floats

File: compiler/compiler/Compiler/Sources/Generation/Rust/RustEnumGenerator.swift

When JavaScript passes NaN, Infinity, an out-of-range float, or a fractional number (such as 4.5) for an integer @ExportEnum, Rust's from_valdi silently coerces it into a valid variant instead of returning an error. RustEnumGenerator.write() matches on value.as_number()? as i32.

In Rust, f64 as i32 is a truncating and saturating cast: NaN as i32 evaluates to 0 (matching any 0 enum variant), 4.5 as i32 truncates to 4, and out-of-range/infinite values saturate to i32::MIN or i32::MAX.

Validate that the f64 from value.as_number()? is finite, has fract() == 0.0, and lies within f64::from(i32::MIN)..=f64::from(i32::MAX) before casting to i32.

主要语言
C++
星标
16.4k
派生
539
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

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

Snapchat/Valdi 的其他 Issue

查看 Snapchat/Valdi 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

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