Skip to content

fix: 建议增加 getString 方法 - #18

Open
webbrain-one wants to merge 1 commit into
CPythoner:masterfrom
webbrain-one:webbrain/issue-4
Open

webbrain-one wants to merge 1 commit into
CPythoner:masterfrom
webbrain-one:webbrain/issue-4

Conversation

@webbrain-one

Copy link
Copy Markdown

Closes #4

Add string extraction helpers by length and by index/length.
@CPythoner

Copy link
Copy Markdown
Owner

PR Review: getString 方法

Scope Check: CLEAN —— issue #4 要的就是这两个方法,没多没少,单 commit 单文件 +16。

✅ 做得好的地方

  • 完全复刻了 getBytes(ByteBuffer.h:193-207)的守卫风格和签名约定,一致性没问题
  • 用 (pointer, len) 构造 std::string,内嵌 \0 安全(已实测验证)
  • 失败时返回空串且 position_ 不前进,与 getBytes 行为一致
  • 两个重载的 const 正确性都对,编译干净无新增警告

🔴 P1(置信度 10/10)— 整数溢出可绕过边界检查

新增的守卫:

if (buffer_.empty() || position_ + len > limit_)

position_、len、limit_ 都是 uint32_t。当 position_ = 1, len = 4294967295 时,1 + UINT32_MAX 回绕为 0,守卫被绕过,随后 std::string(data()+1, 4294967295) 会尝试构造 4GB 字符串并越界读取——崩溃或 DoS。实测证实:

guard math: 1 + 4294967295 = 0, > 4 ? BYPASSED

getString 的 len 典型来源是协议里的长度字段(先 getInt 读长度再 getString(len)),正是不可信输入。修法很简单:

if (position_ > limit_ || len > limit_ - position_)
    return std::string();

(index 版本同理:if (index > limit_ || len > limit_ - index)。)

注意:getBytes 和 read<T> 有同样的历史问题,本 PR 是照抄了既有模式——至少新代码要修对,建议顺手全修。

🟡 P2 — 缺测试

Test.cpp 里有 90+ 个 Catch2 用例,每个 getter 都有 [indexed] 测试,CI 在 3 平台 × 2 构建类型跑 ctest。建议至少补:顺序读取 + position 前进、index 变体、越界返回空、空 buffer。

🟡 P3 — 缺文档

README.md 和 README_CN.md 都有完整 API 表格(159-183 行区域),getString 两个重载需要补进两边。

🔵 Nit — 插入位置破坏了配对约定

代码插在 getDouble() 和 getDouble(uint32_t index) 中间。全文件其他类型都是"无参版紧跟 index 版"成对出现,建议移到 getDouble(uint32_t index) 之后。

🔵 Nit(informational)— 缺 putString 对称方法

issue #4 只要求 getString,不算 scope 问题。但此库每个读方法都有对应写方法,写字符串目前只能 putBytes(reinterpret_cast...),后续可考虑补上。


结论:Request Changes。 功能实现正确(正常路径已实测),但 P1 溢出是真实的安全问题,加上缺测试缺文档,不建议按原样合入。修掉 P1 + 补测试 + 补 README 后即可 approve。

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.

建议增加 getString 方法

2 participants