Skip to content

Commit 979e0cf

Browse files
committed
raise WasmParseError instead of ValueError on func/code length mismatch
zip(function_section, code_section, strict=True) surfaced a bare Python ValueError on mismatch, unlike every other malformed-input path in the parser, which raises WasmParseError.
1 parent f9a4272 commit 979e0cf

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

pykwasm/src/pykwasm/binary/module.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ def parse_module(s: InputStream) -> KInner:
335335
else:
336336
raise WasmParseError('Unexpected data after end of module')
337337

338+
if len(function_section) != len(code_section):
339+
raise WasmParseError(
340+
f'Function and code section length mismatch: {len(function_section)} != {len(code_section)}'
341+
)
342+
338343
functions = [
339344
wast.func(
340345
wast.KInt(typ_idx),

pykwasm/src/tests/unit/test_binary_parser.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ def test_trailing_garbage_after_sections_rejected(self) -> None:
208208
parse_module(stream(MAGIC + VERSION + type_section + b'\xff'))
209209

210210

211+
class TestFuncCodeLengthMismatch:
212+
def test_more_funcs_than_code_entries_rejected(self) -> None:
213+
func_section = section(3, uleb128(1) + uleb128(0))
214+
code_section = section(10, uleb128(0))
215+
with pytest.raises(WasmParseError):
216+
parse_module(stream(MAGIC + VERSION + func_section + code_section))
217+
218+
def test_more_code_entries_than_funcs_rejected(self) -> None:
219+
func_section = section(3, uleb128(0))
220+
code_section = section(10, uleb128(1) + (uleb128(2) + bytes([0x00, 0x0B])))
221+
with pytest.raises(WasmParseError):
222+
parse_module(stream(MAGIC + VERSION + func_section + code_section))
223+
224+
211225
def unwrap_instr(k: KApply) -> KApply:
212226
"""Helper: strip the `aInstrWithPos` position wrapper added by `instr()`."""
213227
assert k.label.name == 'aInstrWithPos'

0 commit comments

Comments
 (0)