]> Cypherpunks repositories - gostls13.git/commitdiff
internal/sync: warn about incorrect unsafe usage in HashTrieMap
authorthepudds <thepudds1460@gmail.com>
Sat, 9 Aug 2025 21:25:14 +0000 (17:25 -0400)
committert hepudds <thepudds1460@gmail.com>
Mon, 11 Aug 2025 13:46:40 +0000 (06:46 -0700)
When the HashTrieMap expand method runs out of bits, it can be because
the user mutated a key after insertion using unsafe or similar
in violation of the HashTrieMap invariants.

Adjust the panic message to help triage and debugging by
more directly suggesting unsafe code might be at fault.

CL 694635 is a follow-up change that attempts to detect and
report illegally mutated keys sooner and more precisely.

Updates #74948
Updates #73427

Change-Id: Ib2bca067f0e212b8765c61183f59ac229513a823
Reviewed-on: https://go-review.googlesource.com/c/go/+/694376
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/internal/sync/hashtriemap.go

index dbf4c3473ed6a236982d5b94d98b76edb608fdeb..db832974278f1635fb1491c5bc75bff34cf911e2 100644 (file)
@@ -178,7 +178,7 @@ func (ht *HashTrieMap[K, V]) expand(oldEntry, newEntry *entry[K, V], newHash uin
        top := newIndirect
        for {
                if hashShift == 0 {
-                       panic("internal/sync.HashTrieMap: ran out of hash bits while inserting")
+                       panic("internal/sync.HashTrieMap: ran out of hash bits while inserting (incorrect use of unsafe or cgo, or data race?)")
                }
                hashShift -= nChildrenLog2 // hashShift is for the level parent is at. We need to go deeper.
                oi := (oldHash >> hashShift) & nChildrenMask