{"record":{"id":"a4d658842b5d5d8d","repo":"inancgumus/learngo","slug":"file-size-len-buf-a4d658","errorCode":null,"errorMessage":"file size < len(buf)","messagePattern":"file size < len\\(buf\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"advfuncs/08-png-detector-with-panic/main.go","lineNumber":107,"sourceCode":"func 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 fmt.Errorf(\"file size < len(buf)\")\n\t}\n\n\t_, err = io.ReadFull(file, buf)\n\treturn err\n}","sourceCodeStart":89,"sourceCodeEnd":113,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/advfuncs/08-png-detector-with-panic/main.go#L89-L113","documentation":"read() guards against files too small to contain the signature buffer: after Stat(), if the file size is less than or equal to len(buf), there cannot be enough bytes to match a header (e.g. the 8-byte PNG signature), so the error is returned to the detector, which then classifies the file as invalid/unknown. Fires on empty or truncated files.","triggerScenarios":"Thrown at advfuncs/08-png-detector-with-panic/main.go:107 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Treat the error as \"not a valid image\" and skip the file.","Check fi.Size() before reading and handle short files explicitly with a clearer message.","Provide a larger/correct file to the detector."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}