{"id":"0f6349197848d7cd","repo":"go-redis/redis","slug":"redis-buffer-too-small-need-d-bytes-have-d","errorCode":null,"errorMessage":"redis: buffer too small: need %d bytes, have %d","messagePattern":"redis: buffer too small: need (.+?) bytes, have (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/proto/reader.go","lineNumber":601,"sourceCode":"// after a bufio refill can be misinterpreted as the verbatim format tag.\n//\n// If the bulk payload does not fit in buf, an error is returned and the\n// payload plus the trailing CRLF are drained from the reader so the\n// connection stays aligned for the next reply. For simple-string / integer\n// / float responses the payload lives in the (already-consumed) header\n// line, so no drain is needed.\nfunc (r *Reader) ReadStringInto(buf []byte) (int, error) {\n\tline, err := r.ReadLine()\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\n\tswitch line[0] {\n\tcase RespStatus:\n\t\t// Simple string — data is in the line itself.\n\t\ts := line[1:]\n\t\tif len(s) > len(buf) {\n\t\t\treturn 0, fmt.Errorf(\"redis: buffer too small: need %d bytes, have %d\", len(s), len(buf))\n\t\t}\n\t\treturn copy(buf, s), nil\n\n\tcase RespString:\n\t\tn, err := replyLen(line)\n\t\tif err != nil {\n\t\t\treturn 0, err\n\t\t}\n\t\tif n > len(buf) {\n\t\t\t// Drain the payload + trailing \\r\\n so the next read on this\n\t\t\t// connection sees the start of the next reply rather than the\n\t\t\t// tail of this one. Otherwise the unread bytes corrupt the\n\t\t\t// stream and the bad connection gets handed back to the pool.\n\t\t\tif _, derr := r.rd.Discard(n + 2); derr != nil {\n\t\t\t\treturn 0, derr\n\t\t\t}\n\t\t\treturn 0, fmt.Errorf(\"redis: buffer too small: need %d bytes, have %d\", n, len(buf))\n\t\t}","sourceCodeStart":583,"sourceCodeEnd":619,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/internal/proto/reader.go#L583-L619","documentation":"Error \"redis: buffer too small: need %d bytes, have %d\" thrown in go-redis/redis.","triggerScenarios":"Thrown at internal/proto/reader.go:601 when the library encounters an invalid state.","commonSituations":"Size buffers from the announced reply length before copying.","solutions":["Increase the destination buffer size (ReadBufferSize) to fit the reply","Use the non-buffered read variant for large values"],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}