From 44ed1c60ad8f838bfb6cfff26683bf706285552a Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Thu, 23 Apr 2026 01:26:03 -0400 Subject: Fix snprintfs --- src/lib/core/artifactory.c | 7 ++----- src/lib/core/conda.c | 41 +++++++++++++++-------------------------- src/lib/core/multiprocessing.c | 5 ++--- src/lib/core/utils.c | 15 ++++----------- 4 files changed, 23 insertions(+), 45 deletions(-) (limited to 'src/lib/core') diff --git a/src/lib/core/artifactory.c b/src/lib/core/artifactory.c index f70c0b8..2490346 100644 --- a/src/lib/core/artifactory.c +++ b/src/lib/core/artifactory.c @@ -76,15 +76,12 @@ int artifactory_download_cli(char *dest, } SYSDEBUG("%s", "Construct path to write data"); - const char *remote_filename_fmt = "/%s"; - int remote_filename_fmt_len = snprintf(NULL, 0, remote_filename_fmt, remote_filename); SYSDEBUG("path buffer contents: '%s'", path); SYSDEBUG("path buffer size: %zu", sizeof(path)); SYSDEBUG("path strlen: %zu", strlen(path)); - SYSDEBUG("remote_filename_fmt_len: %d", remote_filename_fmt_len); - SYSDEBUG("maxlen for snprintf: %zu", sizeof(path) - strlen(path) - remote_filename_fmt_len); + SYSDEBUG("maxlen for snprintf: %zu", sizeof(path) - strlen(path)); - snprintf(path + strlen(path), sizeof(path) - strlen(path) - remote_filename_fmt_len, remote_filename_fmt, remote_filename); + snprintf(path + strlen(path), sizeof(path) - strlen(path), "/%s", remote_filename); char *errmsg = NULL; SYSDEBUG("%s", "Downloading..."); long fetch_status = download(url, path, &errmsg); diff --git a/src/lib/core/conda.c b/src/lib/core/conda.c index c8cdb25..4daf0dc 100644 --- a/src/lib/core/conda.c +++ b/src/lib/core/conda.c @@ -17,10 +17,8 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) { strncpy(sys.machine, "64", sizeof(sys.machine) - 1); } - char url[PATH_MAX]; - const char *url_fmt = "https://micro.mamba.pm/api/micromamba/%s-%s/latest"; - const int url_fmt_len = snprintf(NULL, 0, url_fmt, sys.sysname, sys.machine); - snprintf(url, sizeof(url) - url_fmt_len, url_fmt, sys.sysname, sys.machine); + char url[PATH_MAX] = {0}; + snprintf(url, sizeof(url), "https://micro.mamba.pm/api/micromamba/%s-%s/latest", sys.sysname, sys.machine); char installer_path[PATH_MAX]; snprintf(installer_path, sizeof(installer_path), "%s/latest", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); @@ -95,16 +93,12 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) { int python_exec(const char *args) { const char *command_base = "python "; - const char *command_fmt = "%s%s"; - const int len = snprintf(NULL, 0, command_fmt, command_base, args); - char *command = calloc(len + 1, sizeof(*command)); - if (!command) { - SYSERROR("Unable to allocate %d bytes for command string", len); + char *command = NULL; + if (asprintf(&command, "%s%s", command_base, args) < 0 || !command) { + SYSERROR("%s", "Unable to allocate command string"); return -1; } - - snprintf(command, len + 1, command_fmt, command_base, args); msg(STASIS_MSG_L3, "Executing: %s\n", command); const int result = system(command); @@ -114,15 +108,12 @@ int python_exec(const char *args) { int pip_exec(const char *args) { const char *command_base = "python -m pip "; - const char *command_fmt = "%s%s"; - const int len = snprintf(NULL, 0, command_fmt, command_base, args); - char *command = calloc(len + 1, sizeof(*command)); - if (!command) { - SYSERROR("Unable to allocate %d bytes for command string", len); + char *command = NULL; + if (asprintf(&command, "%s%s", command_base, args) < 0 || !command) { + SYSERROR("%s", "Unable to allocate command string"); return -1; } - snprintf(command, len + 1, command_fmt, command_base, args); msg(STASIS_MSG_L3, "Executing: %s\n", command); const int result = system(command); @@ -170,7 +161,7 @@ int pkg_index_provides(int mode, const char *index, const char *spec) { int status = 0; struct Process proc = {0}; proc.redirect_stderr = 1; - strncpy(proc.f_stdout, logfile, sizeof(proc.f_stdout) - 1); + snprintf(proc.f_stdout, sizeof(proc.f_stdout), "%s", logfile); if (mode == PKG_USE_PIP) { // Do an installation in dry-run mode to see if the package exists in the given index. @@ -512,8 +503,8 @@ int conda_setup_headless() { if (isempty(item)) { continue; } - const int cmd_fmt_len = snprintf(NULL, 0, cmd_fmt, item); - snprintf(cmd + strlen(cmd), sizeof(cmd) - strlen(cmd) - cmd_fmt_len, cmd_fmt, item); + + snprintf(cmd + strlen(cmd), sizeof(cmd) - strlen(cmd), cmd_fmt, item); if (i < total - 1) { strncat(cmd, " ", sizeof(cmd) - strlen(cmd) - 1); } @@ -605,13 +596,12 @@ int conda_env_create_from_uri(char *name, char *uri, char *python_version) { file_replace_text(tempfile, "- python\n", spec, 0); const char *fmt = "env create -n '%s' --file='%s'"; - int len = snprintf(NULL, 0, fmt, name, tempfile); - char *env_command = calloc(len + 1, sizeof(*env_command)); - if (!env_command) { + char *env_command = NULL; + if (asprintf(&env_command, fmt, name, tempfile) < 0 || !env_command) { + SYSERROR("%s", "unable to allocate environment command"); return -1; } - snprintf(env_command, len + 1, fmt, name, tempfile); const int status = conda_exec(env_command); unlink(tempfile); guard_free(env_command); @@ -669,7 +659,6 @@ int conda_index(const char *path) { int conda_env_exists(const char *root, const char *name) { char path[PATH_MAX] = {0}; - const int len = snprintf(NULL, 0, "%s/%s", root, name); - snprintf(path, sizeof(path) - len, "%s/envs/%s", root, name); + snprintf(path, sizeof(path), "%s/envs/%s", root, name); return access(path, F_OK) == 0; } diff --git a/src/lib/core/multiprocessing.c b/src/lib/core/multiprocessing.c index 69bcc02..8d848fe 100644 --- a/src/lib/core/multiprocessing.c +++ b/src/lib/core/multiprocessing.c @@ -74,9 +74,8 @@ int child(struct MultiProcessingPool *pool, struct MultiProcessingTask *task) { // Set log file name if (globals.enable_task_logging) { - const char *log_file_fmt = "task-%zu-%d.log"; - const int log_file_len = snprintf(NULL, 0, log_file_fmt, mp_global_task_count, task->parent_pid); - snprintf(task->log_file + strlen(task->log_file), sizeof(task->log_file) - strlen(task->log_file) - log_file_len, log_file_fmt, mp_global_task_count, task->parent_pid); + snprintf(task->log_file + strlen(task->log_file), sizeof(task->log_file) - strlen(task->log_file), + "task-%zu-%d.log", mp_global_task_count, task->parent_pid); } fp_log = freopen(task->log_file, "w+", stdout); if (!fp_log) { diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index ea23024..83e684f 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -320,9 +320,7 @@ int git_clone(struct Process *proc, char *url, char *destdir, char *gitref) { if (destdir && access(destdir, F_OK) < 0) { // Destination directory does not exist - const char *command_fmt = " %s"; - const int command_fmt_len = snprintf(NULL, 0, command_fmt, destdir); - snprintf(command + strlen(command), sizeof(command) - strlen(command) - command_fmt_len, command_fmt, destdir); + snprintf(command + strlen(command), sizeof(command) - strlen(command), " %s", destdir); // Clone the repo result = shell(proc, command); if (result) { @@ -944,13 +942,8 @@ void debug_hexdump(char *data, int len) { continue; } - const char *bytes_fmt = "%02X "; - const int bytes_fmt_len = snprintf(NULL, 0, bytes_fmt, (unsigned char) *pos); - snprintf(bytes + strlen(bytes), sizeof(bytes) - strlen(bytes) - bytes_fmt_len, bytes_fmt, (unsigned char) *pos); - - const char *ascii_fmt = "%c"; - // no need to calculate length for a single character - snprintf(ascii + strlen(ascii), sizeof(ascii) - strlen(ascii), ascii_fmt, isprint(*pos) ? *pos : '.'); + snprintf(bytes + strlen(bytes), sizeof(bytes) - strlen(bytes), "%02X ", (unsigned char) *pos); + snprintf(ascii + strlen(ascii), sizeof(ascii) - strlen(ascii), "%c", isprint(*pos) ? *pos : '.'); pos++; count++; @@ -964,7 +957,7 @@ void debug_hexdump(char *data, int len) { for (int i = 0; i < padding; i++) { strncat(bytes, " ", sizeof(bytes) - strlen(bytes) - 1); } - snprintf(output, DEBUG_HEXDUMP_FMT_BYTES + sizeof(addr) + sizeof(bytes) + sizeof(ascii), "%s | %s | %s", addr, bytes, ascii); + snprintf(output, sizeof(output), "%s | %s | %s", addr, bytes, ascii); puts(output); } -- cgit