{"record":{"id":"a19fd33c5e848646","repo":"inancgumus/learngo","slug":"file-size-len-buf","errorCode":null,"errorMessage":"file size < len(buf)","messagePattern":"file size < len\\(buf\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"magic/detect.go","lineNumber":68,"sourceCode":"\t// panic(\"unknown format: \" + format)\n\treturn \"unknown\"\n}\n\n// read reads len(buf) bytes to buf from a file\nfunc read(filename string, buf []byte) error {\n\tfile, err := os.Open(filename)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer file.Close()\n\n\tfi, err := file.Stat()\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif fi.Size() <= int64(len(buf)) {\n\t\treturn errors.New(\"file size < len(buf)\")\n\t}\n\n\t_, err = io.ReadFull(file, buf)\n\treturn err\n}\n","sourceCodeStart":50,"sourceCodeEnd":74,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/magic/detect.go#L50-L74","documentation":"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.","triggerScenarios":"Calling Detect on a file whose size <= len(buf) (e.g. a tiny or empty file, a stub, or a truncated download).","commonSituations":"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.","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."],"exampleFix":"// before\nfi, _ := file.Stat()\nDetect(file)\n// after\nfi, _ := file.Stat()\nif fi.Size() <= int64(bufLen) {\n    return unknown, fmt.Errorf(\"file too small (%d bytes) for detection\", fi.Size())\n}\nreturn Detect(file)","handlingStrategy":"validation","validationCode":"fi, err := os.Stat(path)\nif err != nil { return err }\nif fi.Size() <= int64(8 /* magicbuf len */) {\n    return fmt.Errorf(\"%s: %d bytes too small for detection\", path, fi.Size())\n}","typeGuard":"func isDetectable(fi os.FileInfo, bufLen int) bool { return fi.Size() > int64(bufLen) }","tryCatchPattern":"if err := Detect(f); err != nil {\n    if err.Error() == \"file size < len(buf)\" {\n        return TypeUnknown, fmt.Errorf(\"cannot detect type of small file\")\n    }\n    return TypeUnknown, err\n}","preventionTips":["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"],"tags":["file-io","validation","magic-bytes"],"backgroundTag":"file-too-small","analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}