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

Fixup line wrapper short write bug #185

Merged
merged 2 commits into from
Sep 28, 2024
Merged

Fixup line wrapper short write bug #185

merged 2 commits into from
Sep 28, 2024

Conversation

agcom
Copy link
Contributor

@agcom agcom commented Sep 28, 2024

TL;DR, without this patch, the following test would not pass:

func TestLineWrapperShortWrite(t *testing.T) {
    var sb strings.Builder
    w := &lineWrapper{w: &sb, maxLineLen: 76}

    b := []byte("The end.\r\n")
    n, err := w.Write(b)
    if err != nil {
        t.Fatalf("Write() = %v", err)
    }

    if n != len(b) {
        // FAILS HERE.
        t.Errorf("Written = %d, want %d", n, len(b))
    }
}

This is known as io.ErrShortWrite; I encountered it in bytes.Reader.WriteTo (via io.Copy):

package bytes

...

// WriteTo implements the [io.WriterTo] interface.
func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
	r.prevRune = -1
	if r.i >= int64(len(r.s)) {
		return 0, nil
	}
	b := r.s[r.i:]
	m, err := w.Write(b)
	if m > len(b) {
		panic("bytes.Reader.WriteTo: invalid Write count")
	}
	r.i += int64(m)
	n = int64(m)
	if m != len(b) && err == nil {
		err = io.ErrShortWrite
	}
	return
}

...

@emersion
Copy link
Owner

The io.Writer docs say:

It returns the number of bytes written from p (0 <= n <= len(p))

IOW: if we write extra bytes not contained in the input slice, I don't think we should count these?

@emersion
Copy link
Owner

Ah, but sometimes the \n that we write was in the input. How about this?

if lf {
    written++
}

@agcom
Copy link
Contributor Author

agcom commented Sep 28, 2024

Ah, but sometimes the \n that we write was in the input. How about this?

if lf {
    written++
}

Yes, your suggestion is correct. The count of written bytes should match the length of the given slice of bytes (unless returning an error); my initial solution would add two extra written bytes in case of reaching max line length and writing \r\n. I will update in a minute.

@agcom
Copy link
Contributor Author

agcom commented Sep 28, 2024

Also, just an observation: the underlying writer's written bytes count (n in n, err := w.w.Write(l)) should be accounted for, before checking the error; I guess this is a separate issue.

Copy link
Owner

@emersion emersion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@emersion emersion merged commit 6a718fa into emersion:master Sep 28, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants