{"record":{"id":"8ea9927ebc3c1c08","repo":"denoland/deno","slug":"readrawbytes-failed","errorCode":null,"errorMessage":"ReadRawBytes() failed","messagePattern":"ReadRawBytes\\(\\) failed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/v8.ts","lineNumber":424,"sourceCode":"    }\n    this.buffer = buffer;\n    this[kHandle] = op_v8_new_deserializer(this, buffer);\n  }\n  readRawBytes(length: number): Buffer {\n    const offset = this._readRawBytes(length);\n    // `this.buffer` is the Deserializer's own field, not a TypedArray getter.\n    // deno-lint-ignore deno-internal/prefer-primordials\n    const view = this.buffer;\n    return Buffer.from(\n      getViewBuffer(view),\n      getViewByteOffset(view) + offset,\n      length,\n    );\n  }\n  _readRawBytes(length: number): number {\n    const offset = op_v8_read_raw_bytes(this[kHandle], length);\n    if (offset < 0) {\n      throw new Error(\"ReadRawBytes() failed\");\n    }\n    return offset;\n  }\n  getWireFormatVersion(): number {\n    return op_v8_get_wire_format_version(this[kHandle]);\n  }\n  readDouble(): number {\n    return op_v8_read_double(this[kHandle]);\n  }\n  readHeader(): boolean {\n    return op_v8_read_header(this[kHandle]);\n  }\n\n  readUint32(): number {\n    return op_v8_read_uint32(this[kHandle]);\n  }\n  readUint64(): [hi: number, lo: number] {\n    return op_v8_read_uint64(this[kHandle]);","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/v8.ts#L406-L442","documentation":"Deserializer#_readRawBytes(length) asks the op for length bytes; the op returns their offset in the backing store, or a negative value when fewer bytes remain. A negative offset is converted to a plain Error('ReadRawBytes() failed'), meaning the stream is exhausted: the buffer is truncated, or the reader is out of sync with the writer's format.","triggerScenarios":"Calling readRawBytes(n) with n larger than the bytes remaining at the current position — truncated payloads, wire-format drift between runtimes, or a subclass reading fields in the wrong order / without consuming the header first.","commonSituations":"Deserializing payloads produced by a different Node/V8/Deno version; socket reads that stop at a chunk boundary mid-payload; custom _readHostObject implementations parsing an older layout.","solutions":["Validate the stream before field reads: readHeader() must return true and getWireFormatVersion() must match the writer's version.","Verify payload completeness before deserializing (length prefix or checksum), especially over sockets.","In custom readers, track the position and check remaining bytes before each readRawBytes call.","Re-serialize data with the current runtime when it originates from another version."],"exampleFix":"// before\nconst d = new v8.Deserializer(buf);\nd.readHeader();\nconst id = d.readRawBytes(16); // throws if fewer bytes remain\n\n// after\nconst d = new v8.Deserializer(buf);\nif (!d.readHeader() || d.getWireFormatVersion() !== EXPECTED_VERSION) {\n  throw new Error(\"unsupported payload\");\n}\nconst id = d.readRawBytes(16);","handlingStrategy":"try-catch","validationCode":"const d = new v8.Deserializer(buf);\nif (!d.readHeader()) throw new Error(\"not a v8 serialization stream\");","typeGuard":null,"tryCatchPattern":"try {\n  return decode(payload);\n} catch (err) {\n  if (String(err?.message).includes(\"ReadRawBytes() failed\")) {\n    throw new Error(\"payload truncated or wire-format mismatch\", { cause: err });\n  }\n  throw err;\n}","preventionTips":["Treat v8-serialized bytes as versioned: store the wire-format version beside the payload.","Length-prefix or checksum payloads that cross process/network boundaries.","Don't hand-roll readers over foreign buffers; re-serialize when the producer's runtime changes."],"tags":["v8","deserialization","truncated-data","buffer"],"backgroundTag":"corrupt-serialization-data","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}