mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-21 07:28:45 +00:00
util/sha256x: make Hash.Sum non-escaping (#5338)
Since Hash is a concrete type, we can make it such that Sum never escapes the input. Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
@@ -45,10 +45,19 @@ func (h *Hash) Sum(b []byte) []byte {
|
||||
h.h.Write(h.x[:h.nx])
|
||||
h.nx = 0
|
||||
}
|
||||
return h.h.Sum(b)
|
||||
|
||||
// Unfortunately hash.Hash.Sum always causes the input to escape since
|
||||
// escape analysis cannot prove anything past an interface method call.
|
||||
// Assuming h already escapes, we call Sum with h.x first,
|
||||
// and then the copy the result to b.
|
||||
sum := h.h.Sum(h.x[:0])
|
||||
return append(b, sum...)
|
||||
}
|
||||
|
||||
func (h *Hash) Reset() {
|
||||
if h.h == nil {
|
||||
h.h = sha256.New()
|
||||
}
|
||||
h.h.Reset()
|
||||
h.nx = 0
|
||||
}
|
||||
|
Reference in New Issue
Block a user