inancgumus/learngo · error
file size < len(buf)
Error message
file size < len(buf)
What it means
magic/detect.go's read() stats the file and requires it to be strictly larger than the detection buffer so a full buffer of magic bytes can be read. Files smaller than or equal to len(buf) cannot be identified reliably, so Detect returns this error instead of guessing.
Source
Thrown at magic/detect.go:68
// panic("unknown format: " + format)
return "unknown"
}
// read reads len(buf) bytes to buf from a file
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 errors.New("file size < len(buf)")
}
_, err = io.ReadFull(file, buf)
return err
}
View on GitHub (pinned to 3c475a78e5)
Solutions
- Check the file size and skip/flag files smaller than the buffer length before calling Detect.
- Verify the file was fully written/downloaded (compare against expected size).
- Handle the error and fall back to extension-based or content-based detection.
- If tiny files should be detected, reduce len(buf) or make read() tolerate short reads.
Example fix
// before
fi, _ := file.Stat()
Detect(file)
// after
fi, _ := file.Stat()
if fi.Size() <= int64(bufLen) {
return unknown, fmt.Errorf("file too small (%d bytes) for detection", fi.Size())
}
return Detect(file) Defensive patterns
Strategy: validation
Validate before calling
fi, err := os.Stat(path)
if err != nil { return err }
if fi.Size() <= int64(8 /* magicbuf len */) {
return fmt.Errorf("%s: %d bytes too small for detection", path, fi.Size())
} Type guard
func isDetectable(fi os.FileInfo, bufLen int) bool { return fi.Size() > int64(bufLen) } Try / catch
if err := Detect(f); err != nil {
if err.Error() == "file size < len(buf)" {
return TypeUnknown, fmt.Errorf("cannot detect type of small file")
}
return TypeUnknown, err
} Prevention
- Stat the file and skip files below the buffer size before Detect
- Verify downloads completed (size/checksum) before detection
- Treat empty/truncated files separately from type detection
- Fall back to extension-based detection for tiny files
When it happens
Trigger: Calling Detect on a file whose size <= len(buf) (e.g. a tiny or empty file, a stub, or a truncated download).
Common situations: Detecting type of empty placeholder files, truncated uploads, zero-byte outputs from failed writes, or small config/text files fed to a magic-byte sniffer.
Related errors
- not png
- invalid number
- record.domain cannot be empty
- record.page cannot be empty
- record.visits cannot be negative
AI-assisted analysis of inancgumus/learngo@3c475a78e5 (2026-09-02).
Data as JSON: /api/errors/a19fd33c5e848646.
Report an issue: GitHub.