{"record":{"id":"b31c81ca0d288d21","repo":"golang/go","slug":"lzw-unknown-order","errorCode":null,"errorMessage":"lzw: unknown order","messagePattern":"lzw: unknown order","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compress/lzw/reader.go","lineNumber":271,"sourceCode":"// is a *[Reader].\nfunc NewReader(r io.Reader, order Order, litWidth int) io.ReadCloser {\n\treturn newReader(r, order, litWidth)\n}\n\nfunc newReader(src io.Reader, order Order, litWidth int) *Reader {\n\tr := new(Reader)\n\tr.init(src, order, litWidth)\n\treturn r\n}\n\nfunc (r *Reader) init(src io.Reader, order Order, litWidth int) {\n\tswitch order {\n\tcase LSB:\n\t\tr.read = (*Reader).readLSB\n\tcase MSB:\n\t\tr.read = (*Reader).readMSB\n\tdefault:\n\t\tr.err = errors.New(\"lzw: unknown order\")\n\t\treturn\n\t}\n\tif litWidth < 2 || 8 < litWidth {\n\t\tr.err = fmt.Errorf(\"lzw: litWidth %d out of range\", litWidth)\n\t\treturn\n\t}\n\n\tbr, ok := src.(io.ByteReader)\n\tif !ok && src != nil {\n\t\tbr = bufio.NewReader(src)\n\t}\n\tr.r = br\n\tr.litWidth = litWidth\n\tr.width = 1 + uint(litWidth)\n\tr.clear = uint16(1) << uint(litWidth)\n\tr.eof, r.hi = r.clear+1, r.clear+1\n\tr.overflow = uint16(1) << r.width\n\tr.last = decoderInvalidCode","sourceCodeStart":253,"sourceCodeEnd":289,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/compress/lzw/reader.go#L253-L289","documentation":"Reader.init switches on the Order argument and only accepts lzw.LSB and lzw.MSB. Any other value lands in the default branch, sets r.err to \"lzw: unknown order\", and returns without initializing the bit reader. The same Reader can later be repaired via Reset with a valid Order.","triggerScenarios":"Calling lzw.NewReader(src, order, litWidth) where order is neither lzw.LSB (0) nor lzw.MSB (1) — typically a zero-value of a custom int type cast to Order, or an out-of-range constant.","commonSituations":"Calling NewReader with an uninitialized Order variable, or after refactoring where an enum was renamed and the caller still passes the old (now invalid) value.","solutions":["Pass one of the package constants: lzw.LSB for GIF-style data, lzw.MSB for PDF-style data.","Audit enum-to-Order casts; never cast an arbitrary int to lzw.Order without bounds-checking.","Add a unit test that exercises both orders so a regression is caught at build time."],"exampleFix":"// before\nvar order lzw.Order // zero value is not a valid Order\nr := lzw.NewReader(src, order, 8)\n\n// after: pick explicitly\nr := lzw.NewReader(src, lzw.LSB, 8) // for GIF\n// or lzw.MSB for PDF","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 newLZWReader(src io.Reader, order lzw.Order, litWidth int) (*lzw.Reader, error) {\n    if err := validOrder(order); err != nil { return nil, err }\n    return lzw.NewReader(src, order, litWidth), nil\n}","typeGuard":"func isValidOrder(o lzw.Order) bool {\n    return o == lzw.LSB || o == lzw.MSB\n}","tryCatchPattern":"// lzw.NewReader never returns an error; check the first Read.\nif _, err := r.Read(buf); 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 pass lzw.LSB or lzw.MSB explicitly at the call site.","Add a unit test that exercises both orders so regressions surface."],"tags":["lzw","compression","argument-validation","enum","reader"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}