Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/amd_detail/state_mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ static atomic<bool> run_flag{true};
static int
make_temp_file()
{
// Security analyzers will be sad if you don't set umask 0077
// before creating temporary files
mode_t old_umask = umask(S_IRWXG | S_IRWXO);

Comment on lines +52 to +55
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file uses mode_t, umask(), and S_IRWXG/S_IRWXO but does not include <sys/stat.h>, which is where these are defined on POSIX systems. Please add #include <sys/stat.h> (or the appropriate header) to avoid build failures on stricter toolchains.

Copilot uses AI. Check for mistakes.
// Create a unique temporary file
char pathname[]{"state_mt_tmp.XXXXXX"};
int fd{mkstemp(pathname)};
Expand All @@ -57,6 +61,8 @@ make_temp_file()
exit(EXIT_FAILURE);
}

umask(old_umask);

// Ensure the file is deleted when fd is closed
if (-1 == unlink(pathname)) {
cerr << "unlink() failed! " << strerror(errno) << endl;
Expand Down
7 changes: 7 additions & 0 deletions test/common/test-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <iostream>
#include <random>
#include <stdexcept>
#include <sys/stat.h>
#include <unistd.h>

constexpr hipFileError_t
Expand Down Expand Up @@ -62,11 +63,17 @@ struct Tmpfile {

Tmpfile(std::string directory) : path{directory}
{
// Security analyzers will be sad if you don't set umask 0077
// before creating temporary files
mode_t old_umask = umask(S_IRWXG | S_IRWXO);

path += "/hipFile.XXXXXX";
if ((fd = mkstemp(path.data())) == -1) {
throw std::runtime_error("Could not create temporary file");
}

umask(old_umask);

Comment on lines +68 to +76
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umask() is set before mkstemp(), but if mkstemp() fails you throw before restoring the previous umask. That leaves the process umask changed for the rest of the test run. Please ensure the old umask is restored on all paths (e.g., restore before throwing, or use an RAII guard/scope-exit).

Suggested change
mode_t old_umask = umask(S_IRWXG | S_IRWXO);
path += "/hipFile.XXXXXX";
if ((fd = mkstemp(path.data())) == -1) {
throw std::runtime_error("Could not create temporary file");
}
umask(old_umask);
struct UmaskGuard {
mode_t old_umask;
explicit UmaskGuard(mode_t new_mask) : old_umask(umask(new_mask)) {}
~UmaskGuard() { umask(old_umask); }
} umask_guard(S_IRWXG | S_IRWXO);
path += "/hipFile.XXXXXX";
if ((fd = mkstemp(path.data())) == -1) {
throw std::runtime_error("Could not create temporary file");
}

Copilot uses AI. Check for mistakes.
#ifdef __HIP_PLATFORM_AMD__
if (unlink(path.c_str()) == -1) {
throw std::runtime_error("Could not unlink temporary file");
Expand Down
Loading