Skip to content
Go: Read one extra byte to detect an oversized input

Go: Read one extra byte to detect an oversized input

November 13, 2026

Reading exactly the permitted number of bytes cannot distinguish an input at the limit from a longer input with the same prefix. Read at most one extra byte, then reject the result if it exceeds the limit.

Set up

Tested on Linux with Go 1.26.10. Use Bash with Go on your PATH. Keep the same terminal through cleanup, and stop if setup fails.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as limitreader_detect_oversize.go:

package main

import (
	"fmt"
	"io"
	"strings"
)

func readSmall(r io.Reader) ([]byte, error) {
	const maxBytes = 4
	data, err := io.ReadAll(io.LimitReader(r, maxBytes+1))
	if err != nil {
		return nil, err
	}
	if len(data) > maxBytes {
		return nil, fmt.Errorf("input exceeds %d bytes", maxBytes)
	}
	return data, nil
}
func main() {
	for _, input := range []string{"", "abcd", "abcde", "abcdef"} {
		data, err := readSmall(strings.NewReader(input))
		fmt.Printf("%q: data=%q error=%v\n", input, data, err)
	}
}
go run limitreader_detect_oversize.go

Observed output:

"": data="" error=<nil>
"abcd": data="abcd" error=<nil>
"abcde": data="" error=input exceeds 4 bytes
"abcdef": data="" error=input exceeds 4 bytes

Read the result

The four-byte input succeeds. Both longer inputs fail without returning a truncated value to the caller. io.LimitReader produces EOF at its limit; the length check supplies the application-level rejection. Read errors are also returned instead of accepting partial data.

Apply it carefully

The limit counts bytes, not Unicode characters. This fixed small constant keeps the extra-byte arithmetic safe; validate configurable limits before adding one. A byte cap provides no deadline, and a reader can still block. The helper neither drains the remainder nor closes the underlying reader; its owner must manage those responsibilities.

See the Go standard-library reference.

Clean up

Remove the source and the empty scratch directory. Go may retain build-cache files outside this directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- limitreader_detect_oversize.go
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on