]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/pem: yet another fuzz fake failure
authorRuss Cox <rsc@golang.org>
Mon, 3 Apr 2017 18:34:51 +0000 (14:34 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 3 Apr 2017 18:46:56 +0000 (18:46 +0000)
Fixes #19829.

Change-Id: I8500fd73c37b504d6ea25f5aff7017fbc0718570
Reviewed-on: https://go-review.googlesource.com/39314
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/pem/pem_test.go

index 7ab0e8f61d23707beedf2006549dd77c7c996cfc..1a1250a52f4d266e47bf6e3ec6ea58c328afa9e3 100644 (file)
@@ -206,11 +206,20 @@ func TestLineBreaker(t *testing.T) {
 }
 
 func TestFuzz(t *testing.T) {
+       // PEM is a text-based format. Assume header fields with leading/trailing spaces
+       // or embedded newlines will not round trip correctly and don't need to be tested.
+       isBad := func(s string) bool {
+               return strings.ContainsAny(s, "\r\n") || strings.TrimSpace(s) != s
+       }
+
        testRoundtrip := func(block Block) bool {
+               if isBad(block.Type) {
+                       return true
+               }
                for key, val := range block.Headers {
-                       if strings.ContainsAny(key, ":\r\n") || strings.ContainsAny(val, "\r\n") || strings.TrimSpace(key) != key || strings.TrimSpace(val) != val {
-                               // Keys with colons or newlines cannot be encoded.
-                               // Keys/values with surrounding spaces might lose theirs.
+                       // Reject bad key/val.
+                       // Also, keys with colons cannot be encoded, because : is the key: val separator.
+                       if isBad(key) || isBad(val) || strings.Contains(key, ":") {
                                return true
                        }
                }