Browse Source

+ parse the rest of the function section

Nikolay Nikolov 1 year ago
parent
commit
f87a66f1e6
1 changed files with 26 additions and 2 deletions
  1. 26 2
      compiler/ogwasm.pas

+ 26 - 2
compiler/ogwasm.pas

@@ -2547,7 +2547,8 @@ implementation
 
 
         function ReadFunctionSection: Boolean;
         function ReadFunctionSection: Boolean;
           var
           var
-            FunctionsCount: uint64;
+            FunctionsCount, typidx: uint64;
+            i: Integer;
           begin
           begin
             Result:=False;
             Result:=False;
             if FunctionSectionRead then
             if FunctionSectionRead then
@@ -2571,6 +2572,25 @@ implementation
                 InputError('The functions count does not fit in an unsigned 32-bit int');
                 InputError('The functions count does not fit in an unsigned 32-bit int');
                 exit;
                 exit;
               end;
               end;
+            for i:=0 to FunctionsCount-1 do
+              begin
+                if not ReadUleb(AReader,typidx) then
+                  begin
+                    InputError('Error reading type index for function');
+                    exit;
+                  end;
+                if typidx>high(FFuncTypes) then
+                  begin
+                    InputError('Type index in the function section exceeds bounds of the types table');
+                    exit;
+                  end;
+              end;
+            if AReader.Pos<>(SectionStart+SectionSize) then
+              begin
+                InputError('Unexpected function section size');
+                exit;
+              end;
+            Result:=true;
           end;
           end;
 
 
         function ReadGlobalSection: Boolean;
         function ReadGlobalSection: Boolean;
@@ -2637,7 +2657,11 @@ implementation
                   exit;
                   exit;
                 end;
                 end;
             Byte(wsiFunction):
             Byte(wsiFunction):
-              Result := ReadFunctionSection;
+              if not ReadFunctionSection then
+                begin
+                  InputError('Error reading the function section');
+                  exit;
+                end;
             Byte(wsiGlobal):
             Byte(wsiGlobal):
               Result := ReadGlobalSection;
               Result := ReadGlobalSection;
             Byte(wsiExport):
             Byte(wsiExport):