Skip to content

fix(crc): CRC_make_crc_32_table が table[255] を初期化しない#486

Merged
sksat (sksat) merged 1 commit into
mainfrom
fix/crc-32-table-off-by-one
Jun 12, 2026
Merged

fix(crc): CRC_make_crc_32_table が table[255] を初期化しない#486
sksat (sksat) merged 1 commit into
mainfrom
fix/crc-32-table-off-by-one

Conversation

@sksat

Copy link
Copy Markdown
Member

概要

CRC_make_crc_32_table の生成ループが for (i = 0; i < 255; ++i) になっており、table[255] が未初期化のまま残る。8bit版・16bit版はどちらも i < 256 であり、crc.h のコメントも「sizeof(table) = 256」と明記している。

for (i = 0; i < 255; ++i)   // ← 255 だと table[255] が埋まらない

影響

  • 現在 CRC_make_crc_32_table の呼び出し元はリポジトリ内にゼロのため実害は出ていない。
  • ただし将来 CRC-32 を使い始めると、0xFF で終わるバイト(テーブル index 255 を引く入力)の CRC が未初期化のゴミ値で計算され、壊れる。

修正

i < 255i < 256

検証

実ソース library/crc.c をそのままコンパイルした再現プログラムで確認:

  • 修正前: table[255] が番兵値のまま(未初期化1件)
  • 修正後: 未初期化0件、table[255] = 0x2d02ef8d(標準 CRC-32 テーブルの正規値)

備考

library/ の葉関数なので本来は unit test を付けたいが、C コードの unit test 実行基盤は #473 で導入予定。本PRは1行修正のみとし、リグレッションテストは #473 マージ後に同基盤上へ追加する。

🤖 Generated with Claude Code

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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

CRC_make_crc_32_table のテーブル生成ループが i < 255 になっていたため table[255] が未初期化となる不具合を修正し、CRC-32 テーブルが 256 要素すべて初期化されるようにする PR です(crc.h の「sizeof(table) = 256」および CRC-8/16 実装と整合)。

Changes:

  • CRC_make_crc_32_table の生成ループ条件を i < 255i < 256 に修正し、table[255] を確実に初期化

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sksat sksat (sksat) merged commit 2d718c0 into main Jun 12, 2026
36 checks passed
@sksat sksat (sksat) deleted the fix/crc-32-table-off-by-one branch June 12, 2026 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants