{"record":{"id":"780d755f45aa53af","repo":"jackc/pgx","slug":"invalid-length-for-bit-varbit-w","errorCode":null,"errorMessage":"invalid length for bit/varbit: %w","messagePattern":"invalid length for bit/varbit: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgtype/bits.go","lineNumber":174,"sourceCode":"\t\treturn nil, err\n\t}\n\treturn box, nil\n}\n\ntype scanPlanBinaryBitsToBitsScanner struct{}\n\nfunc (scanPlanBinaryBitsToBitsScanner) 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\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","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgtype/bits.go#L156-L192","documentation":"Reading the 4-byte bit-length header of a binary bit/varbit value failed (truncated or unreadable message); the wrapped pgio error names the underlying read problem.","triggerScenarios":"Thrown at pgtype/bits.go:174 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Fail the scan; the binary payload is malformed","Check for truncated network data or a proxy altering binary results","Verify the column is actually bit/varbit and binary format is being used correctly"],"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"}