-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
image/png: limit memory Decode can use while decoding #12512
Comments
It does have such a safety switch already: http://golang.org/pkg/image/png/#DecodeConfig I'm not sure there's anything to be done here that wouldn't be redundant with that. You could imagine a new |
(this applies to all image decoders, really... not just png) |
Ah, yes. It seems there's no way to continue reading the entire image from the same reader without seeking to the start of the stream, though. |
There is a way, once you learn the rest of the standard library. :-) Imagine you have a var header bytes.Buffer
conf, err := png.DecodeConfig(io.TeeReader(streamReader, &header))
if err != nil || !confValid(conf) { ... }
im, err := png.Decode(io.MultiReader(&header, streamReader) See the docs for io.TeeReader and io.MultiReader to see how that works. |
@bradfitz |
With the DEFLATE compression used by the PNG image format one can compress a 50 gigapixel image to a 6 Mb PNG file. When decoding such a file,
png.Decode
throws afatal error: runtime: out of memory
.image/png
should provide a safety switch that allows the user to limit the amount of memory thatpng.Decode
will use.Test case: https://github.com/opennota/spark
Reference: https://www.bamsoftware.com/hacks/deflate.html
The text was updated successfully, but these errors were encountered: