Skip to content

Commit

Permalink
src: limit Buffer::kMaxLength to 1TB
Browse files Browse the repository at this point in the history
This change has no real effect for now, as the V8 maximum typed array
length is still 2**32. When V8 is updated to version 11.9 or later, the
limit will be 2**53-1 on 64-bit architectures, much larger than any
reasonable amount of RAM. This caps the limit at 1TB, which is already
very large and corresponds to the maximum memory that AddressSanitizer
allows to allocate.

Refs: nodejs/node#49876
Refs: #268
  • Loading branch information
targos authored and nodejs-github-bot committed Oct 5, 2023
1 parent f8c0d15 commit 78989d0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/node_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ namespace node {

namespace Buffer {

static const size_t kMaxLength = v8::TypedArray::kMaxLength;
static constexpr size_t kMaxLength =
v8::TypedArray::kMaxLength < 0x10000000000ull ? v8::Uint8Array::kMaxLength
: 0x10000000000ull;

typedef void (*FreeCallback)(char* data, void* hint);

Expand Down
8 changes: 5 additions & 3 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "debug_utils-inl.h"
#include "env.h"
#include "node_buffer.h"
#include "v8.h"

// Use ostringstream to print exact-width integer types
Expand Down Expand Up @@ -216,9 +217,10 @@ inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,

inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) {
char message[128];
snprintf(message, sizeof(message),
"Cannot create a Buffer larger than 0x%zx bytes",
v8::TypedArray::kMaxLength);
snprintf(message,
sizeof(message),
"Cannot create a Buffer larger than 0x%zx bytes",
Buffer::kMaxLength);
return ERR_BUFFER_TOO_LARGE(isolate, message);
}

Expand Down

0 comments on commit 78989d0

Please sign in to comment.