File tree Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ func decodeLEB128<IntType, Stream>(
35
35
36
36
@inlinable
37
37
func decodeLEB128< IntType, Stream> (
38
- stream: Stream
38
+ stream: Stream , bitWidth : Int = IntType . bitWidth
39
39
) throws -> IntType where IntType: FixedWidthInteger , IntType: RawSignedInteger , Stream: ByteStream {
40
40
let firstByte = try stream. consumeAny ( )
41
41
var result = IntType . Unsigned ( firstByte & 0b0111_1111 )
@@ -54,8 +54,8 @@ func decodeLEB128<IntType, Stream>(
54
54
result |= slice << shift
55
55
56
56
// When we don't have enough bit width
57
- if shift > ( IntType . bitWidth - 7 ) {
58
- let remainingBitWidth = IntType . bitWidth - Int( shift)
57
+ if shift > ( bitWidth - 7 ) {
58
+ let remainingBitWidth = bitWidth - Int( shift)
59
59
let continuationBit = ( byte & 0b1000_0000 ) != 0
60
60
// When a next byte is expected
61
61
if continuationBit {
Original file line number Diff line number Diff line change @@ -316,6 +316,10 @@ extension WasmParserError.Message {
316
316
@usableFromInline static func invalidResultArity( expected: Int , actual: Int ) -> Self {
317
317
Self ( " invalid result arity: expected \( expected) but got \( actual) " )
318
318
}
319
+
320
+ @usableFromInline static func invalidFunctionType( _ index: Int64 ) -> Self {
321
+ Self ( " invalid function type index: \( index) , expected a unsigned 32-bit integer " )
322
+ }
319
323
}
320
324
321
325
/// > Note:
@@ -344,6 +348,11 @@ extension ByteStream {
344
348
func parseSigned< T: FixedWidthInteger & RawSignedInteger > ( ) throws -> T {
345
349
try decodeLEB128 ( stream: self )
346
350
}
351
+
352
+ @usableFromInline
353
+ func parseVarSigned33( ) throws -> Int64 {
354
+ try decodeLEB128 ( stream: self , bitWidth: 33 )
355
+ }
347
356
}
348
357
349
358
/// > Note:
@@ -452,7 +461,11 @@ extension Parser {
452
461
case 0x7C ... 0x7F , 0x70 , 0x6F :
453
462
return try . type( parseValueType ( ) )
454
463
default :
455
- return try . funcType( TypeIndex ( stream. consumeAny ( ) ) )
464
+ let rawIndex = try stream. parseVarSigned33 ( )
465
+ guard let index = TypeIndex ( exactly: rawIndex) else {
466
+ throw makeError ( . invalidFunctionType( rawIndex) )
467
+ }
468
+ return . funcType( index)
456
469
}
457
470
}
458
471
You can’t perform that action at this time.
0 commit comments