go: image/jpeg: unexpected EOF when decoding image file

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

$ go version
go version go1.16.3 windows/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
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\***\AppData\Local\go-build
set GOENV=C:\Users\***\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\***\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\***\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\***\AppData\Local\Temp\go-build2404133136=/tmp/go-build -gno-record-gcc-switches

What did you do?

I work with a web platform, where multiple users can post their content, including images. We’re moving from legacy solution based on PHP and re-implementing it in Go. One of the functionality required for a platform is resizing/cropping images. We used github.com/disintegration/imaging for that and encountered this problem with that library originally. What happens is that for some particular jpeg images, there is an error when you try to decode it using standard library.

Please note, that Firefox/Chrome/etc browsers open and display that image correctly, as well as imagick (part of legacy solution) does resize/cropping of it without any problems. With image/jpeg I’m getting unexpected EOF when trying to decode an image.

Here is a sample code to reproduce an error:

package main

import (
	"image"
	_ "image/jpeg"
	"os"
)

func main() {
	f, err := os.Open("1.jpg")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	_, _, err = image.Decode(f)
	if err != nil {
		panic(err)
	}
}

Output:

panic: unexpected EOF
...

Archived image: 1.zip

What did you expect to see?

No panic.

What did you see instead?

Panic.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 24 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Try func (*os.File).Seek(offset int64, whence int) (ret int64, err error) before decoding.

package main

import (
	"image"
	_ "image/jpeg"
	"os"
)

func main() {
	f, err := os.Open("1.jpg")
	if err != nil {
		panic(err)
	}
	defer f.Close()
 
        f.Seek(0, 0)
	_, _, err = image.Decode(f)
	if err != nil {
		panic(err)
	}
}

I figured this because some image file from Samsung Galaxy Panorama Image, which does not have EOI marker from this adobe forum.

https://community.adobe.com/t5/lightroom-classic-discussions/samsung-s9-panoramic-jpeg/td-p/10379560

And solved it by appending two bytes at the end of the buffer before passing it to .Decode

        // MEMO: Workaround for Samsung Galaxy Panorama, by appending EOI marker
	// MEMO: JPEG Spec doesn't define file format, so extra data is OK
	buf = append(buf, 0xff, 0xd9)

Yeah, duplicate of #10447

I figured this because some image file from Samsung Galaxy Panorama Image, which does not have EOI marker from this adobe forum.

https://community.adobe.com/t5/lightroom-classic-discussions/samsung-s9-panoramic-jpeg/td-p/10379560

And solved it by appending two bytes at the end of the buffer before passing it to .Decode

        // MEMO: Workaround for Samsung Galaxy Panorama, by appending EOI marker
	// MEMO: JPEG Spec doesn't define file format, so extra data is OK
	buf = append(buf, 0xff, 0xd9)

it works fine for me , thanks for saving my day

As for (1), that is a duplicate of #10447, as previously mentioned. Even if we ‘fixed’ (2), we can’t solve your underlying problem (you want as much of the JPEG as you can still decode, even if it’s overall technically an invalid JPEG) without solving (1).

As I said, and as you can clearly see, because you are an active participant of that issue, there is no mention of unexpected EOF as a result of processing possibly “corrupted” file. We spent quite some time investigating it, and our case is real-world scenario, and we searched through open and closed issues related to image before posting it. So I disagree it is “duplicate”, if you think it’s similar situation underneath (which is not that clear from the error text, it sounds more like a bug of a library) then you could copy my case to #10447 thread, so it would become an “addition” not just “duplicate”, to help others who may encounter the same situation. Instead you decided to just close my case.

As for (2), io.ErrUnexpectedEOF still seems reasonable to me. We asked jpeg.Decode to do something, passing an io.Reader.

What’s the point? You are talking about some internals of image, which I don’t care. I don’t even use image directly! Processing of images is tightly coupled with receiving a data (i.e. imaging lib does both for us, opens a file and manipulates it). How do I know that io.ErrUnexpectedEOF has anything to do with “wrong” JPEGs? How do I handle this error? What exactly I, as a caller, should do and how do I distinguish this from I/O errors? Is this a problem with web socket or TCP stream? Or NFS? Should I re-instantiate connection or close it? Should I report a caller that file needs to be resent? There is nothing pointing to the fact that file was analyzed and library could not decode it, the only fact you know is that something is wrong with I/O.

It couldn’t do it, because it hit a premature end (an EOF) of the data stream. Sounds like an unexpected EOF to me.

You just said what it sounds: “premature end of [something you just processed] JPEG”.

The compress/zlib package will return io.ErrUnexpectedEOF if it’s invalid ZLIB. …

Ok, nice. I don’t know if their use of io.ErrUnexpectedEOF correct or not, because I don’t have experience report for that, but what I know for sure is that it’s wrong in this particular case.

And, again what’s the point to referring to them? So if, lets imagine, their use of io error was wrong would they refer to image/jpeg and say the problem will not be fixed because it’s used in the same way in image/jpeg? Vicious circle? If it’s wrong or bad design, than it should be fixed, as simple as that.

@inliquid, in fact, I’m with you 🤝 . I wish image/jpeg could be more tolerant with not perfect images too! But the fact is that @nigeltao is very overloaded 🤗 . I guess he has a very big backlog, and this feature doesn’t get the chance to enter the top.

In my opinion, this issue can not be addressed without #10447 being addressed, and it doesn’t help to just leave this one open. #10447 is tagged as NeedsDecision, to push things forward, I think we need someone experienced in image processing to create a proposal first. I wish I could help, but it’s sad that I’m not familiar with image processing.

Image Magic is de-facto industry standard for all sorts of image processing. It’s an addition to a quite decent list of software which doesn’t treat that image as “invalid”.

Use the identify tool (shipped with ImageMagick) on the file and it will tell you that the JPEG file is corrupted/truncated. See an earlier comment where this was already done (using the GraphicsMagick fork of ImageMagick).

@ZekeLu also pointed out that your problem is already discussed in issue #10447.