{"record":{"id":"e4630fee14d97024","repo":"jackc/pgx","slug":"invalid-length-for-bit-varbit-bitlen-d-w","errorCode":null,"errorMessage":"invalid length for bit/varbit: bitLen=%d: %w","messagePattern":"invalid length for bit/varbit: bitLen=(.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgtype/bits.go","lineNumber":184,"sourceCode":"\tif src == nil {\n\t\treturn scanner.ScanBits(Bits{})\n\t}\n\n\tr := pgio.NewReader(src)\n\n\tbitLen := r.Int32()\n\tif err := r.Err(); err != nil {\n\t\treturn fmt.Errorf(\"invalid length for bit/varbit: %w\", err)\n\t}\n\tif bitLen < 0 {\n\t\treturn fmt.Errorf(\"invalid length for bit/varbit: bitLen=%d\", bitLen)\n\t}\n\n\t// Finish rejects trailing bytes, so together with the read below the data\n\t// must be exactly the number of bytes bitLen calls for.\n\tdata := r.Bytes((int(bitLen) + 7) / 8)\n\tif err := r.Finish(); err != nil {\n\t\treturn fmt.Errorf(\"invalid length for bit/varbit: bitLen=%d: %w\", bitLen, err)\n\t}\n\n\tbuf := make([]byte, len(data))\n\tcopy(buf, data)\n\n\treturn scanner.ScanBits(Bits{Bytes: buf, Len: bitLen, Valid: true})\n}\n\ntype scanPlanTextAnyToBitsScanner struct{}\n\nfunc (scanPlanTextAnyToBitsScanner) Scan(src []byte, dst any) error {\n\tscanner := (dst).(BitsScanner)\n\n\tif src == nil {\n\t\treturn scanner.ScanBits(Bits{})\n\t}\n\n\tbitLen := len(src)","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgtype/bits.go#L166-L202","documentation":"After reading (bitLen+7)/8 bytes of bit data, the reader had leftover bytes (Finish failed): the message length is inconsistent with the declared bit length. The bitLen value is included to show the mismatch.","triggerScenarios":"Thrown at pgtype/bits.go:184 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Treat the payload as corrupt and fail the scan","Ensure custom encoders write exactly the bytes implied by the length header","Check for protocol-level corruption (proxy, truncation)"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}