From 5aaf88309cbc897be4df6c7358fba773c69373d6 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sun, 13 Feb 2022 16:03:23 -0500 Subject: Fix a few bugs found while fuzzing (#1308) Signed-off-by: Dave Henderson --- strings/strings.go | 9 +++++++-- strings/strings_test.go | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'strings') diff --git a/strings/strings.go b/strings/strings.go index fd8e6000..d822a57d 100644 --- a/strings/strings.go +++ b/strings/strings.go @@ -9,14 +9,19 @@ import ( "github.com/Masterminds/goutils" ) -// Indent - indent each line of the string with the given indent string +// Indent - indent each line of the string with the given indent string. +// Any indent characters are permitted, except for '\n'. +// +// TODO: return an error if the indent string contains '\n' instead of +// succeeding func Indent(width int, indent, s string) string { - if width == 0 { + if width <= 0 || strings.Contains(indent, "\n") { return s } if width > 1 { indent = strings.Repeat(indent, width) } + var res []byte bol := true for i := 0; i < len(s); i++ { diff --git a/strings/strings_test.go b/strings/strings_test.go index eba33183..e79614f9 100644 --- a/strings/strings_test.go +++ b/strings/strings_test.go @@ -11,11 +11,15 @@ func TestIndent(t *testing.T) { actual := "hello\nworld\n!" expected := " hello\n world\n !" assert.Equal(t, actual, Indent(0, " ", actual)) + assert.Equal(t, actual, Indent(-1, " ", actual)) assert.Equal(t, expected, Indent(1, " ", actual)) assert.Equal(t, "\n", Indent(1, " ", "\n")) assert.Equal(t, " foo\n", Indent(1, " ", "foo\n")) assert.Equal(t, " foo", Indent(1, " ", "foo")) assert.Equal(t, " foo", Indent(3, " ", "foo")) + + // indenting with newline is not permitted + assert.Equal(t, "foo", Indent(3, "\n", "foo")) } func TestTrunc(t *testing.T) { -- cgit v1.2.3