Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Nil type aliases to big.Int cause nil pointer exceptions when used #867

Open
@ncw

Description

@ncw

This program:

package main

import (
	"fmt"
	"math/big"
)

type BigInt big.Int

func main() {
	_2 := big.NewInt(2)
	_100 := big.NewInt(100)
	z := new(big.Int)
	z.Exp(_2, _100, nil)

	// Exp works with a nil modulus
	fmt.Printf("z = %d\n", z)

	// Now make a nil type alias
	var y *BigInt = nil

	// Should now be a nil *big.Int
        // This line doesn't error, but x causes a nil pointer exception wherever it is used
	x := (*big.Int)(y)

	// Note the supressed nil pointer exception
	fmt.Printf("x = %d\n", x)

	// This causes a nil pointer exception
	z.Exp(_2, _100, x)
	fmt.Printf("z = %d\n", z)
}

Works fine under go producing this output

z = 1267650600228229401496703205376
x = <nil>
z = 1267650600228229401496703205376

However it produces this output under gopherjs

z = 1267650600228229401496703205376
x = %!d(PANIC=runtime error: invalid memory address or nil pointer dereference)
Uncaught Error: runtime error: invalid memory address or nil pointer dereference
    at $callDeferred (gopherjs_exp_bug.js:1419)
    at $panic (gopherjs_exp_bug.js:1458)
    at throw$1 (runtime.go:225)
    at Object.$throwNilPointerError (gopherjs_exp_bug.js:29)
    at Object.get (gopherjs_exp_bug.js:309)
    at Object.$packages.math/big.Int.ptr.Exp (int.go:463)
    at main (gopherjs_exp_bug.go:33)
    at $init (gopherjs_exp_bug.js:28464)
    at $goroutine (gopherjs_exp_bug.js:1478)
    at $runScheduled (gopherjs_exp_bug.js:1518)
    at $schedule (gopherjs_exp_bug.js:1534)
    at $go (gopherjs_exp_bug.js:1510)
    at gopherjs_exp_bug.js:28475
    at gopherjs_exp_bug.js:28478


The problem is something to do with the nil type alias for big.Int - it produces a nil pointer exception when used. I'm not sure whether this is specific to big.Int or not though.

Tested with the latest gopherjs (1babbf9) using gopherjs serve and Chrome 69.0.3497.100

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