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)
Try
func (*os.File).Seek(offset int64, whence int) (ret int64, err error)before decoding.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
Yeah, duplicate of #10447
it works fine for me , thanks for saving my day
As I said, and as you can clearly see, because you are an active participant of that issue, there is no mention of
unexpected EOFas 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 toimagebefore 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.What’s the point? You are talking about some internals of
image, which I don’t care. I don’t even useimagedirectly! Processing of images is tightly coupled with receiving a data (i.e.imaginglib does both for us, opens a file and manipulates it). How do I know thatio.ErrUnexpectedEOFhas 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.You just said what it sounds: “premature end of [something you just processed] JPEG”.
Ok, nice. I don’t know if their use of
io.ErrUnexpectedEOFcorrect 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
ioerror was wrong would they refer toimage/jpegand say the problem will not be fixed because it’s used in the same way inimage/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/jpegcould 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.Use the
identifytool (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.