golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.04k stars 17.67k forks source link

image.Decode - Empty formats out of sniff func #66035

Closed DubDub2011 closed 8 months ago

DubDub2011 commented 8 months ago

Go version

go1.22.0 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\{name}\AppData\Local\go-build
set GOENV=C:\Users\{name}\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\{name}\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\{name}\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.0
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\Users\{name}\source\repos\Go\Wave-Function-Collapse\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\{user}\AppData\Local\Temp\go-build1000618332=/tmp/go-build -gno-record-gcc-switches

What did you do?

Was trying to decode a .png using image.Decode, which should be no harm.

Got the "image: unknown format" error out of the Decode func, but I checked and everything I was doing looked correct.

Here is my code:

        imgReader, err := os.Open(imgPath)
        if err != nil {
            panic(fmt.Errorf("failed to open image %s with error %v", imgPath, err))
        }
        defer imgReader.Close()
        img, _, err := image.Decode(imgReader)
        if err != nil {
            panic(fmt.Errorf("failed to decode image %s with error %v", imgPath, err))
        }

Attached here is the image: blank

What did you see happen?

I got to debugging in the source code and found that within the sniff function with image, the line to load the supported formats, formats, _ := atomicFormats.Load().([]format) is returning an empty slice.

Because there are no formats to compare to, this gives the error. There should always be formats, which is the problem.

This issue started appearing out of nowhere, and I could load the image fine before, and I don't think I did anything in particular differently.

Below demonstrates the line above returning an empty set of formats.

no-formats

What did you expect to see?

I expect a list of formats to be returned, and png included inside of them, so that my image is successfully decoded.

To fix it, I've tried:

DubDub2011 commented 8 months ago

Just to add, I assume I've broken something somewhere, but I don't know the internals on where the list of formats is pulled from, so sorry I don't have more helpful reproduction steps, think this one needs to be reverse engineered.

seankhliao commented 8 months ago

I think this is more an issue with your debugger rather than go?

DubDub2011 commented 8 months ago

I don't think so, I've tried running go run main.go and I'm getting the same error.

panic: failed to decode image assets/blank.png with error image: unknown format

goroutine 1 [running, locked to thread]:
wavefunctioncollapse/gui.NewSimulation({0x528b01, 0x6}, 0x10, 0x9)
        C:/Users/{user}/source/repos/Go/Wave-Function-Collapse/gui/gui.go:56 +0x6e7
wavefunctioncollapse/gui.RunSimulation()
        C:/Users/{user}/source/repos/Go/Wave-Function-Collapse/gui/gui.go:118 +0x55
main.main()
        C:/Users/{user}/source/repos/Go/Wave-Function-Collapse/main.go:6 +0xf
exit status 2

The panic I'm just throwing if I get an error, but that's intentional.

seankhliao commented 8 months ago

do you have a minimal reproducer?

DubDub2011 commented 8 months ago

No sorry, I don't know how I produced the error. The code I've given and asset used is all I have, but as I say pretty sure the code is correct.

DubDub2011 commented 8 months ago

Right figured it out from reading the docs, RTFM!

Straight at the top of https://pkg.go.dev/image

Values of the Image interface are created either by calling functions such as NewRGBA and NewPaletted, or by calling Decode on an io.Reader containing image data in a format such as GIF, JPEG or PNG. Decoding any particular image format requires the prior registration of a decoder function. Registration is typically automatic as a side effect of initializing that format's package so that, to decode a PNG image, it suffices to have

import _ "image/png"

in a program's main package. The _ means to import a package purely for its initialization side effects.

Apologies, should have checked there first.