From: Olivier Mengué Date: Tue, 18 Apr 2023 20:13:27 +0000 (+0200) Subject: io: ReadAll: do not check for realloc in first round X-Git-Tag: go1.21rc1~828 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0d699b6cb3e6be63846bbbee8cc7bcbfd6cb9500;p=gostls13.git io: ReadAll: do not check for realloc in first round Refactor io.ReadAll to check for realloc of the buffer only after the first read. Fixes: #59702 Change-Id: I93b99139e6756f21738d47e7d9ad08e1d167258e Reviewed-on: https://go-review.googlesource.com/c/go/+/486236 Auto-Submit: Emmanuel Odeke Reviewed-by: Ian Lance Taylor Reviewed-by: Bryan Mills Run-TryBot: Emmanuel Odeke TryBot-Result: Gopher Robot Run-TryBot: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Emmanuel Odeke --- diff --git a/src/io/io.go b/src/io/io.go index 630ab73b56..7b8ee10a56 100644 --- a/src/io/io.go +++ b/src/io/io.go @@ -694,10 +694,6 @@ func (c nopCloserWriterTo) WriteTo(w Writer) (n int64, err error) { func ReadAll(r Reader) ([]byte, error) { b := make([]byte, 0, 512) for { - if len(b) == cap(b) { - // Add more capacity (let append pick how much). - b = append(b, 0)[:len(b)] - } n, err := r.Read(b[len(b):cap(b)]) b = b[:len(b)+n] if err != nil { @@ -706,5 +702,10 @@ func ReadAll(r Reader) ([]byte, error) { } return b, err } + + if len(b) == cap(b) { + // Add more capacity (let append pick how much). + b = append(b, 0)[:len(b)] + } } }