{"record":{"id":"03f03fbef867f022","repo":"golang/go","slug":"lzw-unknown-order-03f03f","errorCode":null,"errorMessage":"lzw: unknown order","messagePattern":"lzw: unknown order","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compress/lzw/writer.go","lineNumber":274,"sourceCode":"// is a *[Writer].\nfunc NewWriter(w io.Writer, order Order, litWidth int) io.WriteCloser {\n\treturn newWriter(w, order, litWidth)\n}\n\nfunc newWriter(dst io.Writer, order Order, litWidth int) *Writer {\n\tw := new(Writer)\n\tw.init(dst, order, litWidth)\n\treturn w\n}\n\nfunc (w *Writer) init(dst io.Writer, order Order, litWidth int) {\n\tswitch order {\n\tcase LSB:\n\t\tw.write = (*Writer).writeLSB\n\tcase MSB:\n\t\tw.write = (*Writer).writeMSB\n\tdefault:\n\t\tw.err = errors.New(\"lzw: unknown order\")\n\t\treturn\n\t}\n\tif litWidth < 2 || 8 < litWidth {\n\t\tw.err = fmt.Errorf(\"lzw: litWidth %d out of range\", litWidth)\n\t\treturn\n\t}\n\tbw, ok := dst.(writer)\n\tif !ok && dst != nil {\n\t\tbw = bufio.NewWriter(dst)\n\t}\n\tw.w = bw\n\tlw := uint(litWidth)\n\tw.order = order\n\tw.width = 1 + lw\n\tw.litWidth = lw\n\tw.hi = 1<<lw + 1\n\tw.overflow = 1 << (lw + 1)\n\tw.savedCode = invalidCode","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/compress/lzw/writer.go#L256-L292","documentation":"Writer.init switches on the Order argument and only accepts lzw.LSB and lzw.MSB. Any other value lands in the default branch, sets w.err, and returns. The Writer appears to construct successfully but every subsequent Write returns the sticky error.","triggerScenarios":"Calling lzw.NewWriter(dst, order, litWidth) where order is neither lzw.LSB nor lzw.MSB. Because the constructor returns a non-nil *Writer, callers that ignore the first Write's error can write nothing silently.","commonSituations":"Same shape as the reader variant: zero-value Order variable, refactored enum, or unit test using a sentinel int cast to Order.","solutions":["Pass one of the package constants: lzw.LSB for GIF, lzw.MSB for PDF.","Always check the error returned by the first Write call (or call Flush) — NewWriter itself never returns an error.","Add a guard in your wrapper that rejects Order values outside {LSB, MSB} before constructing the Writer."],"exampleFix":"// before\nw := lzw.NewWriter(dst, lzw.Order(2), 8) // invalid\nn, err := w.Write(data) // err = \"lzw: unknown order\"\n\n// after\nw := lzw.NewWriter(dst, lzw.LSB, 8)\nn, err := w.Write(data)","handlingStrategy":"validation","validationCode":"func validOrder(o lzw.Order) error {\n    switch o {\n    case lzw.LSB, lzw.MSB: return nil\n    default: return fmt.Errorf(\"invalid lzw.Order %d\", o)\n    }\n}\n\nfunc newLZWWriter(dst io.Writer, order lzw.Order, litWidth int) (*lzw.Writer, error) {\n    if err := validOrder(order); err != nil { return nil, err }\n    return lzw.NewWriter(dst, order, litWidth), nil\n}","typeGuard":"func isValidOrder(o lzw.Order) bool {\n    return o == lzw.LSB || o == lzw.MSB\n}","tryCatchPattern":"// lzw.NewWriter never returns an error; check the first Write.\nif _, err := w.Write(data); err != nil {\n    if err.Error() == \"lzw: unknown order\" { /* fix the Order argument */ }\n}","preventionTips":["Never cast arbitrary ints to lzw.Order without bounds-checking.","Always check the first Write's error — the constructor cannot signal it.","Pin the order to the format (LSB for GIF, MSB for PDF) in a single constant."],"tags":["lzw","compression","argument-validation","enum","writer"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}