@@ -10,10 +10,12 @@ import (
10
10
)
11
11
12
12
var (
13
- errSmallBlock = errors .New ("buffer too small to contain block" )
14
- errMagic0 = errors .New ("first word is not magic0 word \" UF2\\ n\" " )
15
- errMagic1 = errors .New ("second word is not magic1 word" )
16
- errMagicEnd = errors .New ("last word is not magic end word" )
13
+ errSmallBlock = errors .New ("buffer too small to contain block" )
14
+ errPayload = errors .New ("payload size exceeds max UF2 block size" )
15
+ errBlockNumbering = errors .New ("block number larger than number of blocks" )
16
+ errMagic0 = errors .New ("first word is not magic0 word \" UF2\\ n\" " )
17
+ errMagic1 = errors .New ("second word is not magic1 word" )
18
+ errMagicEnd = errors .New ("last word is not magic end word" )
17
19
)
18
20
19
21
const (
@@ -89,9 +91,11 @@ func (b *Block) Data() ([]byte, error) {
89
91
func (b * Block ) Validate () error {
90
92
sz := b .PayloadSize
91
93
if sz > BlockMaxData {
92
- return errors . New ( "payload size exeeds permissible maximum" )
94
+ return errPayload
93
95
} else if sz == 0 {
94
96
return errors .New ("zero payload size" )
97
+ } else if b .BlockNum >= b .NumBlocks {
98
+ return errBlockNumbering
95
99
}
96
100
return nil
97
101
}
@@ -128,11 +132,15 @@ func DecodeAppendBlocks(dst []Block, r io.Reader, scratchBuf []byte) ([]Block, i
128
132
129
133
// DecodeBlock decodes a 512 byte block from the argument buffer. Buffer must be at least 512 bytes long.
130
134
func DecodeBlock (text []byte ) (Block , error ) {
131
- err := ValidateBlock (text )
135
+ err := validateMagic (text )
132
136
if err != nil {
133
137
return Block {}, err
134
138
}
135
139
block := MustDecodeBlock (text )
140
+ err = block .Validate ()
141
+ if err != nil {
142
+ return Block {}, err
143
+ }
136
144
return block , nil
137
145
}
138
146
@@ -149,7 +157,7 @@ func MustDecodeBlock(text []byte) (block Block) {
149
157
return block
150
158
}
151
159
152
- func ValidateBlock (text []byte ) error {
160
+ func validateMagic (text []byte ) error {
153
161
if len (text ) < 512 {
154
162
return errSmallBlock
155
163
}
0 commit comments