golang/src/image
Nigel Tao e7aeeae0c8 image/jpeg: ignore garbage bytes before a RST marker
Well-formed JPEG images will not have garbage bytes. However, for
corrupted JPEG images, the RST (restart) mechanism is specifically
designed so that a decoder can re-synchronize to an upcoming restartable
MCU (Minimum Coded Unit, e.g. 16x16 block of pixels) boundary and resume
decoding. Even if the resultant image isn't perfect, a 98%-good image is
better than a fatal error.

Every JPEG marker is encoded in two bytes, the first of which is 0xFF.
There are 8 possible RST markers, cycling as "0xFF 0xD0", "0xFF 0xD1",
..., "0xFF 0xD7". Suppose that, our decoder is expecting "0xFF 0xD1".

Before this commit, Go's image/jpeg package would accept only two
possible inputs: a well-formed "0xFF 0xD1" or one very specific pattern
of spec non-compliance, "0xFF 0x00 0xFF 0xD1".

After this commit, it is more lenient, similar to libjpeg's jdmarker.c's
next_marker function.
2dfe6c0fe9/jdmarker.c (L892-L935)

The new testdata file was created by:

$ convert video-001.png a.ppm
$ cjpeg -restart 2 a.ppm > video-001.restart2.jpeg
$ rm a.ppm

Fixes #40130

Change-Id: Ic598a5f489f110d6bd63e0735200fb6acac3aca3
Reviewed-on: https://go-review.googlesource.com/c/go/+/580755
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Joedian Reid <joedian@google.com>
2024-04-25 00:46:29 +00:00
..
color image: add available godoc link 2023-10-19 12:02:45 +00:00
draw image: use built-in clear to simplify code 2024-03-11 17:08:05 +00:00
gif image/gif: revert incorrect usage of clear 2024-04-11 16:15:54 +00:00
internal/imageutil all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
jpeg image/jpeg: ignore garbage bytes before a RST marker 2024-04-25 00:46:29 +00:00
png image: use built-in clear to simplify code 2024-03-11 17:08:05 +00:00
testdata image/jpeg: ignore garbage bytes before a RST marker 2024-04-25 00:46:29 +00:00
decode_example_test.go image: add an example that shows how to get the metadata of an image 2018-05-29 03:16:17 +00:00
decode_test.go image: add missing error check in test 2019-05-23 15:51:48 +00:00
format.go image: add available godoc link 2023-10-19 12:02:45 +00:00
geom.go image: add available godoc link 2023-10-19 12:02:45 +00:00
geom_test.go image: fix the overlap check in Rectangle.Intersect. 2017-02-10 05:05:59 +00:00
image.go image: add available godoc link 2023-10-19 12:02:45 +00:00
image_test.go image: add Uniform.RGBA64At and Rectangle.RGBA64At 2021-06-30 02:00:49 +00:00
names.go image: add available godoc link 2023-10-19 12:02:45 +00:00
ycbcr.go image: add available godoc link 2023-10-19 12:02:45 +00:00
ycbcr_test.go