golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124k stars 17.67k forks source link

image/png: decoding of trailing IDAT chunks #14936

Closed tdilo closed 8 years ago

tdilo commented 8 years ago

Decoding of some PNG files generated by GDAL2Tiles (raster map tile generator) fails with the error message "not enough pixel data". Most other decoders can read these files without any issues.

Even though the error message suggests otherwise, the problematic files actually contain too much data. Following the expected IDAT chunks containing all of the image data, there is an additional zero-length IDAT chunk.

According to the PNG specification, zero-length IDAT chunks are valid. Trailing garbage may not be valid, but the spec explicitly allows ignoring of errors that have no effect on the processing of the image.

The error message "not enough pixel data" is generated after the decoder successfully decodes the image: it then encounters the trailing zero-length IDAT chunk and attempts to decode it as if it were the first IDAT chunk in the image.

No error is generated when the decoder encounters trailing IDAT chunks that contain actual image data, as long as that image data can be decoded with the PNG header (and palette) already seen. This allows users to craft images that trick the decoder into decoding a second image appended after the first.

See the image below, which most decoders will show as plain white, while image/png will happily reveal the gopher hidden within: hiddengopher

CL with proposed fix following.

gopherbot commented 8 years ago

CL https://golang.org/cl/21045 mentions this issue.