Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
icholy opened this issue Oct 22, 2021 · 2 comments
Open

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

icholy opened this issue Oct 22, 2021 · 2 comments
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@icholy
Copy link

icholy commented Oct 22, 2021

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

@dmitshur
Copy link
Contributor

Thanks for reporting this issue. I'm not sure whether it's possible/preferable for Chain to be improved so this internal limit is removed, or maybe just documenting it is the better fix.

CC @mpvl via package owners.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 25, 2021
@cristiprg
Copy link

Hi @dmitshur @mpvl just for clarity, is there any agreement on the resolution for this issue? Even if it's just updating the documentation, it'll be useful to know 🤞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants