diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-04-17 10:57:43 -0400 |
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-04-17 10:57:43 -0400 |
| commit | 90cbf865cb6e88d5db6484040dc4dc885f88caed (patch) | |
| tree | ae587578f48a9cc5c307d0d8d7485947428aa708 | |
| parent | d17103f326bbf026f55a326629e41e69c21a78ad (diff) | |
| download | stasis-90cbf865cb6e88d5db6484040dc4dc885f88caed.tar.gz | |
Convert vsprintf
* Check error conditions in related v-functions
| -rw-r--r-- | src/cli/stasis_indexer/helpers.c | 10 | ||||
| -rw-r--r-- | src/lib/core/conda.c | 25 | ||||
| -rw-r--r-- | src/lib/core/utils.c | 18 |
3 files changed, 47 insertions, 6 deletions
diff --git a/src/cli/stasis_indexer/helpers.c b/src/cli/stasis_indexer/helpers.c index 2ecdc74..0debfe4 100644 --- a/src/cli/stasis_indexer/helpers.c +++ b/src/cli/stasis_indexer/helpers.c @@ -243,7 +243,15 @@ int get_files(struct StrList **out, const char *path, const char *pattern, ...) va_list args; va_start(args, pattern); char userpattern[PATH_MAX] = {0}; - vsprintf(userpattern, pattern, args); + const int len = vsnprintf(userpattern, sizeof(userpattern), pattern, args); + if (len < 0) { + SYSERROR("%s", "vsnprintf failed\n"); + va_end(args); + return -1; + } + if ((size_t) len > sizeof(userpattern)) { + fprintf(stderr, "WARNING: %s: userpattern truncated!\n", __FUNCTION__); + } va_end(args); if (!strlen(userpattern)) { userpattern[0] = '*'; diff --git a/src/lib/core/conda.c b/src/lib/core/conda.c index 3ef4d62..a12bdee 100644 --- a/src/lib/core/conda.c +++ b/src/lib/core/conda.c @@ -49,10 +49,31 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) { } char cmd[STASIS_BUFSIZ] = {0}; - snprintf(cmd, sizeof(cmd), "%s -r %s -p %s ", mmbin, info->conda_prefix, info->conda_prefix); va_list args; + int cmd_len = snprintf(cmd, sizeof(cmd), "%s -r %s -p %s ", mmbin, info->conda_prefix, info->conda_prefix); + if (cmd_len < 0) { + SYSERROR("%s", "Unable to build argument list for micromamba"); + va_end(args); + return -1; + } + if ((size_t) cmd_len > sizeof(cmd)) { + SYSERROR("%s", "micromamba command truncated"); + va_end(args); + return -1; + } + va_start(args, command); - vsprintf(cmd + strlen(cmd), command, args); + cmd_len = vsnprintf(cmd + strlen(cmd), sizeof(cmd) - cmd_len, command, args); + if (cmd_len < 0) { + SYSERROR("%s", "Unable to append arguments to micromamba command"); + va_end(args); + return -1; + } + if ((size_t) cmd_len > sizeof(cmd)) { + SYSERROR("%s", "micromamba command truncated while appending arguments"); + va_end(args); + return -1; + } va_end(args); mkdirs(info->conda_prefix, 0755); diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index 6795931..16f9dbe 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -462,9 +462,21 @@ void msg(unsigned type, char *fmt, ...) { snprintf(header, sizeof(header), STASIS_COLOR_BLUE " ->%s" STASIS_COLOR_RESET, status); } - fprintf(stream, "%s", header); - vfprintf(stream, fmt, args); - fprintf(stream, "%s", STASIS_COLOR_RESET); + if (fprintf(stream, "%s", header) < 0) { + SYSERROR("%s", "unable to write message header to stream"); + return; + } + + const int len = vfprintf(stream, fmt, args); + if (len < 0) { + SYSERROR("%s", "unable to write message to stream"); + return; + } + + if (fprintf(stream, "%s", STASIS_COLOR_RESET) < 0) { + SYSERROR("%s", "unable to write message footer to stream"); + return; + } va_end(args); } |
