{"record":{"id":"5195da7c59f67488","repo":"dotnet/aspnetcore","slug":"cannot-read-message-size","errorCode":null,"errorMessage":"Cannot read message size.","messagePattern":"Cannot read message size\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr-protocol-msgpack/src/BinaryMessageFormat.ts","lineNumber":50,"sourceCode":"    public static parse(input: ArrayBuffer): Uint8Array[] {\n        const result: Uint8Array[] = [];\n        const uint8Array = new Uint8Array(input);\n        const maxLengthPrefixSize = 5;\n        const numBitsToShift = [0, 7, 14, 21, 28 ];\n\n        for (let offset = 0; offset < input.byteLength;) {\n            let numBytes = 0;\n            let size = 0;\n            let byteRead;\n            do {\n                byteRead = uint8Array[offset + numBytes];\n                size = size | ((byteRead & 0x7f) << (numBitsToShift[numBytes]));\n                numBytes++;\n            }\n            while (numBytes < Math.min(maxLengthPrefixSize, input.byteLength - offset) && (byteRead & 0x80) !== 0);\n\n            if ((byteRead & 0x80) !== 0 && numBytes < maxLengthPrefixSize) {\n                throw new Error(\"Cannot read message size.\");\n            }\n\n            if (numBytes === maxLengthPrefixSize && byteRead > 7) {\n                throw new Error(\"Messages bigger than 2GB are not supported.\");\n            }\n\n            if (uint8Array.byteLength >= (offset + numBytes + size)) {\n                // IE does not support .slice() so use subarray\n                result.push(uint8Array.slice\n                    ? uint8Array.slice(offset + numBytes, offset + numBytes + size)\n                    : uint8Array.subarray(offset + numBytes, offset + numBytes + size));\n            } else {\n                throw new Error(\"Incomplete message.\");\n            }\n\n            offset = offset + numBytes + size;\n        }\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/92f07e298d5c5a629385b8b0a32d7164b1b53906/src/SignalR/clients/ts/signalr-protocol-msgpack/src/BinaryMessageFormat.ts#L32-L68","documentation":"BinaryMessageFormat.parse decodes a VarInt length prefix (up to 5 bytes, continuation bit 0x80). If after consuming the available bytes the continuation bit is still set and fewer than 5 bytes were read, the length prefix itself is truncated and unreadable — the frame is corrupt or a partial buffer was handed to parse.","triggerScenarios":"Passing parse() a buffer whose VarInt length prefix is cut off mid-prefix; corrupt or truncated network data; calling parse on a fragment rather than a complete frame.","commonSituations":"Partial reads from a streaming transport; buffer-slicing bugs in custom framing; feeding an incompletely-received chunk directly to the parser.","solutions":["Buffer until at least the full VarInt prefix (and then the declared payload) is available before calling parse.","Use the SignalR transport's built-in framing rather than parsing raw chunks.","If recurring, inspect the transport for data corruption or truncation."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Before parsing, ensure at least one full VarInt prefix is present.\nfunction hasFullVarInt(u8) {\n  for (let i = 0; i < Math.min(5, u8.length); i++) {\n    if ((u8[i] & 0x80) === 0) return true;\n  }\n  return false;\n}","typeGuard":null,"tryCatchPattern":"try {\n  const messages = BinaryMessageFormat.parse(buffer);\n} catch (e) {\n  if (/Cannot read message size/.test(e.message)) {\n    // buffer more bytes and retry once the prefix is complete\n  } else { throw e; }\n}","preventionTips":["Buffer incoming bytes and only parse complete frames.","Use the SignalR transport framing rather than parsing raw chunks.","Log buffer length and prefix bytes when this recurs to spot corruption."],"tags":["signalr","msgpack","binary-protocol","framing"],"analyzedSha":"92f07e298d5c5a629385b8b0a32d7164b1b53906","analyzedAt":"2026-08-07T06:05:17.747Z","schemaVersion":2},"datasetVersion":"2026-08-07T15:17:06.553Z"}