{"record":{"id":"98d127e7fbbdb920","repo":"dotnet/wpf","slug":"read-different-size-from-stream-then-expected-inkserializer","errorCode":null,"errorMessage":"Read different size from stream then expected","messagePattern":"Read different size from stream then expected","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/InkSerializer.cs","lineNumber":222,"sourceCode":"            uint count;\n\n            cb = SerializationHelper.Decode(isfStream, out count);\n            if (cb > cbTotal)\n                throw new ArgumentException(ISFDebugMessage(\"Invalid ISF data\"), nameof(isfStream));\n\n            cbTotal -= cb;\n            if (0 == count)\n                return (cbSize - cbTotal);\n\n            cb = cbTotal;\n\n            byte[] inputdata = new byte[cb];\n\n            // read the stream\n            uint bytesRead = StrokeCollectionSerializer.ReliableRead(isfStream, inputdata, cb);\n            if (cb != bytesRead)\n            {\n                throw new ArgumentException(StrokeCollectionSerializer.ISFDebugMessage(\"Read different size from stream then expected\"), nameof(isfStream));\n            }\n            cbTotal -= cb;\n\n            if (0 != cbTotal)\n                throw new ArgumentException(ISFDebugMessage(\"Invalid ISF data\"), nameof(isfStream));\n\n            return cbSize;\n        }\n\n\n        private bool IsGIFData(Stream inkdata)\n        {\n            Debug.Assert(inkdata != null);\n            long currentPosition = inkdata.Position;\n            try\n            {\n                return ((byte)inkdata.ReadByte() == 'G' &&\n                        (byte)inkdata.ReadByte() == 'I' &&","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/InkSerializer.cs#L204-L240","documentation":"LoadStrokeIds reads the expected number of stroke-ID bytes with StrokeCollectionSerializer.ReliableRead and throws ArgumentException when the bytes actually read differ from the expected count. The stream ended early or returned fewer bytes, so the ISF payload is truncated or inconsistent.","triggerScenarios":"DecodeRawISF on a stream where ReliableRead returns fewer bytes than the declared stroke-ID block size.","commonSituations":"Reading from a network/pipe stream that delivered partial data, truncated .isf files, wrong offsets when slicing ISF out of a container format.","solutions":["Ensure the stream delivers the full ISF payload (read fully into a MemoryStream first)","Verify file integrity (size, checksum) before loading","Re-save the ink data from the original source","Catch ArgumentException and recover with an empty StrokeCollection"],"exampleFix":"// before\nvar strokes = new StrokeCollection(netStream);\n// after\nvar ms = new MemoryStream();\nnetStream.CopyTo(ms); ms.Position = 0;\ntry { var strokes = new StrokeCollection(ms); }\ncatch (ArgumentException) { strokes = new StrokeCollection(); }","handlingStrategy":"fallback","validationCode":"// buffer the full payload so ReliableRead never sees a short stream\nvar ms = new MemoryStream(); source.CopyTo(ms); ms.Position = 0;","typeGuard":"bool FullyBuffered(Stream s) => s is MemoryStream || (s.CanSeek && s.Length > 0);","tryCatchPattern":"try { strokes = new StrokeCollection(ms); }\ncatch (ArgumentException) { strokes = FallbackInk(); }","preventionTips":["Decode from fully buffered MemoryStreams, not live network streams","Verify byte counts after any stream copy","Detect truncation via checksums before decode"],"tags":["ink","isf","stream","truncated-data"],"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"}