Commit 1b7bab1ea8 for golang

commit 1b7bab1ea8d5db8b3d22cdd3db7eb0d0df391287
Author: spa5k <kamrannetic@gmail.com>
Date:   Wed Dec 2 23:08:17 2026 +0530

    hash/maphash: flush a full buffer in WriteComparable

    Hash keeps up to bufSize bytes in a buffer. It hashes the bytes when
    the buffer is full, but not always at the same time. A Write or
    WriteString that fills a partly full buffer flushes it at once. A
    single write of bufSize bytes into an empty buffer, or a WriteByte,
    keeps the full buffer until more bytes come.

    The two states represent the same input, and Sum64 gives the same
    result for both. But WriteComparable mixes the buffer length into the
    hash state. Thus it gives a different hash for each state, and the
    result depends on how the caller split the bytes. The Hash
    documentation says that the result must not depend on this.

    Flush a full buffer at the start of WriteComparable. Then the two
    states are equal before WriteComparable adds its value.

    Fixes #83571

    Change-Id: Ic3a4a64d75f116c79b7f15c3a3ff3aaf56b74cd0
    Reviewed-on: https://go-review.googlesource.com/c/go/+/869321
    Auto-Submit: Alan Donovan <adonovan@google.com>
    Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: Alan Donovan <adonovan@google.com>
    Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>

diff --git a/src/hash/maphash/maphash.go b/src/hash/maphash/maphash.go
index f526084495..06d0bc2609 100644
--- a/src/hash/maphash/maphash.go
+++ b/src/hash/maphash/maphash.go
@@ -308,11 +308,19 @@ func WriteComparable[T comparable](h *Hash, x T) {
 	abi.EscapeNonString(x)
 	h.initSeed()
 	// writeComparable directly operates on h.state
-	// without using h.buf. Mix in the buffer length so it won't
-	// commute with a buffered write, which either changes h.n or changes
-	// h.state.
-	if h.n != 0 {
-		writeComparable(h, h.n)
+	// without using h.buf.
+	if h.n > 0 {
+		if h.n == len(h.buf) {
+			// Write buffer full: hash it. Depending on how
+			// the bytes were written, it may not be flushed yet.
+			h.flush()
+		} else {
+			// Write buffer incomplete; flush it later, but
+			// for now mix in the buffer length so it won't
+			// commute with a buffered write,
+			// which either changes h.n or changes h.state.
+			writeComparable(h, h.n)
+		}
 	}
 	writeComparable(h, x)
 }
diff --git a/src/hash/maphash/maphash_test.go b/src/hash/maphash/maphash_test.go
index a1bc9b13bf..96ac8b647c 100644
--- a/src/hash/maphash/maphash_test.go
+++ b/src/hash/maphash/maphash_test.go
@@ -373,6 +373,28 @@ func TestWriteComparableUnseeded(t *testing.T) {
 	}
 }

+func TestWriteComparableFullBuffer(t *testing.T) {
+	seed := MakeSeed()
+	s := strings.Repeat("a", bufSize)
+
+	// Two writes: the second write fills the buffer and flushes it.
+	var h1 Hash
+	h1.SetSeed(seed)
+	h1.WriteString(s[:bufSize/2])
+	h1.WriteString(s[bufSize/2:])
+	WriteComparable(&h1, 123)
+
+	// One write: the buffer stays full.
+	var h2 Hash
+	h2.SetSeed(seed)
+	h2.WriteString(s)
+	WriteComparable(&h2, 123)
+
+	if got, want := h1.Sum64(), h2.Sum64(); got != want {
+		t.Errorf("split write hash = %#x, want %#x", got, want)
+	}
+}
+
 func testWriteComparableNoEqual[T comparable](t *testing.T, v1, v2 T) {
 	seed := MakeSeed()
 	h1 := Hash{}