{"record":{"id":"2d485a274710fc90","repo":"valyala/fasthttp","slug":"bufio-read-returned-d-nil","errorCode":null,"errorMessage":"bufio read returned (%d, nil)","messagePattern":"bufio read returned \\((.+?), nil\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"http.go","lineNumber":2879,"sourceCode":"\treturn b, nil\n}\n\nfunc readBodyIdentity(r *bufio.Reader, maxBodySize int, dst []byte) ([]byte, error) {\n\tdst = dst[:cap(dst)]\n\tif len(dst) == 0 {\n\t\tdst = make([]byte, 1024)\n\t}\n\toffset := 0\n\tfor {\n\t\tnn, err := r.Read(dst[offset:])\n\t\tif nn <= 0 {\n\t\t\tswitch {\n\t\t\tcase errors.Is(err, io.EOF):\n\t\t\t\treturn dst[:offset], nil\n\t\t\tcase err != nil:\n\t\t\t\treturn dst[:offset], err\n\t\t\tdefault:\n\t\t\t\treturn dst[:offset], fmt.Errorf(\"bufio read returned (%d, nil)\", nn)\n\t\t\t}\n\t\t}\n\t\toffset += nn\n\t\tif maxBodySize > 0 && offset > maxBodySize {\n\t\t\treturn dst[:offset], ErrBodyTooLarge\n\t\t}\n\t\tif len(dst) == offset {\n\t\t\tn := roundUpForSliceCap(2 * offset)\n\t\t\tif maxBodySize > 0 && n > maxBodySize {\n\t\t\t\tn = maxBodySize + 1\n\t\t\t}\n\t\t\tb := make([]byte, n)\n\t\t\tcopy(b, dst)\n\t\t\tdst = b\n\t\t}\n\t}\n}\n","sourceCodeStart":2861,"sourceCodeEnd":2897,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/http.go#L2861-L2897","documentation":"This error is returned by readBodyIdentity in fasthttp when the underlying bufio.Reader.Read call violates the io.Reader contract by returning 0 bytes read with a nil error. The io.Reader contract requires that a Read returning n <= 0 must also return a non-nil error, so this guards against a misbehaving underlying reader (custom conns, wrapped transports). It is a defensive invariant check, not a protocol error.","triggerScenarios":"Reading an identity-encoded (non-chunked) request/response body via Server/Client when the conn's bufio.Reader.Read returns (0, nil) — typically caused by a custom net.Conn or a wrapper implementing io.Reader incorrectly.","commonSituations":"Custom transport/wrapper code (compression, logging, TLS-shim conns) around the connection returning zero bytes without an error; testing with mocked conns that don't follow the io.Reader contract.","solutions":["Fix the custom io.Reader/net.Conn wrapper so it never returns n==0 with err==nil; return io.EOF at end of stream instead.","Check recently added middleware or conn wrappers and replace them with a contract-conforming implementation.","If it reproduces with stock fasthttp and no wrappers, report it to valyala/fasthttp with a minimal repro.","As a workaround, use a plain connection without the custom wrapper to confirm it is the source."],"exampleFix":"// before (bad custom reader)\nfunc (r *myConn) Read(p []byte) (int, error) {\n    if r.pending == 0 { return 0, nil } // violates io.Reader contract\n    ...\n}\n// after\nfunc (r *myConn) Read(p []byte) (int, error) {\n    if r.pending == 0 { return 0, io.EOF }\n    ...\n}","handlingStrategy":"try-catch","validationCode":"// Audit any custom io.Reader/net.Conn wrappers before enabling:\n// they must never return (0, nil).\nfunc checkReader(r io.Reader) error {\n    buf := make([]byte, 1)\n    n, err := r.Read(buf)\n    if n == 0 && err == nil {\n        return errors.New(\"reader violates io.Reader contract: (0, nil)\")\n    }\n    return nil\n}","typeGuard":"if err != nil && strings.Contains(err.Error(), \"bufio read returned\") {\n    // invariant violation: reader returned (0, nil); not an ErrBrokenChunk\n}","tryCatchPattern":"b, err := ... // body read\nif err != nil {\n    if strings.Contains(err.Error(), \"bufio read returned\") {\n        log.Printf(\"underlying reader violates io.Reader contract: %v\", err)\n        // replace/inspect custom conn wrapper\n    }\n    return err\n}","preventionTips":["Never implement io.Reader wrappers that return (0, nil); always return io.EOF or a real error with zero bytes.","Fuzz-test custom connection wrappers against the io.Reader contract before deploying.","Prefer wrapping net.Conn at the transport level with well-tested libraries.","Keep fasthttp updated; watch changelogs for body-reading fixes."],"tags":["fasthttp","io-reader-contract","body-read","bufio"],"backgroundTag":"bufio-read-returned-zero-nil","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}