inancgumus/learngo · error

file size < len(buf)

Error message

file size < len(buf)

What it means

read() fires this error when file.Stat() reports a size less than or equal to len(buf), meaning the file is too short (or empty) to hold the magic-number buffer being checked (e.g. PNG header). The detector treats the file as not matching the format. It is a pre-flight size check before io.ReadFull.

Source

Thrown at advfuncs/08-png-detector/main.go:67

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. Classify the file as invalid format and continue with the next file.
  2. Report the short file to the user with its name and actual size.
  3. Use io.ReadFull's ErrUnexpectedEOF instead of the custom check to signal truncation.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at advfuncs/08-png-detector/main.go:67 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/431331fbd66d423b. Report an issue: GitHub.