Skip to content

Commit 547576f

Browse files
committed
src: always use diagnostic file sequence number
This commit attaches a sequence number to all filenames that are automatically generated by DiagnosticFilename. This prevents accidental overwriting of existing files. PR-URL: #27142 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 44a3acb commit 547576f

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

src/node_internals.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -334,24 +334,21 @@ class DiagnosticFilename {
334334

335335
DiagnosticFilename(Environment* env,
336336
const char* prefix,
337-
const char* ext,
338-
int seq = -1) :
339-
filename_(MakeFilename(env->thread_id(), prefix, ext, seq)) {}
337+
const char* ext) :
338+
filename_(MakeFilename(env->thread_id(), prefix, ext)) {}
340339

341340
DiagnosticFilename(uint64_t thread_id,
342341
const char* prefix,
343-
const char* ext,
344-
int seq = -1) :
345-
filename_(MakeFilename(thread_id, prefix, ext, seq)) {}
342+
const char* ext) :
343+
filename_(MakeFilename(thread_id, prefix, ext)) {}
346344

347345
const char* operator*() const { return filename_.c_str(); }
348346

349347
private:
350348
static std::string MakeFilename(
351349
uint64_t thread_id,
352350
const char* prefix,
353-
const char* ext,
354-
int seq = -1);
351+
const char* ext);
355352

356353
std::string filename_;
357354
};

src/node_report.cc

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <cstring>
1616
#include <ctime>
1717
#include <cwctype>
18-
#include <atomic>
1918
#include <fstream>
2019
#include <iomanip>
2120
#include <climits> // PATH_MAX
@@ -73,9 +72,6 @@ static void PrintLoadedLibraries(JSONWriter* writer);
7372
static void PrintComponentVersions(JSONWriter* writer);
7473
static void PrintRelease(JSONWriter* writer);
7574

76-
// Global variables
77-
static std::atomic_int seq = {0}; // sequence number for report filenames
78-
7975
// External function to trigger a report, writing to file.
8076
// The 'name' parameter is in/out: an input filename is used
8177
// if supplied, and the actual filename is returned.
@@ -99,7 +95,7 @@ std::string TriggerNodeReport(Isolate* isolate,
9995
filename = options->report_filename;
10096
} else {
10197
filename = *DiagnosticFilename(env != nullptr ? env->thread_id() : 0,
102-
"report", "json", seq++);
98+
"report", "json");
10399
}
104100

105101
// Open the report file stream for writing. Supports stdout/err,

src/util.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@
4141
#include <sys/types.h>
4242
#endif
4343

44+
#include <atomic>
4445
#include <cstdio>
4546
#include <iomanip>
4647
#include <sstream>
4748

49+
static std::atomic_int seq = {0}; // Sequence number for diagnostic filenames.
50+
4851
namespace node {
4952

5053
// Microseconds in a second, as a float.
@@ -225,8 +228,7 @@ void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
225228
std::string DiagnosticFilename::MakeFilename(
226229
uint64_t thread_id,
227230
const char* prefix,
228-
const char* ext,
229-
int seq) {
231+
const char* ext) {
230232
std::ostringstream oss;
231233
TIME_TYPE tm_struct;
232234
LocalTime(&tm_struct);
@@ -262,8 +264,7 @@ std::string DiagnosticFilename::MakeFilename(
262264
#endif
263265
oss << "." << uv_os_getpid();
264266
oss << "." << thread_id;
265-
if (seq >= 0)
266-
oss << "." << std::setfill('0') << std::setw(3) << ++seq;
267+
oss << "." << std::setfill('0') << std::setw(3) << ++seq;
267268
oss << "." << ext;
268269
return oss.str();
269270
}

0 commit comments

Comments
 (0)