inancgumus/learngo · error

file size < len(buf)

Error message

file size < len(buf)

What it means

read() size guard: when Stat() shows the file is no larger than the byte buffer, there are too few bytes to contain the expected image signature, so the generic error is returned and detect marks the file as invalid. Typical causes: empty files or files truncated below the header length.

Source

Thrown at advfuncs/10-image-detector-recover/main.go:95

func read(filename string, buf []byte) error {
	file, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer file.Close()

	fi, err := file.Stat()
	if err != nil {
		return err
	}

	if fi.Size() <= int64(len(buf)) {
		return fmt.Errorf("file size < len(buf)")
	}

	_, err = io.ReadFull(file, buf)
	return err
}

View on GitHub (pinned to 3c475a78e5)

Solutions

  1. Skip files reported too small; they cannot be valid images.
  2. Improve the message to include the filename and required vs actual size.
  3. Read with io.ReadFull and handle io.ErrUnexpectedEOF for a standard-truncation signal.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at advfuncs/10-image-detector-recover/main.go:95 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of inancgumus/learngo@3c475a78e5 (2026-09-02). Data as JSON: /api/errors/807dcf331215f476. Report an issue: GitHub.