Skip to content

Commit 1d6a499

Browse files
committed
encoding/pem: yet another fuzz fake failure
Fixes #19829. Change-Id: I8500fd73c37b504d6ea25f5aff7017fbc0718570 Reviewed-on: https://go-review.googlesource.com/39314 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 69fe9ea commit 1d6a499

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/encoding/pem/pem_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,20 @@ func TestLineBreaker(t *testing.T) {
206206
}
207207

208208
func TestFuzz(t *testing.T) {
209+
// PEM is a text-based format. Assume header fields with leading/trailing spaces
210+
// or embedded newlines will not round trip correctly and don't need to be tested.
211+
isBad := func(s string) bool {
212+
return strings.ContainsAny(s, "\r\n") || strings.TrimSpace(s) != s
213+
}
214+
209215
testRoundtrip := func(block Block) bool {
216+
if isBad(block.Type) {
217+
return true
218+
}
210219
for key, val := range block.Headers {
211-
if strings.ContainsAny(key, ":\r\n") || strings.ContainsAny(val, "\r\n") || strings.TrimSpace(key) != key || strings.TrimSpace(val) != val {
212-
// Keys with colons or newlines cannot be encoded.
213-
// Keys/values with surrounding spaces might lose theirs.
220+
// Reject bad key/val.
221+
// Also, keys with colons cannot be encoded, because : is the key: val separator.
222+
if isBad(key) || isBad(val) || strings.Contains(key, ":") {
214223
return true
215224
}
216225
}

0 commit comments

Comments
 (0)