Skip to content

Commit 1a3db37

Browse files
committed
v8: make writeHeapSnapshot throw if fopen fails
Fixes: #41346
1 parent a4795ad commit 1a3db37

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

src/env.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ size_t Environment::NearHeapLimitCallback(void* data,
16431643
env->isolate()->RemoveNearHeapLimitCallback(NearHeapLimitCallback,
16441644
initial_heap_limit);
16451645

1646-
heap::WriteSnapshot(env->isolate(), filename.c_str());
1646+
heap::WriteSnapshot(env, filename.c_str());
16471647
env->heap_limit_snapshot_taken_ += 1;
16481648

16491649
// Don't take more snapshots than the number specified by

src/heap_utils.cc

+9-7
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,22 @@ class HeapSnapshotStream : public AsyncWrap,
308308
HeapSnapshotPointer snapshot_;
309309
};
310310

311-
inline void TakeSnapshot(Isolate* isolate, v8::OutputStream* out) {
311+
inline void TakeSnapshot(Environment* env, v8::OutputStream* out) {
312312
HeapSnapshotPointer snapshot {
313-
isolate->GetHeapProfiler()->TakeHeapSnapshot() };
313+
env->isolate()->GetHeapProfiler()->TakeHeapSnapshot() };
314314
snapshot->Serialize(out, HeapSnapshot::kJSON);
315315
}
316316

317317
} // namespace
318318

319-
bool WriteSnapshot(Isolate* isolate, const char* filename) {
319+
bool WriteSnapshot(Environment* env, const char* filename) {
320320
FILE* fp = fopen(filename, "w");
321-
if (fp == nullptr)
321+
if (fp == nullptr) {
322+
env->ThrowErrnoException(errno, "fopen");
322323
return false;
324+
}
323325
FileOutputStream stream(fp);
324-
TakeSnapshot(isolate, &stream);
326+
TakeSnapshot(env, &stream);
325327
fclose(fp);
326328
return true;
327329
}
@@ -374,7 +376,7 @@ void TriggerHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
374376

375377
if (filename_v->IsUndefined()) {
376378
DiagnosticFilename name(env, "Heap", "heapsnapshot");
377-
if (!WriteSnapshot(isolate, *name))
379+
if (!WriteSnapshot(env, *name))
378380
return;
379381
if (String::NewFromUtf8(isolate, *name).ToLocal(&filename_v)) {
380382
args.GetReturnValue().Set(filename_v);
@@ -384,7 +386,7 @@ void TriggerHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
384386

385387
BufferValue path(isolate, filename_v);
386388
CHECK_NOT_NULL(*path);
387-
if (!WriteSnapshot(isolate, *path))
389+
if (!WriteSnapshot(env, *path))
388390
return;
389391
return args.GetReturnValue().Set(filename_v);
390392
}

src/node_internals.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class DiagnosticFilename {
379379
};
380380

381381
namespace heap {
382-
bool WriteSnapshot(v8::Isolate* isolate, const char* filename);
382+
bool WriteSnapshot(Environment* env, const char* filename);
383383
}
384384

385385
class TraceEventScope {

test/sequential/test-heapdump.js

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (!common.isMainThread)
77

88
const { writeHeapSnapshot, getHeapSnapshot } = require('v8');
99
const assert = require('assert');
10+
const path = require('path');
1011
const fs = require('fs');
1112
const tmpdir = require('../common/tmpdir');
1213

@@ -24,6 +25,18 @@ process.chdir(tmpdir.path);
2425
fs.accessSync(heapdump);
2526
}
2627

28+
{
29+
const readonlyFile = path.join(tmpdir.path, 'ro');
30+
fs.writeFileSync(readonlyFile, Buffer.alloc(0), { mode: 0o444 });
31+
assert.throws(() => {
32+
writeHeapSnapshot(readonlyFile);
33+
}, (e) => {
34+
assert.ok(e, 'writeHeapSnapshot should error');
35+
assert.strictEqual(e.code, 'EACCES');
36+
return true;
37+
});
38+
}
39+
2740
[1, true, {}, [], null, Infinity, NaN].forEach((i) => {
2841
assert.throws(() => writeHeapSnapshot(i), {
2942
code: 'ERR_INVALID_ARG_TYPE',

0 commit comments

Comments
 (0)