Skip to content

Commit 0a75177

Browse files
committed
test(catalog): use mkstemp for secure unique temp file creation
Replace static counter approach with mkstemp for atomic, secure temp file creation that avoids parallel run collisions.
1 parent 7635767 commit 0a75177

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

tests/catalog_coverage_tests.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
#include <cstdio>
99
#include <cstring>
10+
#include <fcntl.h>
1011
#include <filesystem>
12+
#include <unistd.h>
1113
#include <vector>
1214

1315
#include "catalog/catalog.hpp"
@@ -239,10 +241,14 @@ TEST(CatalogCoverageTests, SaveAndLoad) {
239241
std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}};
240242
catalog->create_table("persisted_table", cols);
241243

242-
// Use unique temp path to avoid collisions in parallel runs
243-
static int test_counter = 0;
244-
std::string temp_filename = "/tmp/test_catalog_" + std::to_string(++test_counter) + ".bin";
245-
std::filesystem::path temp_path(temp_filename);
244+
// Use mkstemp to create a unique temp file atomically
245+
std::string temp_template = "/tmp/test_catalog_XXXXXX.bin";
246+
std::vector<char> temp_path_vec(temp_template.begin(), temp_template.end());
247+
temp_path_vec.push_back('\0'); // null-terminate for mkstemp
248+
int fd = mkstemp(temp_path_vec.data());
249+
ASSERT_NE(fd, -1);
250+
std::filesystem::path temp_path(temp_path_vec.data());
251+
close(fd); // Close the file descriptor, we only need the path
246252

247253
// Save catalog - should succeed
248254
ASSERT_TRUE(catalog->save(temp_path.string()));

0 commit comments

Comments
 (0)