@@ -105,19 +105,46 @@ func (e MissingSectionError) Error() string {
105105 return fmt .Sprintf ("wasm: missing section %s" , SectionID (e ).String ())
106106}
107107
108+ type sectionsReader struct {
109+ lastSecOrder uint8 // previous non-custom sectionid
110+ m * Module
111+ }
112+
113+ func newSectionsReader (m * Module ) * sectionsReader {
114+ return & sectionsReader {m : m }
115+ }
116+
117+ func (s * sectionsReader ) readSections (r * readpos.ReadPos ) error {
118+ for {
119+ done , err := s .readSection (r )
120+ switch {
121+ case err != nil :
122+ return err
123+ case done :
124+ return nil
125+ }
126+ }
127+ }
128+
108129// reads a valid section from r. The first return value is true if and only if
109130// the module has been completely read.
110- func (m * Module ) readSection (r * readpos.ReadPos ) (bool , error ) {
111- var err error
112- var id uint32
131+ func (sr * sectionsReader ) readSection (r * readpos.ReadPos ) (bool , error ) {
132+ m := sr .m
113133
114134 logger .Println ("Reading section ID" )
115- id , err = leb128 . ReadVarUint32 ( r )
135+ id , err := r . ReadByte ( )
116136 if err == io .EOF {
117137 return true , nil
118138 } else if err != nil {
119139 return false , err
120140 }
141+ if id != uint8 (SectionIDCustom ) {
142+ if id <= sr .lastSecOrder {
143+ return false , fmt .Errorf ("wasm: sections must occur at most once and in the prescribed order" )
144+ }
145+ sr .lastSecOrder = id
146+ }
147+
121148 s := RawSection {ID : SectionID (id )}
122149
123150 logger .Println ("Reading payload length" )
@@ -204,7 +231,7 @@ func (m *Module) readSection(r *readpos.ReadPos) (bool, error) {
204231 return false , MissingSectionError (SectionIDFunction )
205232 }
206233 if len (m .Function .Types ) != len (s .Bodies ) {
207- return false , errors .New ("The number of entries in the function and code section are unequal" )
234+ return false , errors .New ("wasm: the number of entries in the function and code section are unequal" )
208235 }
209236 if m .Types == nil {
210237 return false , MissingSectionError (SectionIDType )
0 commit comments