Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math/big: UnmarshalText <nil> to *big.Float, *big.Int, *big.Rat caused panic #51498

Open
hopehook opened this issue Mar 5, 2022 · 2 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@hopehook
Copy link
Member

hopehook commented Mar 5, 2022

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

$ go version
go version devel go1.18-8db9e1cebf Sat Feb 12 17:37:33 2022 +0800 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="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/hopehook/Library/Caches/go-build"
GOENV="/Users/hopehook/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/hopehook/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/hopehook/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/hopehook/go-google/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/hopehook/go-google/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="devel go1.18-8db9e1cebf Sat Feb 12 17:37:33 2022 +0800"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/xc/x_k35ynn063c15716c0d_5pm0000gn/T/go-build3768576566=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

  • Encoding *big.Float, *big.Int, *big.Rat nil pointers with MarshalText
  • Unmarshal text <nil> to *big.Float, *big.Int, *big.Rat nil pointers with UnmarshalText

For example:
https://go.dev/play/p/oViLT_0fgBu

What did you expect to see?

  • Encoding *big.Rat nil pointer will return text <nil>

    • Because of encoding *big.Float, *big.Int nil pointers with MarshalText will return the text <nil>
  • Unmarshal text <nil> to *big.Float, *big.Int, *big.Rat nil pointers with UnmarshalText not panic

    • For consistency and reliability, UnmarshalText <nil> should also work fine, not panic.

What did you see instead?

panic like this:

  • Encoding *big.Rat nil pointer with MarshalText
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x109abb8]

goroutine 1 [running]:
math/big.(*Rat).MarshalText(0xc000106f30?)
	/Users/hopehook/go-google/go/src/math/big/ratmarsh.go:62 +0x18
main.main()
	/Users/hopehook/lab/go/src/hello/main.go:13 +0x7e
  • Unmarshal text <nil> to *big.Float nil pointer with UnmarshalText
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x108dc06]

goroutine 1 [running]:
math/big.(*Float).scan(0x0, {0x10cf578, 0xc000130000}, 0x0)
	/Users/hopehook/go-google/go/src/math/big/floatconv.go:34 +0x46
math/big.(*Float).Parse(0x0, {0xc000128007, 0x5}, 0x114af00?)
	/Users/hopehook/go-google/go/src/math/big/floatconv.go:273 +0x156
math/big.(*Float).UnmarshalText(0x114af00?, {0xc000128002, 0x5, 0x5})
	/Users/hopehook/go-google/go/src/math/big/floatmarsh.go:119 +0x4b
main.main()
	/Users/hopehook/lab/go/src/hello/main.go:11 +0x46
@gopherbot
Copy link

Change https://go.dev/cl/390174 mentions this issue: math/big: unmarshal text to struct big.Float caused panic

@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 5, 2022
@hopehook hopehook changed the title math/big: UnmarshalText <nil> to big.Float, big.Int, big.Rat caused panic math/big: UnmarshalText <nil> to *big.Float, *big.Int, *big.Rat caused panic Mar 9, 2022
@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
@odeke-em
Copy link
Member

@hopehook thanks for filing this issue and thanks for the patience this long. So a few things:

MarshalText

  • invoking (*big).(Float, Int, Rat)(nil).MarshalText() is invoking a method on a nil pointer: why shouldn't this panic? It should
  • invoking (*big).(Float, Int, Rat)(nil).UnmarshalText on a nil pointer should also panic, why would you set to a nil pointer?

With the above, I don't think that we should do anything because the requests that you are asking are for special exceptions that differ from the norm of dereferencing objects, and this can cause unexpected results. Kindly please perhaps help me ith some rationale for why you need this exception, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants