{"record":{"id":"a5748d09e6f7a0cd","repo":"golang/go","slug":"gzip-write-extra-data-is-too-large","errorCode":null,"errorMessage":"gzip.Write: Extra data is too large","messagePattern":"gzip\\.Write: Extra data is too large","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compress/gzip/gzip.go","lineNumber":103,"sourceCode":"\t\t},\n\t\tw:          w,\n\t\tlevel:      level,\n\t\tcompressor: compressor,\n\t}\n}\n\n// Reset discards the [Writer] z's state and makes it equivalent to the\n// result of its original state from [NewWriter] or [NewWriterLevel], but\n// writing to w instead. This permits reusing a [Writer] rather than\n// allocating a new one.\nfunc (z *Writer) Reset(w io.Writer) {\n\tz.init(w, z.level)\n}\n\n// writeBytes writes a length-prefixed byte slice to z.w.\nfunc (z *Writer) writeBytes(b []byte) error {\n\tif len(b) > 0xffff {\n\t\treturn errors.New(\"gzip.Write: Extra data is too large\")\n\t}\n\tle.PutUint16(z.buf[:2], uint16(len(b)))\n\t_, err := z.w.Write(z.buf[:2])\n\tif err != nil {\n\t\treturn err\n\t}\n\t_, err = z.w.Write(b)\n\treturn err\n}\n\n// writeString writes a UTF-8 string s in GZIP's format to z.w.\n// GZIP (RFC 1952) specifies that strings are NUL-terminated ISO 8859-1 (Latin-1).\nfunc (z *Writer) writeString(s string) (err error) {\n\t// GZIP stores Latin-1 strings; error if non-Latin-1; convert if non-ASCII.\n\tneedconv := false\n\tfor _, v := range s {\n\t\tif v == 0 || v > 0xff {\n\t\t\treturn errors.New(\"gzip.Write: non-Latin-1 header string\")","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/compress/gzip/gzip.go#L85-L121","documentation":"gzip.Writer.writeBytes rejects any byte slice longer than 0xffff (65535) because the GZIP FEXTRA field stores its length in a 2-byte little-endian XLEN prefix. writeBytes is used internally when emitting the Header.Extra bytes during the next Write/Flush call, so the error propagates from those methods.","triggerScenarios":"Setting gz.Header.Extra (or passing Extra via the Writer zero-value before Reset) to a slice whose length exceeds 65535, then calling Write, Flush, or Close on the writer. The check fires before any byte is written to the underlying writer.","commonSituations":"Embedding a metadata blob, JSON manifest, signing payload, or trace context in the GZIP extra field and exceeding the 64 KiB ceiling; copying Extra from a different format that has no such limit; appending authentication tags without bound.","solutions":["Trim or chunk the Extra payload so len(Header.Extra) <= 65535 bytes.","Move the oversized metadata out of the GZIP header into a sidecar file or a separate framing layer.","If you need arbitrary-length metadata, define your own container format that wraps the gzip stream rather than abusing FEXTRA.","Validate the length once when assigning Header.Extra rather than discovering the error on every Write."],"exampleFix":"// before\ngz.Header.Extra = bigMetadataBlob // > 64 KiB\n// \"gzip.Write: Extra data is too large\"\n\n// after: cap and spill\nconst maxExtra = 0xffff\nif len(bigMetadataBlob) > maxExtra {\n    return fmt.Errorf(\"metadata %d bytes exceeds gzip FEXTRA limit %d\",\n        len(bigMetadataBlob), maxExtra)\n}\ngz.Header.Extra = bigMetadataBlob","handlingStrategy":"validation","validationCode":"const gzipMaxExtra = 0xffff\n\nfunc setExtra(h *gzip.Header, extra []byte) error {\n    if len(extra) > gzipMaxExtra {\n        return fmt.Errorf(\"extra field %d bytes exceeds %d\", len(extra), gzipMaxExtra)\n    }\n    h.Extra = extra\n    return nil\n}","typeGuard":null,"tryCatchPattern":"gz.Header.Extra = blob\nif _, err := gz.Write(nil); err != nil { // or first Write\n    if strings.Contains(err.Error(), \"Extra data is too large\") {\n        return oversizeExtraError{len: len(blob)}\n    }\n}","preventionTips":["Cap Header.Extra at 64 KiB at the boundary that produces it.","Move large metadata to a sidecar file or wrapper framing.","Add a unit test that exercises Extra at boundary lengths (0, 0xffff, 0xffff+1)."],"tags":["gzip","compression","header-validation","size-limit","writer"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}