{"record":{"id":"a826152b19a8d3d9","repo":"golang/go","slug":"zlib-errchecksum","errorCode":"zlib.ErrChecksum","errorMessage":"zlib: invalid checksum","messagePattern":"zlib: invalid checksum","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compress/zlib/reader.go","lineNumber":43,"sourceCode":"\nimport (\n\t\"bufio\"\n\t\"compress/flate\"\n\t\"encoding/binary\"\n\t\"errors\"\n\t\"hash\"\n\t\"hash/adler32\"\n\t\"io\"\n)\n\nconst (\n\tzlibDeflate   = 8\n\tzlibMaxWindow = 7\n)\n\nvar (\n\t// ErrChecksum is returned when reading ZLIB data that has an invalid checksum.\n\tErrChecksum = errors.New(\"zlib: invalid checksum\")\n\t// ErrDictionary is returned when reading ZLIB data that has an invalid dictionary.\n\tErrDictionary = errors.New(\"zlib: invalid dictionary\")\n\t// ErrHeader is returned when reading ZLIB data that has an invalid header.\n\tErrHeader = errors.New(\"zlib: invalid header\")\n)\n\ntype reader struct {\n\tr            flate.Reader\n\tdecompressor io.ReadCloser\n\tdigest       hash.Hash32\n\terr          error\n\tscratch      [4]byte\n}\n\n// Resetter resets a ReadCloser returned by [NewReader] or [NewReaderDict]\n// to switch to a new underlying Reader. This permits reusing a ReadCloser\n// instead of allocating a new one.\ntype Resetter interface {","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/compress/zlib/reader.go#L25-L61","documentation":"Returned by zlib.Reader.Read when the Adler-32 checksum stored in the zlib trailer does not match the Adler-32 of the bytes the deflate decompressor actually produced. Computed via hash/adler32 incrementally, compared once EOF is reached.","triggerScenarios":"Reading from a zlib.NewReader where the trailing 4 bytes (Adler-32, big-endian) disagree with the decompressed payload. Surfaced at the moment the trailer is parsed, i.e., after the last deflate byte has been consumed.","commonSituations":"Truncated HTTP body framed with Content-Encoding: zlib, decompression of a payload that passed through a buggy proxy that re-chunked but did not recompute Adler-32, network byte corruption, or a producer that forgot to flush Close so the trailer is missing.","solutions":["Re-fetch the source payload from the origin; truncation is the most common cause.","If you control the producer, ensure deflater.Close() is called so the Adler-32 trailer is written.","Verify with Python's zlib or `openssl zlib` (where available) to confirm the corruption is in the data, not in your reader wiring.","Wrap the transport in TLS so corruption surfaces as a MAC failure with clearer diagnostics."],"exampleFix":"// before\nif _, err := io.Copy(out, zr); err != nil { return err } // opaque\n\n// after: distinguish checksum failures\nif _, err := io.Copy(out, zr); err != nil {\n    if errors.Is(err, zlib.ErrChecksum) {\n        return fmt.Errorf(\"payload corrupt (adler32 mismatch): %w\", err)\n    }\n    return err\n}","handlingStrategy":"try-catch","validationCode":"import \"hash/adler32\"\n\n// Full pre-validation requires decompressing. Partial checks:\nfunc likelyZlib(data []byte) error {\n    if len(data) < 2 { return errors.New(\"too short\") }\n    cmf, flg := data[0], data[1]\n    if cmf&0x0f != 8 { return zlib.ErrHeader }\n    if (uint16(cmf)<<8 | uint16(flg)) % 31 != 0 { return zlib.ErrHeader }\n    return nil\n}\n// Note: Adler-32 can only be fully validated after decompression.","typeGuard":"func isZlibChecksumErr(err error) bool {\n    return errors.Is(err, zlib.ErrChecksum)\n}","tryCatchPattern":"if _, err := io.Copy(out, zr); err != nil {\n    if errors.Is(err, zlib.ErrChecksum) {\n        return corruptStreamError{kind: \"zlib-adler32\", cause: err}\n    }\n    return err\n}","preventionTips":["Always Close the writer so the Adler-32 trailer is emitted.","Transport zlib over integrity-protected channels (TLS).","For HTTP, prefer gzip over raw deflate/zlib — wider tooling support."],"tags":["zlib","compression","data-integrity","corruption","adler32"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}