fix(library): endian/crc の2件の潜在バグ修正 (size_t アンダーフロー / CRC-32 テーブル未初期化)#485
Closed
sksat (sksat) wants to merge 2 commits into
Closed
fix(library): endian/crc の2件の潜在バグ修正 (size_t アンダーフロー / CRC-32 テーブル未初期化)#485sksat (sksat) wants to merge 2 commits into
sksat (sksat) wants to merge 2 commits into
Conversation
The 32-bit table generator looped `for (i = 0; i < 255; ++i)`, leaving table[255] uninitialized, while the 8-bit and 16-bit variants correctly loop to 256. crc.h documents the table as 256 entries. Any CRC-32 over a byte ending in 0xff would have used a garbage table entry. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The empty-input guard was `if (size < 0)`, which is always false for the unsigned size_t parameter. With size==0, the subsequent `size--` wraps to SIZE_MAX and the reversal loop writes far out of bounds (observed SIGSEGV on little-endian builds where ENDIAN_memcpy delegates to ENDIAN_conv). Guard the empty case explicitly so it behaves like memcpy(dst, src, 0). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
library/ 内の葉関数で見つかった再現可能な潜在バグ 2 件(size_t アンダーフローによる範囲外書き込み、および CRC-32 テーブルの未初期化要素)を、いずれも 1 行修正で是正するPRです。
Changes:
ENDIAN_convにsize == 0のガードを追加し、size--によるsize_tアンダーフローと範囲外書き込みを防止CRC_make_crc_32_tableのループ条件をi < 256に修正し、table[255]の未初期化を解消
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| library/endian.c | size == 0 を早期 return してアンダーフローと OOB 書き込みを防止 |
| library/crc.c | CRC-32 テーブル生成の off-by-one を修正して 256 要素すべて初期化 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
c2a-core 全体のバグ調査の中で、
library/の葉関数に再現可能な潜在バグを2件発見したので修正する。どちらも 1 行修正。実ソースを直接コンパイルした再現コードで「壊れる→直る」を確認済み。1.
endian.cENDIAN_conv: size==0 で size_t アンダーフロー → 範囲外書き込み空入力ガードのつもりの
size < 0が符号なし型で常に false。size==0を渡すとsize--がSIZE_MAXになり、反転ループが範囲外へ書き込む。ENDIAN_memcpyの実体であり「エンディアン考慮版 memcpy」と銘打たれているのに、memcpy(dst,src,0)なら no-op で済む入力でクラッシュする。IS_LITTLE_ENDIAN定義時(SILS/テストビルド)にENDIAN_memcpy → ENDIAN_conv経由で発火。現行のコア内呼び出し元は固定の非ゼロサイズのみだが、可変長を渡す将来の呼び出しで 0 になると即死する地雷。if (size < 0)→if (size == 0)。2.
crc.cCRC_make_crc_32_table: オフバイワンで table[255] 未初期化8bit/16bit 版は
i < 256なのに 32bit 版だけ255で、table[255]が未初期化のまま残る(crc.hのコメントも「sizeof(table) = 256」と明記)。CRC_make_crc_32_tableの呼び出し元はゼロのため実害は出ていないが、将来 CRC-32 を使い始めると 0xFF で終わるバイトの CRC が壊れる。i < 255→i < 256。修正後table[255]は標準 CRC-32 の正規値0x2d02ef8dになることを確認。検証
実ソース(
library/endian.c/library/crc.c)をそのままコンパイルした再現プログラムで確認:ENDIAN_conv(dst,src,0)で SIGSEGV → 修正後は no-op で正常リターン(size=4のバイト反転は維持)table[255] = 0x2d02ef8d備考
library/の葉関数(依存ほぼ無し)なので、本来は unit test を付けたいが、C コードの unit test 実行基盤は test: add DriverSuper unit tests #473 で導入予定。本PRは 2 行修正のみとし、リグレッションテストは test: add DriverSuper unit tests #473 マージ後に同じ cargo↔ctest 基盤の上へ追加する。🤖 Generated with Claude Code