]> Cypherpunks repositories - gostls13.git/commit
iter: detect and reject double next and double yield in Pull, Pull2
authorMichael Anthony Knyszek <mknyszek@google.com>
Tue, 7 May 2024 00:49:03 +0000 (00:49 +0000)
committerMichael Knyszek <mknyszek@google.com>
Wed, 8 May 2024 18:03:22 +0000 (18:03 +0000)
commitff743ce862440f332f76a8a24333a90b7afc9fa6
tree2ba2cabe8f2566a9010a49f650e35f87de4a95f9
parent36d32f68f41561fb64677297e3733f5d5b866c2a
iter: detect and reject double next and double yield in Pull, Pull2

Currently it's possible for next and yield to be called out of sequence,
which will result in surprising behavior due to the implementation.
Because we blindly coroswitch between goroutines, calling next from the
iterator, or yield from the calling goroutine, will actually switch back
to the other goroutine. In the case of next, we'll switch back with a
stale (or zero) value: the results are basically garbage. In the case of
yield, we're switching back to the *same* goroutine, which will crash in
the runtime.

This change adds a single bool to ensure that next and yield are always
called in sequence. That is, every next must always be paired with a
yield before continuing. This restricts what can be done with Pull, but
prevents observing some truly strange behaviors that the user of Pull
likely did not intend, or can't easily predict.

Change-Id: I6f72461f49c5635d6914bc5b968ad6970cd3c734
Reviewed-on: https://go-review.googlesource.com/c/go/+/583676
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
src/iter/iter.go
src/iter/pull_test.go