{"record":{"id":"afa1a5ea33967533","repo":"dotnet/wpf","slug":"packed-button-length-not-equal-to-expected-length","errorCode":null,"errorMessage":"Packed button length not equal to expected length","messagePattern":"Packed button length not equal to expected length","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/StrokeSerializer.cs","lineNumber":905,"sourceCode":"                        //\n                        if (bitsToWrite > 0)\n                        {\n                            bitWriter.Write(byteOfButtonData, bitsToWrite);\n                        }\n                        if (fullBytesForButtonsPerPacket > 0)\n                        {\n                            bitsToWrite = Native.BitsPerByte;\n                        }\n                    }\n                }\n\n                // retrieve the button bytes\n                byte[] packedButtonData = bitWriter.ToBytes();\n\n                if (packedButtonData.Length !=\n                    ((buttonCount * pointCount + 7) / Native.BitsPerByte))\n                {\n                    throw new ArgumentException(StrokeCollectionSerializer.ISFDebugMessage(\"Packed button length not equal to expected length\"));\n                }\n\n                // write out the packed button data to the output stream\n                stream.Write(packedButtonData, 0, packedButtonData.Length);\n                localBytesWritten += (uint)packedButtonData.Length;\n            }\n            */\n\n            return localBytesWritten;\n        }\n#if OLD_ISF\n        /// <summary>\n        /// Saves the packets data corresponding to a packet property (identified by the guid) into the stream\n        /// based on the Compression algorithm and compress header\n        /// </summary>\n        /// <param name=\"packetdata\">packet data to save</param>\n        /// <param name=\"stream\">null to calculate only the size</param>\n        /// <param name=\"compressor\"></param>","sourceCodeStart":887,"sourceCodeEnd":923,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/StrokeSerializer.cs#L887-L923","documentation":"Thrown by StrokeSerializer.SavePackets (invoked from cbWrite) when packing stroke button data for ISF output: the BitWriter produced a byte array whose length differs from the computed expected size (buttonCount*pointCount+7)/8. This is an internal invariant check during serialization.","triggerScenarios":"Saving a StrokeCollection to ISF where a Stroke's button data has an internal length inconsistent with its packet description — usually only reachable with programmatically constructed/extended Stroke data.","commonSituations":"Custom stroke subclasses overriding drawing attributes or packet data incorrectly, ink data built by interop code with mismatched button counts.","solutions":["Verify any custom Stroke/StrokeData construction sets button counts and point counts consistently","Use standard StylusPointCollection-based strokes rather than hand-built packet data","If extending ink serialization, ensure buttonCount matches the actual button bits written","Report as a bug if hit with stock StrokeCollection data"],"exampleFix":"// before\nvar stroke = new CustomStroke(rawPacketData); // buttonCount mismatch\n// after\nvar stroke = new Stroke(new StylusPointCollection(points));","handlingStrategy":"validation","validationCode":"// Ensure stroke was built from a StylusPointCollection, not raw packet data\nbool WellFormedStroke(Stroke s) => s != null && s.StylusPoints != null && s.StylusPoints.Count > 0;","typeGuard":"bool WellFormedStroke(Stroke s) => s?.StylusPoints?.Count > 0;","tryCatchPattern":"try { stream.Save(ms); }\ncatch (ArgumentException ex) { throw new InvalidOperationException(\"Stroke serialization invariant broken\", ex); }","preventionTips":["Construct strokes via StylusPointCollection, not hand-built packet tables","If subclassing Stroke, keep button/point counts consistent","Report framework bugs if hit with stock strokes","Serialize only strokes originating from real stylus input or validated data"],"tags":["ink","isf","serialization","invariant"],"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"}