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: signal SIGSEGV: segmentation violation when using multi-line strings. #60233

Closed
agiUnderground opened this issue May 16, 2023 · 3 comments

Comments

@agiUnderground
Copy link

agiUnderground commented May 16, 2023

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

$ go version
go version go1.20.4 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
macos Ventura 13.3.1 Darwin Kernel Version 22.4.0
x86_64

What did you do?

package main

import (
	"fmt"
	mb "math/big"
)

func main() {
	a := mb.NewInt(0)
	b := mb.NewInt(0)
	c := mb.NewInt(0)
	a, _ = a.SetString(`93495878923078976349082734823469324758967234678569
                           62387685983479898989898989899899898998989889899898
                           98989898989989898989898989899898988989898989898989
                           89898988989898989889898989989898989879879879`, 10);
	b, _ = b.SetString(`9999999949532798763428068937249324976893467324785
                           67843264876432598734698572634856923599999999983493
                           49683247986789324769438726463080980980908998798798
                           797897987987987979878978979878897987897789789`, 10);
	c.Add(a, b);
	fmt.Println(c);
}

https://play.golang.com/p/06JOoCfsT0D

What did you expect to see?

193495878418406963983363424195962574527901907926426408203347478058863368847162484691349989898897348339582237885788314668428625453070879879898897788697787796976977886969768877969868796977777669668

What did you see instead?

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x484d7d]

goroutine 1 [running]:
math/big.(*Int).Add(0xc000060020, 0x4b84a0?, 0x116?)
	/usr/local/go-faketime/src/math/big/int.go:135 +0x1d
main.main()
	/tmp/sandbox4028842665/prog.go:20 +0xe5

It is working with a regular strings formatted in one line, but with this multi-line backtick format it fails.

@shmsr
Copy link
Contributor

shmsr commented May 16, 2023

Here SetString fails in your case i.e., the value of a and b is undefined and the returned value is nil hence panic is expected. Do not ignore the return value (bool) of SetString.

@agiUnderground
Copy link
Author

@shmsr But why it is working as expected with a regular strings?

package main

import (
	"fmt"
	mb "math/big"
)

func main() {
	a := mb.NewInt(0);
	b := mb.NewInt(0);
	c := mb.NewInt(0);
	a, _ = a.SetString("93495878923078976349082734823469324758967234678569623876859834798989898989898998998989989898898998989898989898998989898989898989989898898989898989898989898988989898989889898989989898989879879879", 10);
	b, _ = b.SetString("99999999495327987634280689372493249768934673247856784326487643259873469857263485692359999999998349349683247986789324769438726463080980980908998798798797897987987987979878978979878897987897789789", 10);
	c.Add(a, b);
	fmt.Println(c);
}

@ianlancetaylor
Copy link
Member

Your multi-line strings have whitespace in them, and SetString doesn't permit whitespace.

Always check the error result.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2023
@golang golang locked and limited conversation to collaborators May 16, 2024
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

4 participants