Skip to content

x/text/transform: Writer returns n < len(p), no error #46892

Open
@saracen

Description

@saracen

What version of Go are you using (go version)?

$ go version
go version go1.16.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

  • Created a transformer that almost always requires more data.
  • Setup a transformer.NewWriter and calledWrite() with largish buffers.
type problemTransform []byte

func (problemTransform) Reset() {}

func (t problemTransform) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
	// if we don't have an EOF, progress only 5 bytes at a time whilst asking for more data
	if !atEOF && len(src) > 5 {
		n := copy(dst, src[:5])
		err = transform.ErrShortSrc
		if n < 5 {
			err = transform.ErrShortDst
		}
		return n, n, err
	}

	// if we're at EOF or there's less than 5 bytes available, copy everything
	n := copy(dst, src)
	if n < len(src) {
		err = transform.ErrShortDst
	}
	return n, n, err
}

func TestNBelowLenP(t *testing.T) {
	w := transform.NewWriter(ioutil.Discard, problemTransform{})

	write := func(p []byte) {
		n, err := w.Write(p)
		if err != nil {
			t.Error(err)
		}

		if n != len(p) {
			t.Errorf("n %d below len(p) %d", n, len(p))
		}
	}

	write(append([]byte("1111111111"), make([]byte, 2048)...))
	write(make([]byte, 2048))

	if err := w.Close(); err != nil {
		t.Error(err)
	}
}

What did you expect to see?

Everything written and n == len(p)

What did you see instead?

t.Error raises n 2043 below len(p) 2048

I suspect this is to with the internal buffer size used, however, is this expected? The transformer is making progress, I would have expected everything to still be copied or an err when n != len(p).

Writing the same amount of data but breaking it up over multiple Write() calls does work.

/cc @mpvl Do you have any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions