GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: neuter external `nullptr` buffers · nodejs/node@5667369

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_buffer.cc
  • test/addons/null-buffer-neuter
    • binding.cc
    • binding.gyp
    • test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_buffer.cc‎

Copy file name to clipboardExpand all lines: src/node_buffer.cc
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ MaybeLocal<Object> New(Environment* env,
362362
}
363363

364364
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), data, length);
365+
// `Neuter()`ing is required here to prevent materialization of the backing
366+
// store in v8. `nullptr` buffers are not writable, so this is semantically
367+
// correct.
368+
if (data == nullptr)
369+
ab->Neuter();
365370
Local<Uint8Array> ui = Uint8Array::New(ab, 0, length);
366371
Maybe<bool> mb =
367372
ui->SetPrototype(env->context(), env->buffer_prototype_object());
Collapse file

‎test/addons/null-buffer-neuter/binding.cc‎

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <node.h>
2+
#include <node_buffer.h>
3+
#include <util.h>
4+
#include <v8.h>
5+
6+
static int alive;
7+
8+
static void FreeCallback(char* data, void* hint) {
9+
CHECK_EQ(data, nullptr);
10+
alive--;
11+
}
12+
13+
void Run(const v8::FunctionCallbackInfo<v8::Value>& args) {
14+
v8::Isolate* isolate = args.GetIsolate();
15+
alive++;
16+
17+
{
18+
v8::HandleScope scope(isolate);
19+
v8::Local<v8::Object> buf = node::Buffer::New(
20+
isolate,
21+
nullptr,
22+
0,
23+
FreeCallback,
24+
nullptr).ToLocalChecked();
25+
26+
char* data = node::Buffer::Data(buf);
27+
CHECK_EQ(data, nullptr);
28+
}
29+
30+
isolate->RequestGarbageCollectionForTesting(
31+
v8::Isolate::kFullGarbageCollection);
32+
33+
CHECK_EQ(alive, 0);
34+
}
35+
36+
void init(v8::Local<v8::Object> target) {
37+
NODE_SET_METHOD(target, "run", Run);
38+
}
39+
40+
NODE_MODULE(binding, init);
Collapse file

‎test/addons/null-buffer-neuter/binding.gyp‎

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': [ 'binding.cc' ]
6+
}
7+
]
8+
}
Collapse file

‎test/addons/null-buffer-neuter/test.js‎

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
// Flags: --expose-gc
3+
4+
require('../../common');
5+
var binding = require('./build/Release/binding');
6+
7+
binding.run();

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.