image-rs / image

Encoding and decoding images in Rust
Apache License 2.0
4.77k stars 590 forks source link

JPEG decoder: Failed to write whole buffer #761

Open vorot93 opened 6 years ago

vorot93 commented 6 years ago

The JPEG decoder fails cryprically with several of broken images like this one: std::io::read_exact() returns failed to read whole buffer error.

Here's the backtrace from GDB:

0  std::io::impls::<impl std::io::Read for &'a [u8]>::read_exact (self=0x7ffffffef4b0, buf=...) at /checkout/src/libstd/io/impls.rs:192
#1  0x0000555555745bfc in <std::io::cursor::Cursor<T> as std::io::Read>::read_exact (self=0x7fffffff4748, buf=...) at /checkout/src/libstd/io/cursor.rs:235
#2  0x00005555557466d9 in byteorder::io::ReadBytesExt::read_u8 (self=0x7fffffff4748) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/byteorder-1.2.2/src/io.rs:55
#3  0x000055555574c939 in jpeg_decoder::huffman::HuffmanDecoder::read_bits (self=0x7fffffff0db8, reader=0x7fffffff4748) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/jpeg-decoder-0.1.14/src/huffman.rs:125
#4  0x000055555574c1c5 in jpeg_decoder::huffman::HuffmanDecoder::decode (self=0x7fffffff0db8, reader=0x7fffffff4748, table=0x7ffff2830c00) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/jpeg-decoder-0.1.14/src/huffman.rs:30
#5  0x00005555556866eb in jpeg_decoder::decoder::decode_block (reader=0x7fffffff4748, coefficients=..., huffman=0x7fffffff0db8, dc_table=..., ac_table=..., spectral_selection=..., successive_approximation_low=0, eob_run=0x7fffffff0dd6, 
    dc_predictor=0x7fffffff0dc8) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/jpeg-decoder-0.1.14/src/decoder.rs:599
#6  0x000055555568a26c in <jpeg_decoder::decoder::Decoder<R>>::decode_scan (self=0x7fffffff4748, frame=0x7fffffff2740, scan=0x7fffffff2790, worker_chan=0x7fffffff20b0, produce_data=true)
    at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/jpeg-decoder-0.1.14/src/decoder.rs:458
#7  0x000055555568df17 in <jpeg_decoder::decoder::Decoder<R>>::decode_internal (self=0x7fffffff4748, stop_after_metadata=false) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/jpeg-decoder-0.1.14/src/decoder.rs:219
#8  0x0000555555690bce in <jpeg_decoder::decoder::Decoder<R>>::decode (self=0x7fffffff4748) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/jpeg-decoder-0.1.14/src/decoder.rs:122
#9  0x00005555557b060d in <image::jpeg::decoder::JPEGDecoder<R> as image::image::ImageDecoder>::read_image (self=0x7fffffff4748) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/image-0.18.0/./src/jpeg/decoder.rs:63
#10 0x00005555556bf987 in image::dynimage::decoder_to_image (codec=...) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/image-0.18.0/./src/dynimage.rs:472
#11 0x00005555556c1cf2 in image::dynimage::load (r=..., format=image::image::ImageFormat::JPEG) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/image-0.18.0/./src/dynimage.rs:631
#12 0x00005555557c3001 in image::dynimage::load_from_memory_with_format (buf=..., format=image::image::ImageFormat::JPEG) at /home/vorot93/.cargo/registry/src/github.com-1ecc6299db9ec823/image-0.18.0/./src/dynimage.rs:685
kaksmet commented 6 years ago

I can not reproduce this locally with the linked image.

Running $ cargo run --example decode -- ./store_cover.jpg -o ./store_cover.png inside a jpeg-decoder git clone completes successfully and the png output looks fine.

vorot93 commented 6 years ago

As it turns out the actual bytes received were incomplete because of an error in one of network crates. I have uploaded the version that consists of said bytes and fails - it is available through the same link.

Perhaps it should error anyway but not in such a cryptic way.