Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
feat(NODE-3302): replace MAX_BSON_SIZE with "maxBSONSize" property (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
jancurn authored Jun 1, 2021
1 parent 191ee8b commit 0b59b67
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/bson.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ NAN_METHOD(BSON::New) {
Nan::HandleScope scope;

// Var maximum bson size
uint32_t maxBSONSize = MAX_BSON_SIZE;
uint32_t maxBSONSize = 17 * 1024 * 1024;

// Check that we have an array
if (info.Length() >= 1 && info[0]->IsArray()) {
Expand Down
57 changes: 29 additions & 28 deletions src/bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,30 +282,30 @@ class CountStream {
size_t count;
};

const size_t MAX_BSON_SIZE(1024 * 1024 * 17);
void ThrowAllocatedStringException(size_t allocationSize, const char *format, ...);

class DataStream {
public:
DataStream(char *aDestinationBuffer)
: destinationBuffer(aDestinationBuffer), p(aDestinationBuffer), depth(0) {
DataStream(uint32_t maxBSONSizeArg, char *aDestinationBuffer)
: destinationBuffer(aDestinationBuffer), p(aDestinationBuffer), depth(0), maxBSONSize(maxBSONSizeArg) {
}

void WriteByte(int value) {
if ((size_t)((p + 1) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + 1) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
*p++ = value;
}

void WriteByte(const Local<Object> &object, const Local<String> &key) {
if ((size_t)((p + 1) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + 1) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
*p++ = NanTo<int32_t>(NanGet(object, key));
}

#if USE_MISALIGNED_MEMORY_ACCESS
void WriteInt32(int32_t value) {
if ((size_t)((p + 4) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + 4) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
#if defined(_MSC_VER)
*reinterpret_cast<int32_t *>(p) =
is_bigendian() ? _byteswap_ulong(value) : value;
Expand All @@ -317,8 +317,8 @@ class DataStream {
}

void WriteInt64(int64_t value) {
if ((size_t)((p + 8) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + 8) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
#if defined(_MSC_VER)
*reinterpret_cast<int64_t *>(p) =
is_bigendian() ? _byteswap_uint64(value) : value;
Expand All @@ -330,8 +330,8 @@ class DataStream {
}

void WriteDouble(double value) {
if ((size_t)((p + 8) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + 8) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
*reinterpret_cast<double *>(p) = value;
if (is_bigendian()) {
#if defined(_MSC_VER)
Expand All @@ -347,22 +347,22 @@ class DataStream {
}
#else
void WriteInt32(int32_t value) {
if ((size_t)((p + 4) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + 4) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
memcpy(p, &value, 4);
p += 4;
}

void WriteInt64(int64_t value) {
if ((size_t)((p + 8) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + 8) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
memcpy(p, &value, 8);
p += 8;
}

void WriteDouble(double value) {
if ((size_t)((p + 8) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + 8) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
memcpy(p, &value, 8);
p += 8;
}
Expand Down Expand Up @@ -395,23 +395,23 @@ class DataStream {

void WriteLengthPrefixedString(const Local<String> &value) {
int32_t length = NanUtf8Length(value) + 1;
if ((size_t)((p + length) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + length) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
WriteInt32(length);
WriteString(value);
}

void WriteObjectId(const Local<Object> &object, const Local<String> &key);

void WriteString(const Local<String> &value) {
if ((size_t)(p - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)(p - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
p += NanWriteUtf8(value, p);
} // This returns the number of bytes inclusive of the NULL terminator.

void WriteData(const char *data, size_t length) {
if ((size_t)((p + length) - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)((p + length) - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
memcpy(p, data, length);
p += length;
}
Expand All @@ -427,8 +427,8 @@ class DataStream {
}

void *BeginWriteSize() {
if ((size_t)(p - destinationBuffer) > MAX_BSON_SIZE)
throw "document is larger than max bson document size of 16MB";
if ((size_t)(p - destinationBuffer) > maxBSONSize)
ThrowAllocatedStringException(128, "document is larger than max bson document size of %d bytes", maxBSONSize);
void *returnValue = p;
p += 4;
return returnValue;
Expand Down Expand Up @@ -469,6 +469,7 @@ class DataStream {
char *const destinationBuffer; // base, never changes
char *p; // cursor into buffer
uint32_t depth; // keeps track of recursive depth
uint32_t maxBSONSize; // maximum BSON size
};

template <typename T> class BSONSerializer : public T {
Expand All @@ -483,7 +484,7 @@ template <typename T> class BSONSerializer : public T {
ignoreUndefined(ignoreUndefined), bson(aBson) {}
BSONSerializer(BSON *aBson, bool aCheckKeys, bool aSerializeFunctions,
bool ignoreUndefined, char *parentParam)
: Inherited(parentParam), checkKeys(aCheckKeys),
: Inherited(aBson->maxBSONSize, parentParam), checkKeys(aCheckKeys),
serializeFunctions(aSerializeFunctions),
ignoreUndefined(ignoreUndefined), bson(aBson) {}

Expand Down

0 comments on commit 0b59b67

Please sign in to comment.