Skip to content

Commit

Permalink
Fix off-by-one error in ParseSection_Export() (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
atdrendel authored Feb 21, 2021
1 parent c4bb509 commit d1c37d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/CWasm3/m3_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ _ (ReadLEB_u32 (& index, & i_bytes, i_end));
if (exportKind == d_externalKind_function)
{
u16 numNames = io_module->functions [index].numNames;
if (numNames < d_m3MaxDuplicateFunctionImpl - 1)
if (numNames < d_m3MaxDuplicateFunctionImpl)
{
io_module->functions [index].numNames++;
io_module->functions [index].names[numNames] = utf8;
Expand Down
11 changes: 9 additions & 2 deletions Tests/CWasm3Tests/CWasm3Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ final class CWasm3Tests: XCTestCase {
var constant2Function: IM3Function?
XCTAssertNil(m3_FindFunction(&constant2Function, runtime, "constant_2"))

[constant1Function, constant2Function]
var constant3Function: IM3Function?
XCTAssertNil(m3_FindFunction(&constant3Function, runtime, "constant_3"))

var constant4Function: IM3Function?
let err = try XCTUnwrap(m3_FindFunction(&constant4Function, runtime, "constant_4"))
XCTAssertEqual("function lookup failed", String(cString: err))

[constant1Function, constant2Function, constant3Function]
.forEach { (function) in
guard function != nil else { return XCTFail() }
let size = UnsafeMutablePointer<Int>.allocate(capacity: 1)
Expand Down Expand Up @@ -292,7 +299,7 @@ extension CWasm3Tests {

private func constantWasm() throws -> Array<UInt8> {
let base64 =
"AGFzbQEAAAABBQFgAAF/AwIBAAcbAgpjb25zdGFudF8xAAAKY29uc3RhbnRfMgAACggBBgBBgIAECw=="
"AGFzbQEAAAABBQFgAAF/AwIBAAc1BApjb25zdGFudF8xAAAKY29uc3RhbnRfMgAACmNvbnN0YW50XzMAAApjb25zdGFudF80AAAKCAEGAEGAgAQL"
guard let data = Data(base64Encoded: base64)
else { throw TestError.couldNotDecodeWasm("constant.wasm") }
return Array<UInt8>(data)
Expand Down
4 changes: 3 additions & 1 deletion Tests/CWasm3Tests/Resources/constant.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
(func (;0;) (type 0) (result i32)
i32.const 65536)
(export "constant_1" (func 0))
(export "constant_2" (func 0)))
(export "constant_2" (func 0))
(export "constant_3" (func 0))
(export "constant_4" (func 0)))

0 comments on commit d1c37d0

Please sign in to comment.