Skip to content

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

@hopehook

Description

@hopehook

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions