{"record":{"id":"705b407f00291b27","repo":"dotnet/wpf","slug":"transform-returned-unexpected-results","errorCode":null,"errorMessage":"Transform returned unexpected results","messagePattern":"Transform returned unexpected results","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs","lineNumber":384,"sourceCode":"            if (bitCount == 0)\n            {\n                //adjust if the bitcount is 0\n                //(this makes bitCount 32)\n                bitCount = (int)(Native.SizeOfInt << 3);\n            }\n\n            //have the writer adapt to the List<byte> passed in and write to it\n            BitStreamWriter writer = new BitStreamWriter(compressedData);\n            if (null != dtxf)\n            {\n                int xfData = 0;\n                int xfExtra = 0;\n                for (int i = startInputIndex; i < input.Length; i++)\n                {\n                    dtxf.Transform(input[i], ref xfData, ref xfExtra);\n                    if (xfExtra != 0)\n                    {\n                        throw new InvalidOperationException(StrokeCollectionSerializer.ISFDebugMessage(\"Transform returned unexpected results\"));\n                    }\n                    writer.Write((uint)xfData, bitCount);\n                }\n            }\n            else\n            {\n                for (int i = startInputIndex; i < input.Length; i++)\n                {\n                    writer.Write((uint)input[i], bitCount);\n                }\n            }\n        }\n\n        /// <summary>\n        /// Compress - compresses the byte[] being read by the BitStreamReader into compressed data\n        /// </summary>\n        /// <param name=\"bitCount\">the number of bits to use for each element</param>\n        /// <param name=\"reader\">a reader over the byte[] to compress</param>","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs#L366-L402","documentation":"During GorillaCodec.Compress, the DeltaDelta data transform (dtxf) writes transformed values into xfData and auxiliary information into xfExtra. The codec assumes xfExtra is always zero for this encoding path; if the transform reports extra output, the serializer throws InvalidOperationException because it cannot represent the result in the chosen bit width. This is an internal contract violation between the transform and the codec.","triggerScenarios":"Compress(bitCount, input, ..., dtxf, ...) with a DeltaDelta instance whose Transform produces non-zero xfExtra — caused by using a transform configured for a different encoding mode or feeding values inconsistent with the transform's expected state.","commonSituations":"Custom or misused DeltaDelta transforms reused across compress calls with stale state; developers experimenting with the ISF codec internals; passing a dtxf from a decode pass into an encode pass.","solutions":["Use a fresh, default-configured DeltaDelta instance for each Compress call instead of reusing stateful transforms.","Verify the DeltaDelta transform is the one designed for the encode path (not the decode path).","Remove custom dtxf usage and pass the standard transform (or match what the ISF serializer constructs internally).","Catch InvalidOperationException during compression and fall back to uncompressed encoding if the API allows."],"exampleFix":"// before: reused transform from decode\ncodec.Compress(bitCount, packets, 0, sharedDtxf, out);\n// after: fresh transform per compress\ncodec.Compress(bitCount, packets, 0, new DeltaDelta(), out);","handlingStrategy":"try-catch","validationCode":"// Use a fresh transform per pass; assert invariant before compress\ndebugAssert: dtxf should be newly constructed for encode paths","typeGuard":"bool IsFreshTransform(DeltaDelta d) => d != null && !d.IsShared;","tryCatchPattern":"try { codec.Compress(bitCount, packets, 0, new DeltaDelta(), outBuf); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Transform returned unexpected results\")) { /* recreate transform or fall back to raw encoding */ }","preventionTips":["Instantiate a new DeltaDelta per compression pass; never share between encode/decode.","Do not implement custom dtxf that emit extra (xfExtra) output.","Add unit tests round-tripping Compress/Uncompress with the exact transform used in production."],"tags":["ink","gorilla-codec","compression","transform"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}