{"record":{"id":"e3fc73224bbbe2eb","repo":"dotnet/wpf","slug":"button-data-length-not-equal-to-expected-length","errorCode":null,"errorMessage":"Button data length not equal to expected length","messagePattern":"Button data 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":518,"sourceCode":"                int byteCounter = 0;\n                while (!bitReader.EndOfStream)\n                {\n                    // unpack the fully bytes first\n                    for (int fullBytes = 0; fullBytes < fullBytesForButtonsPerPacket; fullBytes++)\n                    {\n                        buttonData[byteCounter++] = bitReader.ReadByte(Native.BitsPerByte);\n                    }\n                    // then unpack a single partial byte if necessary\n                    if (partialBitsForButtonsPerPacket > 0)\n                    {\n                        buttonData[byteCounter++] = bitReader.ReadByte((int)partialBitsForButtonsPerPacket);\n                    }\n                }\n\n                // if the number of bytes allocated != necessary byte amount then an error occurred\n                if (byteCounter != buttonData.Length)\n                {\n                    throw new ArgumentException(StrokeCollectionSerializer.ISFDebugMessage(\"Button data length not equal to expected length\"));\n                }\n\n                //\n                // set the point data in the raw array\n                //\n                FillButtonData( (int)pointCount,\n                                buttonCount,\n                                valueIntsPerPoint, //gives the first button index\n                                rawPointData,\n                                buttonData);\n            }\n\n\n            stylusPoints = new StylusPointCollection(stylusPointDescription, rawPointData, null, transform);\n\n            // if we read too far into the stream (e.g. the packets were compressed)\n            //      then move the stream pointer back to the end of the actual packet\n            //      data before returning. This keeps the return value on the function","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/StrokeSerializer.cs#L500-L536","documentation":"Thrown by StrokeSerializer.LoadPackets after unpacking per-packet button bits: the number of bytes actually accumulated (byteCounter) does not match the pre-allocated buttonData.Length. This is a post-decode consistency check confirming the ISF button section was well-formed.","triggerScenarios":"DecodeISFIntoStroke reads button bits whose packed/unpacked byte counts disagree — internal inconsistency in the ISF stream's button descriptors.","commonSituations":"ISF streams from incompatible ink runtimes, corrupted clipboard ink data, streams altered after serialization.","solutions":["Regenerate the ISF stream with StrokeCollection.Save and reload","Verify no byte-level transformation (encoding, compression) altered the stream between save and load","Use matching .NET Framework/WPF versions on both ends","Catch ArgumentException during decode and skip invalid strokes"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Round-trip test the ISF before trusting it\nvar ms = new MemoryStream(); strokeCollection.Save(ms); // valid baseline\n","typeGuard":null,"tryCatchPattern":"try { DecodeISFIntoStroke(isfStream); }\ncatch (ArgumentException ex) { MessageBox.Show(\"Ink data is corrupted: \" + ex.Message); }","preventionTips":["Only load ISF produced by the same or compatible ink stack","Never mutate ISF bytes between save and load","Include ISF round-trip tests when persisting ink","Checksum ISF payloads stored/transported long-term"],"tags":["ink","isf","deserialization","consistency-check"],"backgroundTag":"schema-validation-failed","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"}