Skip to content

math/big: Int returned by Float.Int(Inf) is not usable #33798

@robpike

Description

@robpike

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

$ go version

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GO111MODULE=""
GOARCH="amd64"
GOBIN="/Users/r/bin"
GOCACHE="/Users/r/Library/Caches/go-build"
GOENV="/Users/r/Library/Preferences/go/env"
GOEXE=""
GOFLAGS="-ldflags=-w"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/r"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/r/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/r/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/r3/cryt3b891yj6rzqr8vzcn2xc0000gn/T/go-build885119120=/tmp/go-build -gno-record-gcc-switches -fno-common"

$ go env

What did you do?

This program crashes, as demonstrated. One is supposed to use the second return value from the call to inf.Int to see what to do (its value in this case is Below) but it's not helpful because the first result is poisoned with an internal nil.

% cat x.go
package main

import (
	"fmt"
	"math/big"
)

func main() {
	var one big.Int
	var inf big.Float
	one.SetInt64(1)
	inf.SetInf(false)
	i, _ := inf.Int(nil)
	i.Add(i, &one)
	fmt.Println(i)
}
% go run x.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10a1fb9]

goroutine 1 [running]:
math/big.(*Int).Add(0x0, 0x0, 0xc00006ef40, 0x10acaff)
	/Users/r/go/src/math/big/int.go:117 +0x29
main.main()
	/Users/r/x.go:14 +0xbb
exit status 2

What did you expect to see?

What did you see instead?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions