summaryrefslogtreecommitdiff
path: root/strings
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2022-02-13 16:03:23 -0500
committerGitHub <noreply@github.com>2022-02-13 21:03:23 +0000
commit5aaf88309cbc897be4df6c7358fba773c69373d6 (patch)
tree18fdbfd904643d5278d2ba93013c272b43626782 /strings
parentb9d6f96d85297f51b029fa025711b52c49b9a443 (diff)
Fix a few bugs found while fuzzing (#1308)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'strings')
-rw-r--r--strings/strings.go9
-rw-r--r--strings/strings_test.go4
2 files changed, 11 insertions, 2 deletions
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) {