datastore: Commit.Key doesn't actually validate the PendingKey is from the right commit #4595
Labels
api: datastore
Issues related to the Datastore API.
priority: p2
Moderately-important priority. Fix may not be included in next release.
triage me
I really want to be triaged.
type: bug
Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
I was looking through the code for handling pending keys, in search of a bug that turned out to be unrelated (and in our code), and I noticed the following seemingly-incorrect behavior.
Code
Expected behavior
Since
pk
is aPendingKey
from the commitc2
, it seems the intention is thatc1.Key(pk)
is invalid and should panic.Actual behavior
Instead,
c1.Key(pk)
is valid, and returns the same value asc2.Key(pk)
. This isn't a huge deal since the answer is right, but it's confusing; for example one could accidentally callc1.Key(pk)
beforec2
exists, and it would just returnnil
.Cause
The problem is pretty clear in transaction.go: in
Transaction.Commit
(line 226) the code assignsp.commit = c
, and then inCommit.Key
(line 400) it errs ifc != p.commit
. But nothing ever assignsc
inCommit
, so it'snil
! Meaning that all such commits compare equal. I think the intention is for commit to be effectively a sentinel, in which case the fix would be to at some point inTransaction.Commit
assignc = new(Commit)
or similar.The text was updated successfully, but these errors were encountered: