You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a structure that implements the error interface, and an API whose return type is a pointer to that structure type. The API returns nil on success or a pointer to an instance of that structure on failure. I have caller of that API returns error, and returns the value received from the previously described API.
When the caller of the second function examines the error that it returns, the err != nil check always evaluates to true, even when the value is nil.
The sample program above uses the number of command line arguments to control its behavior. If there are no arguments, then baz() returns a pointer to a MyError structure; if there are arguments, then baz() returns nil.
I expect that when the program is run with no arguments, I see the error handled:
$ go run example.go
error: badness
I also expect that when the program is run with arguments, I should see the "no-error" path execute:
$ go run example.go xxx
value: hello
What did you see instead?
When I run the program with arguments --- the case where baz() returns nil --- the err != nil check in main() evaluates to true even though the err is nil, and a call to err.Error() results in e.msg triggering a nil pointer dereference panic:
$ go run example.go xxx
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x49a845]
goroutine 1 [running]:
main.(*MyError).Error(0x0, 0xc0000561d0, 0xc000074f48)
/tmp/bar.go:12 +0x5
main.main()
/tmp/bar.go:37 +0x58
exit status 2
I note that in this example, changing the return type of baz() from *MyError to error results in the expected behavior.
The text was updated successfully, but these errors were encountered:
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I have a structure that implements the
error
interface, and an API whose return type is a pointer to that structure type. The API returnsnil
on success or a pointer to an instance of that structure on failure. I have caller of that API returnserror
, and returns the value received from the previously described API.When the caller of the second function examines the
error
that it returns, theerr != nil
check always evaluates totrue
, even when the value isnil
.This example program illustrates the problem
What did you expect to see?
The sample program above uses the number of command line arguments to control its behavior. If there are no arguments, then
baz()
returns a pointer to aMyError
structure; if there are arguments, thenbaz()
returnsnil
.I expect that when the program is run with no arguments, I see the error handled:
I also expect that when the program is run with arguments, I should see the "no-error" path execute:
What did you see instead?
When I run the program with arguments --- the case where
baz()
returnsnil
--- theerr != nil
check inmain()
evaluates totrue
even though theerr
isnil
, and a call toerr.Error()
results ine.msg
triggering a nil pointer dereference panic:I note that in this example, changing the return type of
baz()
from*MyError
toerror
results in the expected behavior.The text was updated successfully, but these errors were encountered: