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: Int returned by Float.Int(Inf) is not usable #33798

Closed
robpike opened this issue Aug 23, 2019 · 2 comments
Closed

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

robpike opened this issue Aug 23, 2019 · 2 comments

Comments

@robpike
Copy link
Contributor

robpike commented Aug 23, 2019

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?

@robpike robpike changed the title math/big: Int returned by Float.Int is not usable math/big: Int returned by Float.Int(Inf) is not usable Aug 23, 2019
@robpike
Copy link
Contributor Author

robpike commented Aug 23, 2019

This is clearly a minor case, but it bit me today.

@robpike
Copy link
Contributor Author

robpike commented Aug 23, 2019

Never mind, this is documented.

@robpike robpike closed this as completed Aug 23, 2019
@golang golang locked and limited conversation to collaborators Aug 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants