big int panic on text conversion

132 views
Skip to first unread message

Poonai

unread,
Feb 14, 2024, 12:35:52 AM2/14/24
to golang-nuts
big int panics during text conversion randomly 

stack trace:

panic: runtime error: index out of range [1] with length 1

goroutine 2088184 [running]:
math/big.nat.itoa({0xc01db71500, 0x1, 0x199?}, 0x0, 0xa)
        /usr/local/go/src/math/big/natconv.go:340 +0x3d2
math/big.(*Int).Text(...)
        /usr/local/go/src/math/big/intconv.go:25
math/big.(*Int).String(...)
        /usr/local/go/src/math/big/intconv.go:40

It shows up randomly, help me debug

Poonai

unread,
Feb 14, 2024, 12:37:07 AM2/14/24
to golang-nuts
I'm not able to reproduce as well

Dan Kortschak

unread,
Feb 14, 2024, 1:00:37 AM2/14/24
to golan...@googlegroups.com
I think you have probably ended up with a zero big.Int with a non-zero
length abs field. This is likely either from unsafe manipulation or a
race.

The crash can be replicated thus https://go.dev/play/p/WV78xvsbn4m

Kurtis Rader

unread,
Feb 14, 2024, 1:02:02 AM2/14/24
to Poonai, golang-nuts
Maybe provide a minimal reproducible example (https://stackoverflow.com/help/minimal-reproducible-example)?

While it is theoretically possible there is a bug in the `big` package that could result in such panics it is much more likely the bug is in your code. So showing us how you are using the `big` package will help us help you.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/db0a5e73-0a1d-4dfb-9629-1d54cf492f95n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Brian Candler

unread,
Feb 14, 2024, 4:22:19 AM2/14/24
to golang-nuts
Have you tried running your entire code under the race detector?

Poonai

unread,
Feb 14, 2024, 5:07:48 AM2/14/24
to golang-nuts
Thanks all,

figured out the issue:

func main() {
    a := big.NewInt(1)
    b := *a
    c := *a
    c.Sub(&c, big.NewInt(1))
    fmt.Println(b.String())
    fmt.Println(c.String())
    fmt.Println(a.String())
}


reproducible code. don't know what causing it tho :(

Dan Kortschak

unread,
Feb 14, 2024, 6:14:20 AM2/14/24
to golan...@googlegroups.com
On Wed, 2024-02-14 at 02:07 -0800, Poonai wrote:
> Thanks all,
>
> figured out the issue:
>
> func main() {
>     a := big.NewInt(1)
>     b := *a
>     c := *a
>     c.Sub(&c, big.NewInt(1))
>     fmt.Println(b.String())
>     fmt.Println(c.String())
>     fmt.Println(a.String())
> }
>
>
> reproducible code. don't know what causing it tho :(

Minimised: https://go.dev/play/p/oL17vkcjaEl

This is happening because the sum in a doesn't result in a
normalisation of b, https://go.dev/play/p/cIBDbRXFnAT (in this
situation where the abs field represents a zero, it should be zero
length).

Given that this can happen without a race or unsafe modifications it
might be worth filing a issue for.

Jan Mercl

unread,
Feb 14, 2024, 6:25:00 AM2/14/24
to Dan Kortschak, golan...@googlegroups.com
On Wed, Feb 14, 2024 at 12:14 PM 'Dan Kortschak' via golang-nuts
<golan...@googlegroups.com> wrote:

> Given that this can happen without a race or unsafe modifications it
> might be worth filing a issue for.

I think this is expected. Quoting from the big.Int docs
https://pkg.go.dev/math/big#Int

----
To "copy" an Int value, an existing (or newly allocated) Int must be
set to a new value using the Int.Set method; shallow copies of Ints
are not supported and may lead to errors.
----

But OP's 'b := *a' is creating a shallow copy.

Dan Kortschak

unread,
Feb 14, 2024, 6:36:25 AM2/14/24
to golan...@googlegroups.com
You are absolutely right. I looked for this kind of warning, but
obviously not hard enough.

Reply all
Reply to author
Forward
0 new messages