Browse Source

+ verify the segment count when parsing the WASM_SEGMENT_INFO subsection
* moved local vars to avoid bug

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

+ 26 - 21
compiler/ogwasm.pas

@@ -2149,12 +2149,28 @@ implementation
 
 
     function TWasmObjInput.ReadObjData(AReader: TObjectreader; out ObjData: TObjData): boolean;
     function TWasmObjInput.ReadObjData(AReader: TObjectreader; out ObjData: TObjData): boolean;
 
 
+      var
+        SectionId: Byte;
+        SectionSize: uint32;
+        SectionStart: LongInt;
+        CheckSectionBounds: Boolean;
+
+        TypeSectionRead: Boolean = false;
+        ImportSectionRead: Boolean = false;
+        FunctionSectionRead: Boolean = false;
+        CodeSectionRead: Boolean = false;
+        DataSectionRead: Boolean = false;
+        DataCountSectionRead: Boolean = false;
+
+        DataSegments: array of record
+          Active: Boolean;
+          MemIdx: uint32;
+          Len: uint32;
+          Offset: int32;
+          DataPos: LongInt;
+        end;
+
       function ReadSection: Boolean;
       function ReadSection: Boolean;
-        var
-          SectionId: Byte;
-          SectionSize: uint32;
-          SectionStart: LongInt;
-          CheckSectionBounds: Boolean;
 
 
         function read(out b;len:longint):boolean;
         function read(out b;len:longint):boolean;
           begin
           begin
@@ -2265,22 +2281,6 @@ implementation
               result:=true;
               result:=true;
           end;
           end;
 
 
-        var
-          TypeSectionRead: Boolean = false;
-          ImportSectionRead: Boolean = false;
-          FunctionSectionRead: Boolean = false;
-          CodeSectionRead: Boolean = false;
-          DataSectionRead: Boolean = false;
-          DataCountSectionRead: Boolean = false;
-
-          DataSegments: array of record
-            Active: Boolean;
-            MemIdx: uint32;
-            Len: uint32;
-            Offset: int32;
-            DataPos: LongInt;
-          end;
-
         function ReadCustomSection: Boolean;
         function ReadCustomSection: Boolean;
 
 
           function ReadRelocationSection: Boolean;
           function ReadRelocationSection: Boolean;
@@ -2311,6 +2311,11 @@ implementation
                     InputError('Error reading the segment count from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
                     InputError('Error reading the segment count from the WASM_SEGMENT_INFO subsection of the ''linking'' section');
                     exit;
                     exit;
                   end;
                   end;
+                if SegmentCount<>Length(DataSegments) then
+                  begin
+                    InputError('Segment count in the WASM_SEGMENT_INFO subsection does not match the data count in the data section');
+                    exit;
+                  end;
                 for i:=0 to SegmentCount-1 do
                 for i:=0 to SegmentCount-1 do
                   begin
                   begin
                     if not ReadName(SegName) then
                     if not ReadName(SegName) then