Skip to content

tools: Skip the rest of the WAVE fmt chunk correctly - #93

Open
0z5a wants to merge 1 commit into
google:mainfrom
0z5a:fix/wave-fmt-chunk-skip
Open

0z5a wants to merge 1 commit into
google:mainfrom
0z5a:fix/wave-fmt-chunk-skip

Conversation

@0z5a

@0z5a 0z5a commented Nov 14, 2026

Copy link
Copy Markdown

wave_read_header() reads the 16-byte PCM part of the fmt chunk and then seeks by sizeof(format) - (8 + format.size) to get past the rest of the chunk. That offset is negative whenever the chunk is longer than 16 bytes, so the reader moves backwards instead of forwards:

  • A WAVEFORMATEX header (fmt chunk size 18, cbSize = 0), as written by several common tools, is rejected by elc3 with "Bad or unsupported WAVE input file".
  • A size of 40 (WAVE_FORMAT_EXTENSIBLE) only works by accident: the backward seek of 24 bytes lands on the start of the fmt chunk, which the data-chunk loop then skips as an unknown chunk.

The chunk loop also ignores the RIFF pad byte after an odd-sized chunk, so a file with, e.g., an odd-length LIST/junk chunk before data is rejected too.

This change seeks forward by the remaining bytes of the fmt chunk (plus the pad byte when its size is odd), skips the pad byte of odd-sized chunks in the loop, and rejects a fmt chunk shorter than 16 bytes, which the old code would have read past.

Testing (there is no test harness for tools/, so this was checked by hand): I generated the same 0.1 s 48 kHz mono 16-bit sine as four WAV files: canonical fmt 16, fmt 18 with cbSize = 0, fmt 16 with a 3-byte chunk (plus pad byte) before data, and WAVE_FORMAT_EXTENSIBLE (fmt 40). With elc3 -b 64000:

input before after
fmt 16 OK OK
fmt 18 rejected OK
odd-sized chunk before data rejected OK
fmt 40 (extensible) OK OK

All .lc3 outputs after the change are byte-identical to the canonical fmt 16 output, and the fmt 16 output is unchanged.

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

wave_read_header() seeks by sizeof(format) - (8 + format.size) after
reading the 16-byte fmt body, which moves backwards when the chunk is
longer. A WAVEFORMATEX header (fmt size 18, cbSize = 0) was then
rejected; size 40 (WAVE_FORMAT_EXTENSIBLE) only worked because the
backward seek lands on the start of the fmt chunk, which is then skipped
as an unknown chunk.

Seek forward by the remaining chunk bytes, honour the RIFF pad byte of
odd-sized chunks, and reject a fmt chunk shorter than 16 bytes.

This branch has not been deployed

No deployments
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