]> Cypherpunks repositories - gostls13.git/commitdiff
html/template: grow srcset buffer in proportion to need
authorRhys Hiltner <rhys@justin.tv>
Fri, 6 Apr 2018 18:06:03 +0000 (11:06 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 6 Apr 2018 18:51:08 +0000 (18:51 +0000)
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í <mvdan@mvdan.cc>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/html/template/escape_test.go
src/html/template/url.go

index dd4c53a80d600439ddc1a5bcba785aa4e7944d58..d5c258ecaa87d0da6e8aa283a26489550940603c 100644 (file)
@@ -656,6 +656,11 @@ func TestEscape(t *testing.T) {
                        // The second URL is also filtered.
                        `<img srcset="/not-an-image#,#ZgotmplZ">`,
                },
+               {
+                       "srcset buffer growth",
+                       `<img srcset={{",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"}}>`,
+                       `<img srcset=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>`,
+               },
        }
 
        for _, test := range tests {
index a5c775c94e2af1d4bc639c27a60327bfcc3688b7..f0516300deb8ccd860644b8f881692d635b68253 100644 (file)
@@ -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