]> Cypherpunks repositories - gostls13.git/commitdiff
errors: return false if nil error is passed to As
authorAhsun Ahmed <ahmed.ahsun@gmail.com>
Thu, 21 Mar 2019 16:45:49 +0000 (22:45 +0600)
committerMarcel van Lohuizen <mpvl@golang.org>
Wed, 10 Apr 2019 15:59:35 +0000 (15:59 +0000)
Fixes #30970

Change-Id: I333676b55a2364e329fffeafca8fc57d45a0b84b
Reviewed-on: https://go-review.googlesource.com/c/go/+/168598
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/errors/wrap.go
src/errors/wrap_test.go

index fc7bf71f8a602ba155a75bf1839aebc3f392258d..b1a15d01dd7f40063c6e6fe1f9a0141790c3012a 100644 (file)
@@ -72,7 +72,7 @@ func Is(err, target error) bool {
 // matches a type if it is assignable to the target type, or if it has a method
 // As(interface{}) bool such that As(target) returns true. As will panic if
 // target is not a non-nil pointer to a type which implements error or is of
-// interface type.
+// interface type. As returns false if error is nil.
 //
 // The As method should set the target to its value and return true if err
 // matches the type to which target points.
@@ -89,7 +89,7 @@ func As(err error, target interface{}) bool {
                panic("errors: *target must be interface or implement error")
        }
        targetType := typ.Elem()
-       for {
+       for err != nil {
                if reflectlite.TypeOf(err).AssignableTo(targetType) {
                        val.Elem().Set(reflectlite.ValueOf(err))
                        return true
@@ -97,10 +97,9 @@ func As(err error, target interface{}) bool {
                if x, ok := err.(interface{ As(interface{}) bool }); ok && x.As(target) {
                        return true
                }
-               if err = Unwrap(err); err == nil {
-                       return false
-               }
+               err = Unwrap(err)
        }
+       return false
 }
 
 var errorType = reflectlite.TypeOf((*error)(nil)).Elem()
index 657890c1a6a7f386fdfc2d35b721d26d1a3305b9..022f429c0c7427667f0385b7c2250bb6b4bf833f 100644 (file)
@@ -90,6 +90,10 @@ func TestAs(t *testing.T) {
                target interface{}
                match  bool
        }{{
+               nil,
+               &errP,
+               false,
+       }, {
                wrapped{"pittied the fool", errorT{}},
                &errT,
                true,