@@ -59,6 +59,7 @@ using v8::ArrayBuffer;
5959using v8::ArrayBufferView;
6060using v8::BackingStore;
6161using v8::BackingStoreInitializationMode;
62+ using v8::BackingStoreOnFailureMode;
6263using v8::CFunction;
6364using v8::Context;
6465using v8::EscapableHandleScope;
@@ -304,17 +305,20 @@ MaybeLocal<Object> New(Isolate* isolate,
304305 EscapableHandleScope scope (isolate);
305306
306307 size_t length;
307- if (!StringBytes::Size (isolate, string, enc).To (&length))
308- return Local<Object>();
308+ if (!StringBytes::Size (isolate, string, enc).To (&length)) return {};
309309 size_t actual = 0 ;
310310 std::unique_ptr<BackingStore> store;
311311
312312 if (length > 0 ) {
313- store = ArrayBuffer::NewBackingStore (isolate, length);
313+ store = ArrayBuffer::NewBackingStore (
314+ isolate,
315+ length,
316+ BackingStoreInitializationMode::kZeroInitialized ,
317+ BackingStoreOnFailureMode::kReturnNull );
314318
315319 if (!store) [[unlikely]] {
316320 THROW_ERR_MEMORY_ALLOCATION_FAILED (isolate);
317- return Local<Object>() ;
321+ return {} ;
318322 }
319323
320324 actual = StringBytes::Write (
@@ -329,7 +333,14 @@ MaybeLocal<Object> New(Isolate* isolate,
329333 if (actual < length) {
330334 std::unique_ptr<BackingStore> old_store = std::move (store);
331335 store = ArrayBuffer::NewBackingStore (
332- isolate, actual, BackingStoreInitializationMode::kUninitialized );
336+ isolate,
337+ actual,
338+ BackingStoreInitializationMode::kUninitialized ,
339+ BackingStoreOnFailureMode::kReturnNull );
340+ if (!store) [[unlikely]] {
341+ THROW_ERR_MEMORY_ALLOCATION_FAILED (isolate);
342+ return {};
343+ }
333344 memcpy (store->Data (), old_store->Data (), actual);
334345 }
335346 Local<ArrayBuffer> buf = ArrayBuffer::New (isolate, std::move (store));
@@ -372,7 +383,14 @@ MaybeLocal<Object> New(Environment* env, size_t length) {
372383 Local<ArrayBuffer> ab;
373384 {
374385 std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore (
375- isolate, length, BackingStoreInitializationMode::kUninitialized );
386+ isolate,
387+ length,
388+ BackingStoreInitializationMode::kUninitialized ,
389+ BackingStoreOnFailureMode::kReturnNull );
390+ if (!bs) [[unlikely]] {
391+ THROW_ERR_MEMORY_ALLOCATION_FAILED (isolate);
392+ return {};
393+ }
376394
377395 CHECK (bs);
378396
@@ -412,9 +430,14 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {
412430 }
413431
414432 std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore (
415- isolate, length, BackingStoreInitializationMode::kUninitialized );
416-
417- CHECK (bs);
433+ isolate,
434+ length,
435+ BackingStoreInitializationMode::kUninitialized ,
436+ BackingStoreOnFailureMode::kReturnNull );
437+ if (!bs) [[unlikely]] {
438+ THROW_ERR_MEMORY_ALLOCATION_FAILED (isolate);
439+ return {};
440+ }
418441
419442 if (length > 0 ) memcpy (bs->Data (), data, length);
420443
@@ -1449,8 +1472,8 @@ void CreateUnsafeArrayBuffer(const FunctionCallbackInfo<Value>& args) {
14491472 BackingStoreInitializationMode::kUninitialized ,
14501473 v8::BackingStoreOnFailureMode::kReturnNull );
14511474
1452- if (!store) {
1453- env-> ThrowRangeError ( " Array buffer allocation failed " );
1475+ if (!store) [[unlikely]] {
1476+ THROW_ERR_MEMORY_ALLOCATION_FAILED (env );
14541477 return ;
14551478 }
14561479
0 commit comments