{"record":{"id":"e8680655bbc0be6b","repo":"golang/go","slug":"lzw-invalid-code","errorCode":null,"errorMessage":"lzw: invalid code","messagePattern":"lzw: invalid code","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compress/lzw/reader.go","lineNumber":198,"sourceCode":"\t\t\t\tr.output[i] = uint8(c)\n\t\t\t\ti--\n\t\t\t\tc = r.last\n\t\t\t}\n\t\t\t// Copy the suffix chain into output and then write that to w.\n\t\t\tfor c >= r.clear {\n\t\t\t\tr.output[i] = r.suffix[c]\n\t\t\t\ti--\n\t\t\t\tc = r.prefix[c]\n\t\t\t}\n\t\t\tr.output[i] = uint8(c)\n\t\t\tr.o += copy(r.output[r.o:], r.output[i:])\n\t\t\tif r.last != decoderInvalidCode {\n\t\t\t\t// Save what the hi code expands to.\n\t\t\t\tr.suffix[r.hi] = uint8(c)\n\t\t\t\tr.prefix[r.hi] = r.last\n\t\t\t}\n\t\tdefault:\n\t\t\tr.err = errors.New(\"lzw: invalid code\")\n\t\t\tbreak loop\n\t\t}\n\t\tr.last, r.hi = code, r.hi+1\n\t\tif r.hi >= r.overflow {\n\t\t\tif r.hi > r.overflow {\n\t\t\t\tpanic(\"unreachable\")\n\t\t\t}\n\t\t\tif r.width == maxWidth {\n\t\t\t\tr.last = decoderInvalidCode\n\t\t\t\t// Undo the d.hi++ a few lines above, so that (1) we maintain\n\t\t\t\t// the invariant that d.hi < d.overflow, and (2) d.hi does not\n\t\t\t\t// eventually overflow a uint16.\n\t\t\t\tr.hi--\n\t\t\t} else {\n\t\t\t\tr.width++\n\t\t\t\tr.overflow = 1 << r.width\n\t\t\t}\n\t\t}","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/compress/lzw/reader.go#L180-L216","documentation":"The LZW decoder hit its default switch branch, meaning the code read from the stream did not correspond to any defined literal or dictionary entry for the current decoder state. The reader sets r.err and stops decoding; further Reads return the same error.","triggerScenarios":"Calling Read on an lzw.Reader fed a malformed compressed stream — codes that exceed the current dictionary width, point at not-yet-defined entries, or appear after the decoder has hit its overflow handling. Typical when the producer and consumer disagree on litWidth or bit order.","commonSituations":"Decoding a GIF whose LZW stream was truncated, decoding a PDF ASCII85/LZW stream with the wrong EndOfInformation handling, mismatched LSB/MSB order between encoder and decoder, litWidth mismatch (decoder built with litWidth=8 but encoder used 7), or bit-rot in a binary payload.","solutions":["Verify the Order (LSB vs MSB) matches the producer: GIF uses LSB, PDF uses MSB.","Verify the litWidth matches: GIF mandates 2..8 (typically 8 for full-color), PDF typically 8.","Re-encode or re-transmit the source; this error almost always indicates corruption rather than recoverable input.","If you control the producer, ensure it emits a clear code and respects the width-bump invariants described in the spec the consumer implements."],"exampleFix":"// before: wrong order for a PDF LZW stream\nr := lzw.NewReader(src, lzw.LSB, 8) // GIF order on PDF data\n\n// after: match the format's bit order\nr := lzw.NewReader(src, lzw.MSB, 8) // PDF uses MSB\ndefer r.Close()","handlingStrategy":"try-catch","validationCode":"// No purely static validation: correctness depends on encoder/decoder agreement.\n// Document the contract instead:\n//   - GIF LZW: lzw.LSB, litWidth per GIF spec (typically 8)\n//   - PDF LZW: lzw.MSB, litWidth per /BitsPerComponent (typically 8)\n// Validate the order/litWidth pair against the format spec before constructing.","typeGuard":null,"tryCatchPattern":"n, err := r.Read(buf)\nif err != nil && err.Error() == \"lzw: invalid code\" {\n    // The stream is corrupt or order/litWidth mismatch.\n    // Re-encode the source or re-fetch a known-good copy.\n    return corruptLZWStream{cause: err}\n}","preventionTips":["Pin the order and litWidth to the format spec at construction time.","Round-trip test your encoder against the decoder before shipping.","Verify bit order in code review — LSB vs MSB is the #1 LZW bug."],"tags":["lzw","compression","data-integrity","corruption","decoder"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}