From f4412aee749e9ca50e37427d169ce1a5543d7f6f Mon Sep 17 00:00:00 2001 From: Rhys Hiltner Date: Fri, 6 Apr 2018 11:06:03 -0700 Subject: [PATCH] html/template: grow srcset buffer in proportion to need MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit In particular, avoid exponential memory usage from growing it in proportion to its current size. Fixes #24731 Change-Id: I277d2fbac2ef7b00ae4b83d6d1dcd7f2e630a5cd Reviewed-on: https://go-review.googlesource.com/105155 Reviewed-by: Daniel Martí Reviewed-by: Brad Fitzpatrick Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot --- src/html/template/escape_test.go | 5 +++++ src/html/template/url.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/html/template/escape_test.go b/src/html/template/escape_test.go index dd4c53a80d..d5c258ecaa 100644 --- a/src/html/template/escape_test.go +++ b/src/html/template/escape_test.go @@ -656,6 +656,11 @@ func TestEscape(t *testing.T) { // The second URL is also filtered. ``, }, + { + "srcset buffer growth", + ``, + ``, + }, } for _, test := range tests { diff --git a/src/html/template/url.go b/src/html/template/url.go index a5c775c94e..f0516300de 100644 --- a/src/html/template/url.go +++ b/src/html/template/url.go @@ -88,7 +88,7 @@ func urlProcessor(norm bool, args ...interface{}) string { // processURLOnto appends a normalized URL corresponding to its input to b // and returns true if the appended content differs from s. func processURLOnto(s string, norm bool, b *bytes.Buffer) bool { - b.Grow(b.Cap() + len(s) + 16) + b.Grow(len(s) + 16) written := 0 // The byte loop below assumes that all URLs use UTF-8 as the // content-encoding. This is similar to the URI to IRI encoding scheme -- 2.50.0