Browse Source

Reader: validate the block size

Pierre.Curto 5 years ago
parent
commit
01ada88c17
2 changed files with 13 additions and 1 deletions
  1. 5 1
      frame.go
  2. 8 0
      options.go

+ 5 - 1
frame.go

@@ -129,7 +129,7 @@ func (fd *FrameDescriptor) initR(r *Reader) error {
 	fd.Flags = DescriptorFlags(descr)
 	if fd.Flags.Size() {
 		// Append the 8 missing bytes.
-		buf = buf[:11]
+		buf = buf[:3+8]
 		if _, err := io.ReadFull(r.src, buf[3:]); err != nil {
 			return err
 		}
@@ -140,6 +140,10 @@ func (fd *FrameDescriptor) initR(r *Reader) error {
 	if c := descriptorChecksum(buf); fd.Checksum != c {
 		return fmt.Errorf("%w: got %x; expected %x", ErrInvalidHeaderChecksum, c, fd.Checksum)
 	}
+	// Validate the elements that can be.
+	if !fd.Flags.BlockSizeIndex().isValid() {
+		return ErrOptionInvalidBlockSize
+	}
 	return nil
 }
 

+ 8 - 0
options.go

@@ -67,6 +67,14 @@ func (b BlockSize) index() BlockSizeIndex {
 
 type BlockSizeIndex uint8
 
+func (b BlockSizeIndex) isValid() bool {
+	switch b {
+	case 4, 5, 6, 7:
+		return true
+	}
+	return false
+}
+
 func (b BlockSizeIndex) get() []byte {
 	var buf interface{}
 	switch b {