Skip to content

Commit

Permalink
One more fuzz error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jul 21, 2024
1 parent 26e1d91 commit f5e3b3c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
7 changes: 6 additions & 1 deletion imagedecoder_png.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ func (e *imageDecoderPNG) decode() error {
if sources.Has(IPTC) {
sources = sources.Remove(IPTC)

dataLen := int(chunkLength) - int(profileNameLength)
if dataLen < 0 {
return newInvalidFormatErrorf("invalid data length %d", dataLen)
}

// TODO(bep) According to the spec, this should always return Latin-1 encoded text.
// The image editors out there does not seem to care much about this.
// See https://github.com/bep/imagemeta/issues/19
data, err := decompressZTXt(e.readBytesVolatile(int(chunkLength) - int(profileNameLength)))
data, err := decompressZTXt(e.readBytesVolatile(dataLen))
if err != nil {
return newInvalidFormatError(fmt.Errorf("decompressing zTXt: %w", err))
}
Expand Down
16 changes: 14 additions & 2 deletions imagemeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ func Decode(opts Options) (err error) {

defer func() {
if r := recover(); r != nil {
if errp := r.(error); errp != nil {
if errp, ok := r.(error); ok {
if isInvalidFormatErrorCandidate(errp) {
err = newInvalidFormatError(errp)
} else {
err = errp
if err != errStop {
printStackTrace(os.Stderr)
}
}
} else {
err = fmt.Errorf("unknown panic: %v", r)
printStackTrace(os.Stderr)
}
}

Expand Down Expand Up @@ -168,8 +174,14 @@ func Decode(opts Options) (err error) {
go func() {
defer func() {
if r := recover(); r != nil {
if errp := r.(error); errp != nil {
if errp, ok := r.(error); ok {
if errp != errStop {
printStackTrace(os.Stderr)
}
errc <- errp
} else {
errc <- fmt.Errorf("unknown panic: %v", r)
printStackTrace(os.Stderr)
}
}
}()
Expand Down
2 changes: 1 addition & 1 deletion metadecoder_exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (e *metaDecoderEXIF) decodeTag(namespace string) error {

size, ok := exifTypeSize[typ]
if !ok {
return fmt.Errorf("%w: unknown EXIF type %d", errInvalidFormat, typ)
return newInvalidFormatErrorf("unknown EXIF type %d", typ)
}
valLen := size * count

Expand Down
2 changes: 2 additions & 0 deletions testdata/fuzz/FuzzDecodePNG/419ad6feeb3d533e
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000000\x00\x00\x00\x10zTXtRaw profile type iptc\x00")

0 comments on commit f5e3b3c

Please sign in to comment.