dotnet/wpf · error · ArgumentException

bogus GorillaEncodingType passed to compress

Error message

bogus GorillaEncodingType passed to compress

What it means

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.

Solutions

  1. Validate/mapping the encoding type before compressing: if it equals Unknown, choose a concrete member like OneByte.
  2. Replace raw casts ((GorillaEncodingType)someInt) with an explicit switch that defaults to a known valid encoding.
  3. Ensure enum fields are explicitly initialized instead of relying on the 0 (Unknown) default.
  4. Catch ArgumentException around compression and retry with an explicit standard encoding.

Example fix

// before
codec.Compress(bitCount, reader, parsedEncoding, count, out);
// after
var encoding = parsedEncoding == GorillaEncodingType.Unknown
    ? GorillaEncodingType.OneByte
    : parsedEncoding;
codec.Compress(bitCount, reader, encoding, count, out);
Defensive patterns

Strategy: validation

Validate before calling

if (encodingType == GorillaEncodingType.Unknown || !Enum.IsDefined(typeof(GorillaEncodingType), encodingType))
    encodingType = GorillaEncodingType.OneByte;

Type guard

bool IsValidEncoding(GorillaEncodingType t) => t != GorillaEncodingType.Unknown && Enum.IsDefined(typeof(GorillaEncodingType), t);

Try / catch

try { codec.Compress(bitCount, reader, encodingType, units, outBuf); }
catch (ArgumentException ex) when (ex.Message.Contains("bogus GorillaEncodingType")) { /* retry with explicit valid encoding */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/9c9848a8f9ce9358. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs:438

                {
                    case GorillaEncodingType.Int:
                        {
                            bitCount = Native.BitsPerInt;
                            break;
                        }
                    case GorillaEncodingType.Short:
                        {
                            bitCount = Native.BitsPerShort;
                            break;
                        }
                    case GorillaEncodingType.Byte:
                        {
                            bitCount = Native.BitsPerByte;
                            break;
                        }
                    default:
                        {
                            throw new ArgumentException(StrokeCollectionSerializer.ISFDebugMessage("bogus GorillaEncodingType passed to compress"));
                        }
                }
            }

            //have the writer adapt to the List<byte> passed in and write to it
            BitStreamWriter writer = new BitStreamWriter(compressedData);
            while (!reader.EndOfStream && unitsToEncode > 0)
            {
                int data = GetDataFromReader(reader, encodingType);
                writer.Write((uint)data, bitCount);
                unitsToEncode--;
            }
        }

        /// <summary>
        /// Private helper used to read an int, short or byte (in reverse order) from the reader
        /// and return an int
        /// </summary>

View on GitHub (pinned to 81131a70a4)