{"record":{"id":"9c9848a8f9ce9358","repo":"dotnet/wpf","slug":"bogus-gorillaencodingtype-passed-to-compress","errorCode":null,"errorMessage":"bogus GorillaEncodingType passed to compress","messagePattern":"bogus GorillaEncodingType passed to compress","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs","lineNumber":438,"sourceCode":"                {\n                    case GorillaEncodingType.Int:\n                        {\n                            bitCount = Native.BitsPerInt;\n                            break;\n                        }\n                    case GorillaEncodingType.Short:\n                        {\n                            bitCount = Native.BitsPerShort;\n                            break;\n                        }\n                    case GorillaEncodingType.Byte:\n                        {\n                            bitCount = Native.BitsPerByte;\n                            break;\n                        }\n                    default:\n                        {\n                            throw new ArgumentException(StrokeCollectionSerializer.ISFDebugMessage(\"bogus GorillaEncodingType passed to compress\"));\n                        }\n                }\n            }\n\n            //have the writer adapt to the List<byte> passed in and write to it\n            BitStreamWriter writer = new BitStreamWriter(compressedData);\n            while (!reader.EndOfStream && unitsToEncode > 0)\n            {\n                int data = GetDataFromReader(reader, encodingType);\n                writer.Write((uint)data, bitCount);\n                unitsToEncode--;\n            }\n        }\n\n        /// <summary>\n        /// Private helper used to read an int, short or byte (in reverse order) from the reader\n        /// and return an int\n        /// </summary>","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs#L420-L456","documentation":"GorillaCodec.Compress switches on the GorillaEncodingType to determine the bit layout; any value outside the known enum members (e.g. Unknown or out-of-range casts) falls into the default case and throws ArgumentException with this message. It guards the bit-packing logic against encodings it cannot write.","triggerScenarios":"Passing an invalid GorillaEncodingType to Compress — commonly GorillaEncodingType.Unknown, a value 0 cast from an uninitialized field, or a value read from a header that was never mapped to a concrete encoding before compression.","commonSituations":"Custom ISF writers that echo an encoding type parsed from data without defaulting it; uninitialized enum fields defaulting to 0 (Unknown); tight casts of int to GorillaEncodingType.","solutions":["Validate/mapping the encoding type before compressing: if it equals Unknown, choose a concrete member like OneByte.","Replace raw casts ((GorillaEncodingType)someInt) with an explicit switch that defaults to a known valid encoding.","Ensure enum fields are explicitly initialized instead of relying on the 0 (Unknown) default.","Catch ArgumentException around compression and retry with an explicit standard encoding."],"exampleFix":"// before\ncodec.Compress(bitCount, reader, parsedEncoding, count, out);\n// after\nvar encoding = parsedEncoding == GorillaEncodingType.Unknown\n    ? GorillaEncodingType.OneByte\n    : parsedEncoding;\ncodec.Compress(bitCount, reader, encoding, count, out);","handlingStrategy":"validation","validationCode":"if (encodingType == GorillaEncodingType.Unknown || !Enum.IsDefined(typeof(GorillaEncodingType), encodingType))\n    encodingType = GorillaEncodingType.OneByte;","typeGuard":"bool IsValidEncoding(GorillaEncodingType t) => t != GorillaEncodingType.Unknown && Enum.IsDefined(typeof(GorillaEncodingType), t);","tryCatchPattern":"try { codec.Compress(bitCount, reader, encodingType, units, outBuf); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"bogus GorillaEncodingType\")) { /* retry with explicit valid encoding */ }","preventionTips":["Never cast raw ints to GorillaEncodingType without Enum.IsDefined validation.","Explicitly initialize encoding enum fields; the 0 default maps to Unknown.","Normalize encodings parsed from headers to known members before compressing."],"tags":["ink","gorilla-codec","enum","compression"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}