Skip to content

Commit

Permalink
Fix slice index out of range panic possibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Oct 6, 2019
1 parent 3b3d620 commit 3177fc1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/matchers/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ func Json(in []byte) bool {
//
// BUG(gabriel-vasile): The "type" key should be searched for in the root object.
func GeoJson(in []byte) bool {
in = trimLWS(in)
if len(in) == 0 {
return false
}
in = trimLWS(in)
// geojson is always an object
if in[0] != '{' {
return false
Expand Down
13 changes: 6 additions & 7 deletions internal/matchers/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ func isMatroskaFileTypeMatched(in []byte, flType string) bool {
// The logic of search is: find first instance of \x42\x82 and then
// search for given string after one byte of above instance.
func isFileTypeNamePresent(in []byte, flType string) bool {
var ind int
if len(in) >= 4096 { // restricting length to 4096
ind = bytes.Index(in[0:4096], []byte("\x42\x82"))
} else {
ind = bytes.Index(in, []byte("\x42\x82"))
ind, maxInd, lenIn := 0, 4096, len(in)
if lenIn < maxInd { // restricting length to 4096
maxInd = lenIn
}
if ind > 0 {
ind = bytes.Index(in[:maxInd], []byte("\x42\x82"))
if ind > 0 && lenIn > ind+3 {
// filetype name will be present exactly
// one byte after the match of the two bytes "\x42\x82"
return bytes.HasPrefix(in[ind+3:], []byte(flType))
Expand All @@ -52,7 +51,7 @@ func Flv(in []byte) bool {

// Mpeg matches a Moving Picture Experts Group file.
func Mpeg(in []byte) bool {
return bytes.HasPrefix(in, []byte{0x00, 0x00, 0x01}) &&
return len(in) > 3 && bytes.HasPrefix(in, []byte{0x00, 0x00, 0x01}) &&
in[3] >= 0xB0 && in[3] <= 0xBF
}

Expand Down

0 comments on commit 3177fc1

Please sign in to comment.