Skip to content

Commit 4576505

Browse files
targosnodejs-github-bot
authored andcommitted
src: limit Buffer::kMaxLength to 1TB
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
1 parent da7e052 commit 4576505

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/node_buffer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ namespace node {
2929

3030
namespace Buffer {
3131

32-
static const size_t kMaxLength = v8::TypedArray::kMaxLength;
32+
static constexpr size_t kMaxLength =
33+
v8::TypedArray::kMaxLength < 0x10000000000ull ? v8::Uint8Array::kMaxLength
34+
: 0x10000000000ull;
3335

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

src/node_errors.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "debug_utils-inl.h"
77
#include "env.h"
8+
#include "node_buffer.h"
89
#include "v8.h"
910

1011
// Use ostringstream to print exact-width integer types
@@ -210,9 +211,10 @@ inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,
210211

211212
inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) {
212213
char message[128];
213-
snprintf(message, sizeof(message),
214-
"Cannot create a Buffer larger than 0x%zx bytes",
215-
v8::TypedArray::kMaxLength);
214+
snprintf(message,
215+
sizeof(message),
216+
"Cannot create a Buffer larger than 0x%zx bytes",
217+
Buffer::kMaxLength);
216218
return ERR_BUFFER_TOO_LARGE(isolate, message);
217219
}
218220

0 commit comments

Comments
 (0)