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

net: Invalid CIDR in net.ParseCIDR causes panic #11393

Closed
radeksimko opened this issue Jun 25, 2015 · 2 comments

Comments

Projects
None yet
3 participants
@radeksimko
Copy link

commented Jun 25, 2015

I wanted to check if a given CIDR from user is valid, so I tried using the following approach:

_, ipnet, err := net.ParseCIDR("10.256.0.0/16")
if err != nil {
    fmt.Printf("%#v, %#v", ipnet.String(), err)
}

but that caused panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x57850]

goroutine 1 [running]:
net.networkNumberAndMask(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /usr/local/Cellar/go/1.4.2/libexec/src/net/ip.go:436 +0x200
net.(*IPNet).String(0x0, 0x0, 0x0)
    /usr/local/Cellar/go/1.4.2/libexec/src/net/ip.go:486 +0x4b
main.main()
    /var/workspace/terraform-test/test.go:11 +0x68

goroutine 2 [runnable]:
runtime.forcegchelper()
    /usr/local/Cellar/go/1.4.2/libexec/src/runtime/proc.go:90
runtime.goexit()
    /usr/local/Cellar/go/1.4.2/libexec/src/runtime/asm_amd64.s:2232 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
    /usr/local/Cellar/go/1.4.2/libexec/src/runtime/mgc0.go:82
runtime.goexit()
    /usr/local/Cellar/go/1.4.2/libexec/src/runtime/asm_amd64.s:2232 +0x1

goroutine 4 [runnable]:
runtime.runfinq()
    /usr/local/Cellar/go/1.4.2/libexec/src/runtime/malloc.go:712
runtime.goexit()
    /usr/local/Cellar/go/1.4.2/libexec/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

I'm using go 1.4.2 as it's obvious from the log above.
See on playground: http://play.golang.org/p/CqmfdTgQLm

The cleanest workaround I was able to come up with:

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Invalid CIDR")
            return
        }
    }()
    _, ipnet, err := net.ParseCIDR("10.256.0.0/16")
    if err != nil {
        fmt.Printf("%#v, %#v", ipnet.String(), err)
    }
}

@mikioh mikioh changed the title Invalid CIDR in net.ParseCIDR causes panic net: Invalid CIDR in net.ParseCIDR causes panic Jun 25, 2015

@mikioh

This comment has been minimized.

Copy link
Contributor

commented Jun 25, 2015

When ParseCIDR fails, the returned values are:

ip = net.IP(nil)
ipnet = (*net.IPNet)(nil)
err = &net.ParseError{Type:"CIDR address", Text:"10.256.0.0/16"}

@mikioh mikioh closed this Jun 25, 2015

@radeksimko

This comment has been minimized.

Copy link
Author

commented Jun 25, 2015

True, it's actually me causing the panic by calling .String()! Sorry!

@golang golang locked and limited conversation to collaborators Jun 25, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.