]> Cypherpunks repositories - gostls13.git/commitdiff
context: prevent creation of invalid contexts
authorKyle Nusbaum <kyle@datadog.com>
Wed, 18 Mar 2020 19:17:50 +0000 (19:17 +0000)
committerIan Lance Taylor <iant@golang.org>
Wed, 18 Mar 2020 19:44:13 +0000 (19:44 +0000)
This commit makes it impossible to create derived contexts with nil parents.
Previously it was possible to create derived contexts with nil parents, and
invalid contexts could propogate through the program. Eventually this can
cause a panic downstream, which is difficult to trace back to the source
of the error.

Although `WithCancel` and `WithDeadline` already panic if `parent` is `nil`, this adds explicit checks to give a useful message in the panic.

Fixes #37908

Change-Id: I70fd01f6539c1b0da0e775fc5457e32e7075e52c
GitHub-Last-Rev: 1b7dadd7db9ba42952644ad5e9a49591d6a5191f
GitHub-Pull-Request: golang/go#37898
Reviewed-on: https://go-review.googlesource.com/c/go/+/223777
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/context/context.go
src/context/context_test.go
src/context/x_test.go

index b561968f31c7648fc465f694c745271bc8ae7569..b3fdb8277afc37a6c2cdbc8fee9c1967f00ea6ca 100644 (file)
@@ -230,6 +230,9 @@ type CancelFunc func()
 // Canceling this context releases resources associated with it, so code should
 // call cancel as soon as the operations running in this Context complete.
 func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
+       if parent == nil {
+               panic("cannot create context from nil parent")
+       }
        c := newCancelCtx(parent)
        propagateCancel(parent, &c)
        return &c, func() { c.cancel(true, Canceled) }
@@ -425,6 +428,9 @@ func (c *cancelCtx) cancel(removeFromParent bool, err error) {
 // Canceling this context releases resources associated with it, so code should
 // call cancel as soon as the operations running in this Context complete.
 func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
+       if parent == nil {
+               panic("cannot create context from nil parent")
+       }
        if cur, ok := parent.Deadline(); ok && cur.Before(d) {
                // The current deadline is already sooner than the new one.
                return WithCancel(parent)
@@ -511,6 +517,9 @@ func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
 // struct{}. Alternatively, exported context key variables' static
 // type should be a pointer or interface.
 func WithValue(parent Context, key, val interface{}) Context {
+       if parent == nil {
+               panic("cannot create context from nil parent")
+       }
        if key == nil {
                panic("nil key")
        }
index da29ed0c2b903ec904b442caa46b78828e7d1df7..98c6683335370fbd68f5e473c36e963dd81555e5 100644 (file)
@@ -667,6 +667,21 @@ func XTestWithValueChecksKey(t testingT) {
        }
 }
 
+func XTestInvalidDerivedFail(t testingT) {
+       panicVal := recoveredValue(func() { WithCancel(nil) })
+       if panicVal == nil {
+               t.Error("expected panic")
+       }
+       panicVal = recoveredValue(func() { WithDeadline(nil, time.Now().Add(shortDuration)) })
+       if panicVal == nil {
+               t.Error("expected panic")
+       }
+       panicVal = recoveredValue(func() { WithValue(nil, "foo", "bar") })
+       if panicVal == nil {
+               t.Error("expected panic")
+       }
+}
+
 func recoveredValue(fn func()) (v interface{}) {
        defer func() { v = recover() }()
        fn()
index e85ef2d50e5fd8cffad74f6c17f06669f3eb3935..00eca72d5aff0a3237da8d0f832d1d5f202b0951 100644 (file)
@@ -26,5 +26,6 @@ func TestLayersTimeout(t *testing.T)                   { XTestLayersTimeout(t) }
 func TestCancelRemoves(t *testing.T)                   { XTestCancelRemoves(t) }
 func TestWithCancelCanceledParent(t *testing.T)        { XTestWithCancelCanceledParent(t) }
 func TestWithValueChecksKey(t *testing.T)              { XTestWithValueChecksKey(t) }
+func TestInvalidDerivedFail(t *testing.T)              { XTestInvalidDerivedFail(t) }
 func TestDeadlineExceededSupportsTimeout(t *testing.T) { XTestDeadlineExceededSupportsTimeout(t) }
 func TestCustomContextGoroutines(t *testing.T)         { XTestCustomContextGoroutines(t) }