We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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
1.4.2
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) } }
The text was updated successfully, but these errors were encountered:
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"}
Sorry, something went wrong.
True, it's actually me causing the panic by calling .String()! Sorry!
.String()
No branches or pull requests
I wanted to check if a given CIDR from user is valid, so I tried using the following approach:
but that caused panic:
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:
The text was updated successfully, but these errors were encountered: