Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package config
import (
"bytes"
"fmt"
"io/ioutil"
"strings"

"golang.org/x/text/transform"
Expand All @@ -43,7 +42,10 @@ func expandVariables(f LookupFunc, buf *bytes.Buffer) (*bytes.Buffer, error) {
if f == nil {
return buf, nil
}
exp, err := ioutil.ReadAll(transform.NewReader(buf, newExpandTransformer(f)))
// when a single ${VAR} expands to a string larger than the reader's fixed dst buffer,
// transform.Bytes will grow the dst buffer automatically, unlike f.e.
// transform.NewReader + ReadAll, which will return ErrShortDst.
exp, _, err := transform.Bytes(newExpandTransformer(f), buf.Bytes())
if err != nil {
return nil, fmt.Errorf("couldn't expand environment: %v", err)
}
Expand Down
16 changes: 16 additions & 0 deletions expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ func TestExpanderLongSrc(t *testing.T) {
}
}

func TestExpandVariablesLargeReplacement(t *testing.T) {
// Regression: transform.Reader fails with ErrShortDst when the replacement
// is larger than transform's internal destination buffer and sits at the
// start of the input (Reader requires nDst!=0 || nSrc!=0 to retry).
output := strings.Repeat("a", transformBufSize-1) + "aa"
lookup := func(key string) (string, bool) {
if key == "a" {
return output, true
}
return "", false
}
out, err := expandVariables(lookup, bytes.NewBufferString("$a"))
require.NoError(t, err)
assert.Equal(t, output, out.String())
}

func TestTransformLimit(t *testing.T) {
a := strings.Repeat("a", transformBufSize-1)

Expand Down
Loading