Browse Source

+ parse the data count section

Nikolay Nikolov 1 year ago
parent
commit
bcb7c069fb
1 changed files with 35 additions and 1 deletions
  1. 35 1
      compiler/ogwasm.pas

+ 35 - 1
compiler/ogwasm.pas

@@ -2195,6 +2195,9 @@ implementation
           TypeSectionRead: Boolean = false;
           TypeSectionRead: Boolean = false;
           ImportSectionRead: Boolean = false;
           ImportSectionRead: Boolean = false;
           FunctionSectionRead: Boolean = false;
           FunctionSectionRead: Boolean = false;
+          DataCountSectionRead: Boolean = false;
+
+          DataCount: uint32;
 
 
         function ReadCustomSection: Boolean;
         function ReadCustomSection: Boolean;
           begin
           begin
@@ -2614,8 +2617,33 @@ implementation
           end;
           end;
 
 
         function ReadDataCountSection: Boolean;
         function ReadDataCountSection: Boolean;
+          var
+            v: uint64;
           begin
           begin
             Result:=False;
             Result:=False;
+            if DataCountSectionRead then
+              begin
+                InputError('Data count section is duplicated');
+                exit;
+              end;
+            DataCountSectionRead:=True;
+            if not ReadUleb(AReader,v) then
+              begin
+                InputError('Error reading the data count from the data count section');
+                exit;
+              end;
+            if v>high(uint32) then
+              begin
+                InputError('Data count does not fit in an unsigned 32-bit int');
+                exit;
+              end;
+            DataCount:=v;
+            if AReader.Pos<>(SectionStart+SectionSize) then
+              begin
+                InputError('Unexpected data count section size');
+                exit;
+              end;
+            Result:=true;
           end;
           end;
 
 
         begin
         begin
@@ -2671,7 +2699,13 @@ implementation
             Byte(wsiData):
             Byte(wsiData):
               Result := ReadDataSection;
               Result := ReadDataSection;
             Byte(wsiDataCount):
             Byte(wsiDataCount):
-              Result := ReadDataCountSection;
+              begin
+                if not ReadDataCountSection then
+                  begin
+                    InputError('Error reading the data count section');
+                    exit;
+                  end;
+              end
             else
             else
               begin
               begin
                 InputError('Unknown section: ' + ToStr(SectionId));
                 InputError('Unknown section: ' + ToStr(SectionId));