{"record":{"id":"31e7bb5461623773","repo":"mongodb/node-mongodb-native","slug":"argument-size-must-be-a-non-negative-number","errorCode":null,"errorMessage":"Argument \"size\" must be a non-negative number","messagePattern":"Argument \"size\" must be a non-negative number","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/utils.ts","lineNumber":840,"sourceCode":"      return NumberUtils.getInt32LE(firstBuffer, 0);\n    }\n\n    // Unlikely case: an int32 is split across buffers.\n    // Use read and put the returned buffer back on top\n    const top4Bytes = this.read(4);\n    const value = NumberUtils.getInt32LE(top4Bytes, 0);\n\n    // Put it back.\n    this.totalByteLength += 4;\n    this.buffers.unshift(top4Bytes);\n\n    return value;\n  }\n\n  /** Reads the requested number of bytes, optionally consuming them */\n  read(size: number): Uint8Array {\n    if (typeof size !== 'number' || size < 0) {\n      throw new MongoInvalidArgumentError('Argument \"size\" must be a non-negative number');\n    }\n\n    // oversized request returns empty buffer\n    if (size > this.totalByteLength) {\n      return ByteUtils.allocate(0);\n    }\n\n    // We know we have enough, we just don't know how it is spread across chunks\n    // TODO(NODE-4732): alloc API should change based on raw option\n    const result = ByteUtils.allocateUnsafe(size);\n\n    for (let bytesRead = 0; bytesRead < size; ) {\n      const buffer = this.buffers.shift();\n      if (buffer == null) {\n        break;\n      }\n      const bytesRemaining = size - bytesRead;\n      const bytesReadable = Math.min(bytesRemaining, buffer.byteLength);","sourceCodeStart":822,"sourceCodeEnd":858,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/utils.ts#L822-L858","documentation":"Thrown by the internal message-buffer read() method when the size argument is not a number or is negative. This Buffer/MessageStream utility reads a fixed number of bytes from a queue; a non-numeric or negative size is invalid. Raised as MongoInvalidArgumentError. This is an internal API not invoked directly by users.","triggerScenarios":"Internal driver wire-protocol parsing code calling read() with an undefined or negative size, typically due to a malformed message length header from the server or a corrupted buffer state. Not reachable through normal user API calls.","commonSituations":"Extremely rare. May surface if a custom wire-protocol interceptor or proxy sends malformed responses, or from driver bugs in buffer handling. Indicates corruption in the incoming byte stream or an internal logic error.","solutions":["Upgrade the driver to the latest stable release; internal buffer bugs are fixed upstream.","If behind a proxy or mongos/router, ensure it emits valid MongoDB wire-protocol responses.","Capture the full stack trace and report to the NODE driver Jira project."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await client.db('test').command({ ping: 1 });\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /size.*non-negative/.test(e.message)) {\n    // internal buffer error: upgrade driver, check proxy/server compatibility\n  } else throw e;\n}","preventionTips":["Keep the driver updated to avoid internal buffer-handling bugs.","Ensure any proxy/mongos in the path emits valid MongoDB wire-protocol messages.","Report persistent occurrences with a full stack trace to the driver project."],"tags":["internal","buffer","wire-protocol","validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}