Skip to content

image/jpeg: "bad RST marker" error when decoding #40130

@haoyang-zheng

Description

@haoyang-zheng

What version of Go are you using (go version)?

$ go version
go version go1.14.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/teikouyou/Library/Caches/go-build"
GOENV="/Users/teikouyou/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/teikouyou/dev/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.4/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.4/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d7/wlm33f9550x_3bzzp_3b32h40000gn/T/go-build574141687=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

The following error occurred when I decoded a specific image.

invalid JPEG format: bad RST marker

Part of binary data of the image

ff ff ff ff ff ff ff ff ff ff ff ff d0 fd 32 4b

The correct marker here should be [ ff d0 ], but there is some ff bytes in front of it.

It seems that there is a consecutive 0xFF bytes in the SOS segment that causes problems in marker's matching.

This is a quote from jpeg Wikipedia.
https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure

Note that consecutive 0xFF bytes are used as fill bytes for padding purposes, although this fill byte padding should only ever take place for markers immediately following entropy-coded scan data (see JPEG specification section B.1.1.2 and E.1.2 for details; specifically "In all cases where markers are appended after the compressed data, optional 0xFF fill bytes may precede the marker"

So I think this situation should be taken into account at image/jpeg/scan.go

if d.tmp[0] != 0xff || d.tmp[1] != expectedRST {

What did you expect to see?

Image successfully decoded

What did you see instead?

invalid JPEG format: bad RST marker

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions