Skip to content

convert sprintf calls to snprintf #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion cli/fossilize_disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ static void print_help()
static string uint64_string(uint64_t value)
{
char str[17]; // 16 digits + null
sprintf(str, "%016" PRIx64, value);
snprintf(str, sizeof(str), "%016" PRIx64, value);
return string(str);
}

Expand Down
2 changes: 1 addition & 1 deletion cli/fossilize_replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ struct ThreadedReplayer : StateCreatorInterface
auto &props = device->get_module_identifier_properties();
char uuid_string[2 * VK_UUID_SIZE + 1];
for (unsigned i = 0; i < VK_UUID_SIZE; i++)
sprintf(uuid_string + 2 * i, "%02x", props.shaderModuleIdentifierAlgorithmUUID[i]);
snprintf(uuid_string + 2 * i, 2 * VK_UUID_SIZE + 1 - 2 * i, "%02x", props.shaderModuleIdentifierAlgorithmUUID[i]);

report_module_uuid(uuid_string);

Expand Down
34 changes: 17 additions & 17 deletions cli/fossilize_replay_linux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void ProcessProgress::parse(const char *cmd)
if (Global::control_block && graphics_progress > 0 && graphics_pipeline != 0)
{
char buffer[ControlBlockMessageSize];
sprintf(buffer, "GRAPHICS %d %" PRIx64 "\n", graphics_progress - 1, graphics_pipeline);
snprintf(buffer, ControlBlockMessageSize, "GRAPHICS %d %" PRIx64 "\n", graphics_progress - 1, graphics_pipeline);
futex_wrapper_lock(&Global::control_block->futex_lock);
shared_control_block_write(Global::control_block, buffer, sizeof(buffer));
futex_wrapper_unlock(&Global::control_block->futex_lock);
Expand All @@ -220,7 +220,7 @@ void ProcessProgress::parse(const char *cmd)
if (Global::control_block && raytracing_progress > 0 && raytracing_pipeline != 0)
{
char buffer[ControlBlockMessageSize];
sprintf(buffer, "RAYTRACE %d %" PRIx64 "\n", raytracing_progress - 1, raytracing_pipeline);
snprintf(buffer, ControlBlockMessageSize, "RAYTRACE %d %" PRIx64 "\n", raytracing_progress - 1, raytracing_pipeline);
futex_wrapper_lock(&Global::control_block->futex_lock);
shared_control_block_write(Global::control_block, buffer, sizeof(buffer));
futex_wrapper_unlock(&Global::control_block->futex_lock);
Expand All @@ -238,7 +238,7 @@ void ProcessProgress::parse(const char *cmd)
if (Global::control_block && compute_progress > 0 && compute_pipeline)
{
char buffer[ControlBlockMessageSize];
sprintf(buffer, "COMPUTE %d %" PRIx64 "\n", compute_progress - 1, compute_pipeline);
snprintf(buffer, ControlBlockMessageSize, "COMPUTE %d %" PRIx64 "\n", compute_progress - 1, compute_pipeline);
futex_wrapper_lock(&Global::control_block->futex_lock);
shared_control_block_write(Global::control_block, buffer, sizeof(buffer));
futex_wrapper_unlock(&Global::control_block->futex_lock);
Expand Down Expand Up @@ -417,7 +417,7 @@ static void send_faulty_modules_and_close(int fd)
for (auto &m : Global::faulty_spirv_modules)
{
char buffer[18];
sprintf(buffer, "%" PRIx64 "\n", m);
snprintf(buffer, sizeof(buffer), "%" PRIx64 "\n", m);
write_all(fd, buffer);
}

Expand Down Expand Up @@ -1426,19 +1426,19 @@ static void validation_error_cb(ThreadedReplayer *replayer)

if (per_thread.current_graphics_pipeline)
{
sprintf(buffer, "GRAPHICS_VERR %" PRIx64 "\n", per_thread.current_graphics_pipeline);
snprintf(buffer, sizeof(buffer), "GRAPHICS_VERR %" PRIx64 "\n", per_thread.current_graphics_pipeline);
write_all(crash_fd, buffer);
}

if (per_thread.current_compute_pipeline)
{
sprintf(buffer, "COMPUTE_VERR %" PRIx64 "\n", per_thread.current_compute_pipeline);
snprintf(buffer, sizeof(buffer), "COMPUTE_VERR %" PRIx64 "\n", per_thread.current_compute_pipeline);
write_all(crash_fd, buffer);
}

if (per_thread.current_raytracing_pipeline)
{
sprintf(buffer, "RAYTRACE_VERR %" PRIx64 "\n", per_thread.current_raytracing_pipeline);
snprintf(buffer, sizeof(buffer), "RAYTRACE_VERR %" PRIx64 "\n", per_thread.current_raytracing_pipeline);
write_all(crash_fd, buffer);
}
}
Expand All @@ -1448,7 +1448,7 @@ static void report_module_uuid(const char (&path)[2 * VK_UUID_SIZE + 1])
if (crash_fd >= 0)
{
char buffer[64];
sprintf(buffer, "MODULE_UUID %s\n", path);
snprintf(buffer, sizeof(buffer), "MODULE_UUID %s\n", path);
if (!write_all(crash_fd, buffer))
_exit(2);
}
Expand All @@ -1462,7 +1462,7 @@ static void crash_handler(ThreadedReplayer &replayer, ThreadedReplayer::PerThrea
// This allows a new process to ignore these modules.
for (unsigned i = 0; i < per_thread.num_failed_module_hashes; i++)
{
sprintf(buffer, "MODULE %" PRIx64 "\n", per_thread.failed_module_hashes[i]);
snprintf(buffer, sizeof(buffer), "MODULE %" PRIx64 "\n", per_thread.failed_module_hashes[i]);
if (!write_all(crash_fd, buffer))
_exit(2);
}
Expand All @@ -1475,18 +1475,18 @@ static void crash_handler(ThreadedReplayer &replayer, ThreadedReplayer::PerThrea
per_thread.current_raytracing_pipeline)
{
// Report where we stopped, so we can continue.
sprintf(buffer, "GRAPHICS %d %" PRIx64 "\n", per_thread.current_graphics_index,
per_thread.current_graphics_pipeline);
snprintf(buffer, sizeof(buffer), "GRAPHICS %d %" PRIx64 "\n", per_thread.current_graphics_index,
per_thread.current_graphics_pipeline);
if (!write_all(crash_fd, buffer))
_exit(2);

sprintf(buffer, "COMPUTE %d %" PRIx64 "\n", per_thread.current_compute_index,
per_thread.current_compute_pipeline);
snprintf(buffer, sizeof(buffer), "COMPUTE %d %" PRIx64 "\n", per_thread.current_compute_index,
per_thread.current_compute_pipeline);
if (!write_all(crash_fd, buffer))
_exit(2);

sprintf(buffer, "RAYTRACE %d %" PRIx64 "\n", per_thread.current_raytracing_index,
per_thread.current_raytracing_pipeline);
snprintf(buffer, sizeof(buffer), "RAYTRACE %d %" PRIx64 "\n", per_thread.current_raytracing_index,
per_thread.current_raytracing_pipeline);
if (!write_all(crash_fd, buffer))
_exit(2);
}
Expand Down Expand Up @@ -1707,7 +1707,7 @@ static int run_slave_process(const VulkanDevice::Options &opts,
{
futex_wrapper_lock(&Global::control_block->futex_lock);
char msg[ControlBlockMessageSize] = {};
sprintf(msg, "SLAVE_FINISHED\n");
snprintf(msg, ControlBlockMessageSize, "SLAVE_FINISHED\n");
shared_control_block_write(Global::control_block, msg, sizeof(msg));
futex_wrapper_unlock(&Global::control_block->futex_lock);
}
Expand All @@ -1719,7 +1719,7 @@ static int run_slave_process(const VulkanDevice::Options &opts,
static void log_process_memory()
{
char path[1024];
sprintf(path, "/proc/%d/status", getpid());
snprintf(path, sizeof(path), "/proc/%d/status", getpid());
FILE *file = fopen(path, "r");
if (!file)
{
Expand Down
28 changes: 14 additions & 14 deletions cli/fossilize_replay_windows.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void ProcessProgress::parse(const char *cmd)
if (Global::control_block && graphics_progress > 0 && graphics_pipeline != 0)
{
char buffer[ControlBlockMessageSize];
sprintf(buffer, "GRAPHICS %d %" PRIx64 "\n", graphics_progress - 1, graphics_pipeline);
snprintf(buffer, ControlBlockMessageSize, "GRAPHICS %d %" PRIx64 "\n", graphics_progress - 1, graphics_pipeline);

if (WaitForSingleObject(Global::shared_mutex, INFINITE) == WAIT_OBJECT_0)
{
Expand All @@ -185,7 +185,7 @@ void ProcessProgress::parse(const char *cmd)
if (Global::control_block && raytracing_progress > 0 && raytracing_pipeline != 0)
{
char buffer[ControlBlockMessageSize];
sprintf(buffer, "RAYTRACE %d %" PRIx64 "\n", raytracing_progress - 1, raytracing_pipeline);
snprintf(buffer, ControlBlockMessageSize, "RAYTRACE %d %" PRIx64 "\n", raytracing_progress - 1, raytracing_pipeline);

if (WaitForSingleObject(Global::shared_mutex, INFINITE) == WAIT_OBJECT_0)
{
Expand All @@ -206,7 +206,7 @@ void ProcessProgress::parse(const char *cmd)
if (Global::control_block && compute_progress > 0 && compute_pipeline)
{
char buffer[ControlBlockMessageSize];
sprintf(buffer, "COMPUTE %d %" PRIx64 "\n", compute_progress - 1, compute_pipeline);
snprintf(buffer, ControlBlockMessageSize, "COMPUTE %d %" PRIx64 "\n", compute_progress - 1, compute_pipeline);

if (WaitForSingleObject(Global::shared_mutex, INFINITE) == WAIT_OBJECT_0)
{
Expand Down Expand Up @@ -351,7 +351,7 @@ static void send_faulty_modules_and_close(HANDLE file)
for (auto &m : Global::faulty_spirv_modules)
{
char buffer[18];
sprintf(buffer, "%" PRIx64 "\n", m);
snprintf(buffer, sizeof(buffer), "%" PRIx64 "\n", m);
write_all(file, buffer);
}

Expand All @@ -368,7 +368,7 @@ static bool CreateCustomPipe(HANDLE *read_pipe, HANDLE *write_pipe, LPSECURITY_A
// This is so that we can safely read one message at a time with ReadFile rather than rely on fgets to delimit each message for us.
static unsigned pipe_serial;
char pipe_name_buffer[MAX_PATH];
sprintf(pipe_name_buffer, "\\\\.\\Pipe\\Fossilize.%08lx.%08x", GetCurrentProcessId(), pipe_serial++);
snprintf(pipe_name_buffer, sizeof(pipe_name_buffer), "\\\\.\\Pipe\\Fossilize.%08lx.%08x", GetCurrentProcessId(), pipe_serial++);
*read_pipe = CreateNamedPipeA(pipe_name_buffer, PIPE_ACCESS_INBOUND | (overlapped_read ? FILE_FLAG_OVERLAPPED : 0),
PIPE_TYPE_MESSAGE | PIPE_WAIT | PIPE_READMODE_MESSAGE, 1, 4096, 4096, 10000, attrs);

Expand Down Expand Up @@ -1045,19 +1045,19 @@ static void validation_error_cb(ThreadedReplayer *replayer)

if (per_thread.current_graphics_pipeline)
{
sprintf(buffer, "GRAPHICS_VERR %" PRIx64 "\n", per_thread.current_graphics_pipeline);
snprintf(buffer, sizeof(buffer), "GRAPHICS_VERR %" PRIx64 "\n", per_thread.current_graphics_pipeline);
write_all(crash_handle, buffer);
}

if (per_thread.current_compute_pipeline)
{
sprintf(buffer, "COMPUTE_VERR %" PRIx64 "\n", per_thread.current_compute_pipeline);
snprintf(buffer, sizeof(buffer), "COMPUTE_VERR %" PRIx64 "\n", per_thread.current_compute_pipeline);
write_all(crash_handle, buffer);
}

if (per_thread.current_raytracing_pipeline)
{
sprintf(buffer, "RAYTRACE_VERR %" PRIx64 "\n", per_thread.current_raytracing_pipeline);
snprintf(buffer, sizeof(buffer), "RAYTRACE_VERR %" PRIx64 "\n", per_thread.current_raytracing_pipeline);
write_all(crash_handle, buffer);
}
}
Expand All @@ -1067,7 +1067,7 @@ static void report_module_uuid(const char (&path)[2 * VK_UUID_SIZE + 1])
if (crash_handle)
{
char buffer[64];
sprintf(buffer, "MODULE_UUID %s\n", path);
snprintf(buffer, sizeof(buffer), "MODULE_UUID %s\n", path);
if (!write_all(crash_handle, buffer))
ExitProcess(2);
}
Expand All @@ -1081,7 +1081,7 @@ static void crash_handler(ThreadedReplayer &replayer, ThreadedReplayer::PerThrea
// This allows a new process to ignore these modules.
for (unsigned i = 0; i < per_thread.num_failed_module_hashes; i++)
{
sprintf(buffer, "MODULE %" PRIx64 "\n", per_thread.failed_module_hashes[i]);
snprintf(buffer, sizeof(buffer), "MODULE %" PRIx64 "\n", per_thread.failed_module_hashes[i]);
if (!write_all(crash_handle, buffer))
ExitProcess(2);
}
Expand All @@ -1094,17 +1094,17 @@ static void crash_handler(ThreadedReplayer &replayer, ThreadedReplayer::PerThrea
per_thread.current_raytracing_pipeline)
{
// Report where we stopped, so we can continue.
sprintf(buffer, "GRAPHICS %d %" PRIx64 "\n", per_thread.current_graphics_index,
snprintf(buffer, sizeof(buffer), "GRAPHICS %d %" PRIx64 "\n", per_thread.current_graphics_index,
per_thread.current_graphics_pipeline);
if (!write_all(crash_handle, buffer))
ExitProcess(2);

sprintf(buffer, "COMPUTE %d %" PRIx64 "\n", per_thread.current_compute_index,
snprintf(buffer, sizeof(buffer), "COMPUTE %d %" PRIx64 "\n", per_thread.current_compute_index,
per_thread.current_compute_pipeline);
if (!write_all(crash_handle, buffer))
ExitProcess(2);

sprintf(buffer, "RAYTRACE %d %" PRIx64 "\n", per_thread.current_raytracing_index,
snprintf(buffer, sizeof(buffer), "RAYTRACE %d %" PRIx64 "\n", per_thread.current_raytracing_index,
per_thread.current_raytracing_pipeline);
if (!write_all(crash_handle, buffer))
ExitProcess(2);
Expand Down Expand Up @@ -1325,7 +1325,7 @@ static int run_slave_process(const VulkanDevice::Options &opts,
if (WaitForSingleObject(Global::shared_mutex, INFINITE) == WAIT_OBJECT_0)
{
char msg[ControlBlockMessageSize] = {};
sprintf(msg, "SLAVE_FINISHED\n");
snprintf(msg, ControlBlockMessageSize, "SLAVE_FINISHED\n");
shared_control_block_write(Global::control_block, msg, sizeof(msg));
ReleaseMutex(Global::shared_mutex);
}
Expand Down
2 changes: 1 addition & 1 deletion fossilize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ template <typename Allocator>
static Value uint64_string(uint64_t value, Allocator &alloc)
{
char str[17]; // 16 digits + null
sprintf(str, "%016" PRIx64, value);
snprintf(str, sizeof(str), "%016" PRIx64, value);
return Value(str, alloc);
}

Expand Down
12 changes: 6 additions & 6 deletions fossilize_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ struct DumbDirectoryDatabase : DatabaseInterface
return false;

char filename[25]; // 2 digits + "." + 16 digits + ".json" + null
sprintf(filename, "%02x.%016" PRIx64 ".json", static_cast<unsigned>(tag), hash);
snprintf(filename, sizeof(filename), "%02x.%016" PRIx64 ".json", static_cast<unsigned>(tag), hash);
auto path = Path::join(base_directory, filename);

FILE *file = fopen(path.c_str(), "rb");
Expand Down Expand Up @@ -646,7 +646,7 @@ struct DumbDirectoryDatabase : DatabaseInterface
return true;

char filename[25]; // 2 digits + "." + 16 digits + ".json" + null
sprintf(filename, "%02x.%016" PRIx64 ".json", static_cast<unsigned>(tag), hash);
snprintf(filename, sizeof(filename), "%02x.%016" PRIx64 ".json", static_cast<unsigned>(tag), hash);
auto path = Path::join(base_directory, filename);

FILE *file = fopen(path.c_str(), "wb");
Expand Down Expand Up @@ -862,8 +862,8 @@ struct ZipDatabase : DatabaseInterface
return true;

char str[FOSSILIZE_BLOB_HASH_LENGTH + 1]; // 40 digits + null
sprintf(str, "%0*x", FOSSILIZE_BLOB_HASH_LENGTH - 16, tag);
sprintf(str + FOSSILIZE_BLOB_HASH_LENGTH - 16, "%016" PRIx64, hash);
snprintf(str, FOSSILIZE_BLOB_HASH_LENGTH + 1, "%0*x", FOSSILIZE_BLOB_HASH_LENGTH - 16, tag);
snprintf(str + FOSSILIZE_BLOB_HASH_LENGTH - 16, 17, "%016" PRIx64, hash);

unsigned mz_flags;
if ((flags & PAYLOAD_WRITE_COMPRESS_BIT) != 0)
Expand Down Expand Up @@ -1353,8 +1353,8 @@ struct StreamArchive : DatabaseInterface
return true;

char str[FOSSILIZE_BLOB_HASH_LENGTH + 1]; // 40 digits + null
sprintf(str, "%0*x", FOSSILIZE_BLOB_HASH_LENGTH - 16, tag);
sprintf(str + FOSSILIZE_BLOB_HASH_LENGTH - 16, "%016" PRIx64, hash);
snprintf(str, FOSSILIZE_BLOB_HASH_LENGTH + 1, "%0*x", FOSSILIZE_BLOB_HASH_LENGTH - 16, tag);
snprintf(str + FOSSILIZE_BLOB_HASH_LENGTH - 16, 17, "%016" PRIx64, hash);

if (fwrite(str, 1, FOSSILIZE_BLOB_HASH_LENGTH, file) != FOSSILIZE_BLOB_HASH_LENGTH)
return false;
Expand Down
Loading