File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -57,6 +57,10 @@ func (h *Header) Hash() Hash {
5757// MemoizeHash computes the header hash and stores it on the header for future
5858// Hash() calls. Call this before publishing the header to shared goroutines or
5959// caches.
60+ //
61+ // If a Header struct is reused (e.g. overwritten via FromProto or field
62+ // assignment), call InvalidateHash() first to clear the cached value before
63+ // calling MemoizeHash again. Failure to do so will return the stale cached hash.
6064func (h * Header ) MemoizeHash () Hash {
6165 if h == nil {
6266 return nil
@@ -73,6 +77,8 @@ func (h *Header) MemoizeHash() Hash {
7377}
7478
7579func (h * Header ) computeHash () Hash {
80+ // Legacy hash takes precedence when legacy fields are present (backwards
81+ // compatibility). Slim hash is the canonical hash for all other headers.
7682 if h .Legacy != nil && ! h .Legacy .IsZero () {
7783 if legacyHash , err := h .HashLegacy (); err == nil {
7884 return legacyHash
Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ func HeaderFromContext(ctx context.Context) (Header, bool) {
2323 return Header {}, false
2424 }
2525
26+ // Clear the memoized hash on the returned copy. The caller may mutate
27+ // fields and call Hash() without knowing to call InvalidateHash() first,
28+ // which would return a stale cached value.
29+ h .InvalidateHash ()
2630 return h , true
2731}
2832
You can’t perform that action at this time.
0 commit comments