{"id":"2bfd018e0aba3f98","repo":"mongodb/node-mongodb-native","slug":"negative-binary-type-element-size-found-for-subtyp","errorCode":null,"errorMessage":"Negative binary type element size found for subtype 0x02","messagePattern":"Negative binary type element size found for subtype 0x02","errorType":"exception","errorClass":"BSONError","httpStatus":null,"severity":"error","filePath":"src/cmap/wire_protocol/on_demand/document.ts","lineNumber":204,"sourceCode":"        return NumberUtils.getInt32LE(this.bson, offset);\n      case BSONType.long:\n        return NumberUtils.getBigInt64LE(this.bson, offset);\n      case BSONType.bool:\n        return Boolean(this.bson[offset]);\n      case BSONType.objectId:\n        return new ObjectId(this.bson.subarray(offset, offset + 12));\n      case BSONType.timestamp:\n        return new Timestamp(NumberUtils.getBigInt64LE(this.bson, offset));\n      case BSONType.string:\n        return ByteUtils.toUTF8(this.bson, offset + 4, offset + length - 1, false);\n      case BSONType.binData: {\n        const totalBinarySize = NumberUtils.getInt32LE(this.bson, offset);\n        const subType = this.bson[offset + 4];\n\n        if (subType === 2) {\n          const subType2BinarySize = NumberUtils.getInt32LE(this.bson, offset + 1 + 4);\n          if (subType2BinarySize < 0)\n            throw new BSONError('Negative binary type element size found for subtype 0x02');\n          if (subType2BinarySize > totalBinarySize - 4)\n            throw new BSONError('Binary type with subtype 0x02 contains too long binary size');\n          if (subType2BinarySize < totalBinarySize - 4)\n            throw new BSONError('Binary type with subtype 0x02 contains too short binary size');\n          return new Binary(\n            this.bson.subarray(offset + 1 + 4 + 4, offset + 1 + 4 + 4 + subType2BinarySize),\n            2\n          );\n        }\n\n        return new Binary(\n          this.bson.subarray(offset + 1 + 4, offset + 1 + 4 + totalBinarySize),\n          subType\n        );\n      }\n      case BSONType.date:\n        // Pretend this is correct.\n        return new Date(Number(NumberUtils.getBigInt64LE(this.bson, offset)));","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/wire_protocol/on_demand/document.ts#L186-L222","documentation":"Thrown while decoding a BSON binary element of subtype 0x02 (old binary) when the embedded subType2BinarySize (a 4-byte little-endian int32 read after the subtype byte) is negative. A negative length is impossible for valid BSON and indicates corruption or malformed data. Surfaced as a BSONError from the on-demand BSON decoder used to parse server responses lazily.","triggerScenarios":"OnDemandDocument.toJSValue() reads a binData element whose subtype byte is 2, then reads the inner binary size as int32; if that value is < 0 the error throws. Triggered when parsing any server response (cursor documents, command results) containing a subtype-0x02 binary field with a corrupt length prefix. Common when the document was hand-constructed incorrectly or corrupted on the wire.","commonSituations":"Storing/receiving binary data with subtype 0x02 from a source that wrote the length incorrectly. Wire corruption. Interop with another BSON producer that emits malformed old-binary elements. Parsing a truncated buffer.","solutions":["Validate the source of the binary data; if you wrote it, ensure subtype 0x02 payloads include the correct 4-byte inner length prefix.","Check for network/proxy corruption by comparing server-side and client-side bytes.","If reading from a non-MongoDB BSON producer, ensure it emits spec-compliant subtype-0x02 binary.","Report a BSON issue in js-bson if the data is known-good and the error still occurs on the current version."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"import { BSONError } from 'bson';\ntry {\n  await collection.findOne({ _id });\n} catch (err) {\n  if (err instanceof BSONError && /Negative binary type element size/.test(err.message)) {\n    // data corruption; re-read or quarantine the document\n  }\n  throw err;\n}","preventionTips":["Validate binary payloads at write time using the official bson Binary type.","Audit non-MongoDB BSON producers for subtype-0x02 length correctness.","Check for network/proxy corruption on persistent decode errors."],"tags":["bson","decoding","binary","corruption"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}