Skip to content

x/text/transform: Document Chain buffer limit #49117

Open
@icholy

Description

@icholy

There is an undocumented buffer limit of 4kb for each link in a transform.Chain

What did you do?

Implemented a transform.Transformer which expects transform.Chain to buffer all src until EOF.

package main

import "golang.org/x/text/transform"

type Transformer struct {
	transform.NopResetter
}

func (t *Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
	if !atEOF {
		err = transform.ErrShortSrc
	} else {
		n := copy(dst, src)
		nDst = n
		nSrc = n
		if n < len(src) {
			err = transform.ErrShortDst
		}
	}
	return
}

func main() {
	data := make([]byte, 4096+1)
	tr := transform.Chain(
		transform.Nop,
		&Transformer{},
	)
	_, _, err := transform.Bytes(tr, data)
	if err != nil {
		panic(err)
	}
}

https://play.golang.org/p/kFLxWtfNwnF

What did you expect to see?

Since the transform.Transformer implementation follows all the rules laid out by the docs, I expected it to work.

What did you see instead?

The chain returns a "transform: short internal buffer" error if it's asked to buffer more than 4kb (while nothing is consumed).

https://cs.opensource.google/go/x/text/+/refs/tags/v0.3.7:transform/transform.go;l=454-461;drc=5bd84dd9b33bd2bdebd8a6a6477920a8e492d47f

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.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