aboutsummaryrefslogtreecommitdiff
path: root/src/lib/core
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2026-04-28 11:45:10 -0400
committerGitHub <noreply@github.com>2026-04-28 11:45:10 -0400
commit10f13b36560c5112554c54d8958081b7aa518050 (patch)
treebd27866c4352de38ebc5b13f8208768d576ae904 /src/lib/core
parent017bc273aedf3f20512beeb78a2f513913e56305 (diff)
parentd761cc976a8d645e61d23f6e43dcfec1a7fdb061 (diff)
downloadstasis-10f13b36560c5112554c54d8958081b7aa518050.tar.gz
Merge pull request #137 from jhunkeler/bughunt-1001
Bug Hunt 0x1001
Diffstat (limited to 'src/lib/core')
-rw-r--r--src/lib/core/artifactory.c11
-rw-r--r--src/lib/core/conda.c68
-rw-r--r--src/lib/core/copy.c1
-rw-r--r--src/lib/core/docker.c9
-rw-r--r--src/lib/core/envctl.c2
-rw-r--r--src/lib/core/environment.c1
-rw-r--r--src/lib/core/include/conda.h4
-rw-r--r--src/lib/core/include/core_message.h4
-rw-r--r--src/lib/core/ini.c64
-rw-r--r--src/lib/core/multiprocessing.c1
-rw-r--r--src/lib/core/recipe.c1
-rw-r--r--src/lib/core/relocation.c6
-rw-r--r--src/lib/core/str.c18
-rw-r--r--src/lib/core/strlist.c24
-rw-r--r--src/lib/core/system.c4
-rw-r--r--src/lib/core/template.c4
-rw-r--r--src/lib/core/template_func_proto.c3
-rw-r--r--src/lib/core/utils.c27
-rw-r--r--src/lib/core/wheel.c18
-rw-r--r--src/lib/core/wheelinfo.c4
20 files changed, 217 insertions, 57 deletions
diff --git a/src/lib/core/artifactory.c b/src/lib/core/artifactory.c
index 2490346..54f4ba0 100644
--- a/src/lib/core/artifactory.c
+++ b/src/lib/core/artifactory.c
@@ -26,14 +26,17 @@ int artifactory_download_cli(char *dest,
// convert platform string to lower-case
SYSDEBUG("%s", "Set os_ident");
strncpy(os_ident, os, sizeof(os_ident) - 1);
+ os_ident[sizeof(os_ident) - 1] = '\0';
tolower_s(os_ident);
SYSDEBUG("os_ident=%s", os_ident);
// translate OS identifier
if (!strcmp(os_ident, "darwin") || startswith(os_ident, "macos")) {
strncpy(os_ident, "mac", sizeof(os_ident) - 1);
+ os_ident[sizeof(os_ident) - 1] = '\0';
} else if (!strcmp(os_ident, "linux")) {
strncpy(os_ident, "linux", sizeof(os_ident) - 1);
+ os_ident[sizeof(os_ident) - 1] = '\0';
} else {
fprintf(stderr, "%s: unknown operating system: %s\n", __FUNCTION__, os_ident);
return -1;
@@ -42,7 +45,9 @@ int artifactory_download_cli(char *dest,
// translate ARCH identifier
SYSDEBUG("%s", "Set arch_ident");
strncpy(arch_ident, arch, sizeof(arch_ident) - 1);
+ arch_ident[sizeof(arch_ident) - 1] = '\0';
SYSDEBUG("arch_ident=%s", arch_ident);
+
if (startswith(arch_ident, "i") && endswith(arch_ident, "86")) {
strncpy(arch_ident, "386", sizeof(arch_ident) - 1);
} else if (!strcmp(arch_ident, "amd64") || !strcmp(arch_ident, "x86_64") || !strcmp(arch_ident, "x64")) {
@@ -57,6 +62,8 @@ int artifactory_download_cli(char *dest,
fprintf(stderr, "%s: unknown architecture: %s\n", __FUNCTION__, arch_ident);
return -1;
}
+ arch_ident[sizeof(arch_ident) - 1] = '\0';
+
SYSDEBUG("%s", "Construct URL");
snprintf(url, sizeof(url), "%s/%s/%s/%s/%s-%s-%s/%s",
@@ -69,6 +76,7 @@ int artifactory_download_cli(char *dest,
arch_ident, // jfrog-cli-linux-x86_64
remote_filename); // jf
strncpy(path, dest, sizeof(path) - 1);
+ path[sizeof(path) - 1] = '\0';
if (mkdirs(path, 0755)) {
fprintf(stderr, "%s: %s: %s", __FUNCTION__, path, strerror(errno));
@@ -265,7 +273,10 @@ int jfrog_cli(struct JFRT_Auth *auth, const char *subsystem, const char *task, c
if (!globals.verbose) {
strncpy(proc.f_stdout, "/dev/null", sizeof(proc.f_stdout) - 1);
+ proc.f_stdout[sizeof(proc.f_stdout) - 1] = '\0';
+
strncpy(proc.f_stderr, "/dev/null", sizeof(proc.f_stderr) - 1);
+ proc.f_stderr[sizeof(proc.f_stderr) - 1] = '\0';
}
return shell(&proc, cmd);
}
diff --git a/src/lib/core/conda.c b/src/lib/core/conda.c
index 854d56d..4557c5c 100644
--- a/src/lib/core/conda.c
+++ b/src/lib/core/conda.c
@@ -11,17 +11,30 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) {
tolower_s(sys.sysname);
if (!strcmp(sys.sysname, "darwin")) {
strncpy(sys.sysname, "osx", sizeof(sys.sysname) - 1);
+ sys.sysname[sizeof(sys.sysname) - 1] = '\0';
}
if (!strcmp(sys.machine, "x86_64")) {
strncpy(sys.machine, "64", sizeof(sys.machine) - 1);
+ sys.machine[sizeof(sys.machine) - 1] = '\0';
+ }
+
+ if (!info->download_dir || isempty(info->download_dir)) {
+ SYSERROR("%s", "micromamba inf->download_dir is NULL, or empty");
+ return -1;
+ }
+
+ if (mkdirs(info->download_dir, 0755) < 0) {
+ SYSERROR("Unable to create info->download_dir: %s", info->download_dir);
+ return -1;
}
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");
+ const char installer_name[] = "mm_latest";
+ char installer_path[PATH_MAX] = {0};
+ snprintf(installer_path, sizeof(installer_path), "%s/%s", info->download_dir, installer_name);
if (access(installer_path, F_OK)) {
char *errmsg = NULL;
@@ -39,7 +52,9 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) {
if (access(mmbin, F_OK)) {
char untarcmd[PATH_MAX * 2];
mkdirs(info->micromamba_prefix, 0755);
- snprintf(untarcmd, sizeof(untarcmd), "tar -xvf %s -C %s --strip-components=1 bin/micromamba 1>/dev/null", installer_path, info->micromamba_prefix);
+ snprintf(untarcmd, sizeof(untarcmd),
+ "tar -xvf %s -C %s --strip-components=1 bin/micromamba 1>/dev/null",
+ installer_path, info->micromamba_prefix);
int untarcmd_status = system(untarcmd);
if (untarcmd_status) {
return -1;
@@ -135,7 +150,7 @@ const char *pkg_index_provides_strerror(int code) {
return PKG_ERROR_STR[code];
}
-int pkg_index_provides(int mode, const char *index, const char *spec) {
+int pkg_index_provides(int mode, const char *index, const char *spec, const char *logdir) {
char cmd[PATH_MAX] = {0};
char spec_local[255] = {0};
@@ -146,11 +161,19 @@ int pkg_index_provides(int mode, const char *index, const char *spec) {
// Normalize the local spec string
strncpy(spec_local, spec, sizeof(spec_local) - 1);
+ spec_local[sizeof(spec_local) - 1] = '\0';
tolower_s(spec_local);
lstrip(spec_local);
strip(spec_local);
- char logfile[] = "/tmp/STASIS-package_exists.XXXXXX";
+ if (mkdirs(logdir, 0700) < 0) {
+ SYSERROR("Unable to create log directory: %s", logdir ? logdir : "NULL");
+ return -1;
+ }
+ const char logfile_template[] = "STASIS-package_exists.XXXXXX";
+ char logfile[PATH_MAX] = {0};
+ snprintf(logfile, sizeof(logfile), "%s/%s", logdir, logfile_template);
+
int logfd = mkstemp(logfile);
if (logfd < 0) {
perror(logfile);
@@ -167,12 +190,15 @@ int pkg_index_provides(int mode, const char *index, const char *spec) {
// Do an installation in dry-run mode to see if the package exists in the given index.
// The --force argument ignores local installation and cache, and actually polls the remote index(es)
strncpy(cmd, "python -m pip install --force --dry-run --no-cache --no-deps ", sizeof(cmd) - 1);
+ cmd[sizeof(cmd) - 1] = '\0';
if (index) {
snprintf(cmd + strlen(cmd), sizeof(cmd) - strlen(cmd), "--index-url='%s' ", index);
}
snprintf(cmd + strlen(cmd), sizeof(cmd) - strlen(cmd), "'%s' ", spec_local);
} else if (mode == PKG_USE_CONDA) {
strncpy(cmd, "mamba search ", sizeof(cmd) - 1);
+ cmd[sizeof(cmd) - 1] = '\0';
+
if (index) {
snprintf(cmd + strlen(cmd), sizeof(cmd) - strlen(cmd), "--channel '%s' ", index);
}
@@ -181,11 +207,24 @@ int pkg_index_provides(int mode, const char *index, const char *spec) {
return PKG_INDEX_PROVIDES_E_INTERNAL_MODE_UNKNOWN;
}
+#if defined(DEBUG)
+ const int debug_log = 1;
+#else
+ const debug_log = 0;
+#endif
+
// Print errors only when shell() itself throws one
// If some day we want to see the errors thrown by pip too, use this
// condition instead: (status != 0)
+ if (debug_log) {
+ SYSDEBUG("Executing: %s", cmd);
+ }
status = shell(&proc, cmd);
- if (status < 0) {
+
+ if (debug_log) {
+ SYSDEBUG("Log file: %s", logfile);
+ }
+ if (status < 0 || debug_log) {
FILE *fp = fdopen(logfd, "r");
if (!fp) {
remove(logfile);
@@ -195,7 +234,11 @@ int pkg_index_provides(int mode, const char *index, const char *spec) {
fflush(stdout);
fflush(stderr);
while (fgets(line, sizeof(line) - 1, fp) != NULL) {
- fprintf(stderr, "%s", line);
+ if (debug_log) {
+ SYSDEBUG("%s", strip(line));
+ } else {
+ fprintf(stderr, "%s", line);
+ }
}
fflush(stderr);
fclose(fp);
@@ -247,6 +290,8 @@ int conda_exec(const char *args) {
break;
}
}
+ conda_as[sizeof(conda_as) - 1] = '\0';
+
const char *command_fmt = "%s %s";
const int len = snprintf(NULL, 0, command_fmt, conda_as, args);
@@ -352,7 +397,8 @@ int conda_activate(const char *root, const char *env_name) {
close(fd);
// Configure our process for output to a log file
- strncpy(proc.f_stdout, logfile, PATH_MAX - 1);
+ strncpy(proc.f_stdout, logfile, sizeof(proc.f_stdout) - 1);
+ proc.f_stdout[sizeof(proc.f_stdout) - 1] = '\0';
// Verify conda's init scripts are available
if (access(path_conda, F_OK) < 0) {
@@ -497,6 +543,7 @@ int conda_setup_headless() {
if (globals.conda_packages && strlist_count(globals.conda_packages)) {
memset(cmd, 0, sizeof(cmd));
strncpy(cmd, "install ", sizeof(cmd) - 1);
+ cmd[sizeof(cmd) - 1] = '\0';
total = strlist_count(globals.conda_packages);
for (size_t i = 0; i < total; i++) {
@@ -520,6 +567,7 @@ int conda_setup_headless() {
if (globals.pip_packages && strlist_count(globals.pip_packages)) {
memset(cmd, 0, sizeof(cmd));
strncpy(cmd, "install ", sizeof(cmd) - 1);
+ cmd[sizeof(cmd) - 1] = '\0';
total = strlist_count(globals.pip_packages);
for (size_t i = 0; i < total; i++) {
@@ -561,11 +609,11 @@ int conda_env_create_from_uri(char *name, char *uri, char *python_version) {
// Convert a bare system path to a file:// path
if (!strstr(uri, "://")) {
- uri_fs = calloc(strlen(uri) + strlen("file://") + 1, sizeof(*uri_fs));
+ uri_fs = calloc(PATH_MAX, sizeof(*uri_fs));
if (!uri_fs) {
return -1;
}
- snprintf(uri_fs, strlen(uri) + strlen("file://") + 1, "%s%s", "file://", uri);
+ snprintf(uri_fs, PATH_MAX, "%s%s", "file://", uri);
}
char tempfile[PATH_MAX] = {0};
diff --git a/src/lib/core/copy.c b/src/lib/core/copy.c
index 5b4e468..6697f59 100644
--- a/src/lib/core/copy.c
+++ b/src/lib/core/copy.c
@@ -15,6 +15,7 @@ int copy2(const char *src, const char *dest, unsigned int op) {
char dname[1024] = {0};
strncpy(dname, dest, sizeof(dname) - 1);
+ dname[sizeof(dname) - 1] = '\0';
char *dname_endptr = strrchr(dname, '/');
if (dname_endptr != NULL) {
diff --git a/src/lib/core/docker.c b/src/lib/core/docker.c
index b289e5a..484f476 100644
--- a/src/lib/core/docker.c
+++ b/src/lib/core/docker.c
@@ -19,9 +19,11 @@ int docker_exec(const char *args, const unsigned flags) {
if (final_flags & STASIS_DOCKER_QUIET_STDOUT) {
strncpy(proc.f_stdout, "/dev/null", sizeof(proc.f_stdout) - 1);
+ proc.f_stdout[sizeof(proc.f_stdout) - 1] = '\0';
}
if (final_flags & STASIS_DOCKER_QUIET_STDERR) {
strncpy(proc.f_stderr, "/dev/null", sizeof(proc.f_stderr) - 1);
+ proc.f_stderr[sizeof(proc.f_stderr) - 1] = '\0';
}
if (!final_flags) {
@@ -74,6 +76,8 @@ int docker_build(const char *dirpath, const char *args, int engine) {
if (engine & STASIS_DOCKER_BUILD_X) {
strncpy(build, "buildx build", sizeof(build) - 1);
}
+ build[sizeof(build) - 1] = '\0';
+
snprintf(cmd, sizeof(cmd), "%s %s %s", build, args, dirpath);
return docker_exec(cmd, 0);
}
@@ -94,6 +98,7 @@ int docker_save(const char *image, const char *destdir, const char *compression_
} else {
strncpy(ext, compression_program, sizeof(ext) - 1);
}
+ ext[sizeof(ext) - 1] = '\0';
snprintf(cmd, sizeof(cmd), "save \"%s\" | %s > \"%s/%s.tar.%s\"", image, compression_program, destdir, image, ext);
} else {
snprintf(cmd, sizeof(cmd), "save \"%s\" -o \"%s/%s.tar\"", image, destdir, image);
@@ -122,7 +127,11 @@ static char *docker_ident() {
memset(&proc, 0, sizeof(proc));
strncpy(proc.f_stdout, tempfile, sizeof(proc.f_stdout) - 1);
+ proc.f_stdout[sizeof(proc.f_stdout) - 1] = '\0';
+
strncpy(proc.f_stderr, "/dev/null", sizeof(proc.f_stderr) - 1);
+ proc.f_stderr[sizeof(proc.f_stderr) - 1] = '\0';
+
shell(&proc, "docker --version");
if (!freopen(tempfile, "r", fp)) {
diff --git a/src/lib/core/envctl.c b/src/lib/core/envctl.c
index d8d1b3d..5987616 100644
--- a/src/lib/core/envctl.c
+++ b/src/lib/core/envctl.c
@@ -53,7 +53,7 @@ size_t envctl_get_index(const struct EnvCtl *envctl, const char *name) {
for (size_t i = 0; i < envctl->num_used; i++) {
if (!strcmp(envctl->item[i]->name, name)) {
// pack state flag, outer (struct) index and inner (name) index
- return 1L << 63L | i;
+ return 1UL << 63UL | i;
}
}
return 0;
diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c
index 3c94d33..1bd9d28 100644
--- a/src/lib/core/environment.c
+++ b/src/lib/core/environment.c
@@ -90,6 +90,7 @@ void runtime_export(RuntimeEnv *env, char **keys) {
break;
}
}
+ export_command[sizeof(export_command) - 1] = '\0';
for (size_t i = 0; i < strlist_count(env); i++) {
char output[STASIS_BUFSIZ] = {0};
diff --git a/src/lib/core/include/conda.h b/src/lib/core/include/conda.h
index f3d481c..cc9426d 100644
--- a/src/lib/core/include/conda.h
+++ b/src/lib/core/include/conda.h
@@ -29,6 +29,7 @@
struct MicromambaInfo {
char *micromamba_prefix; //!< Path to write micromamba binary
char *conda_prefix; //!< Path to install conda base tree
+ char *download_dir; //!< Path to store micromamba installer
};
/**
@@ -219,12 +220,13 @@ int conda_index(const char *path);
* @param mode USE_CONDA
* @param index a file system path or url pointing to a simple index or conda channel
* @param spec a pip package specification (e.g. `name==1.2.3`)
+ * @param logdir the directory to store the output log
* @param spec a conda package specification (e.g. `name=1.2.3`)
* @return PKG_NOT_FOUND, if not found
* @return PKG_FOUND, if found
* @return PKG_E_INDEX_PROVIDES_{ERROR}, on error (see conda.h)
*/
-int pkg_index_provides(int mode, const char *index, const char *spec);
+int pkg_index_provides(int mode, const char *index, const char *spec, const char *logdir);
const char *pkg_index_provides_strerror(int code);
char *conda_get_active_environment();
diff --git a/src/lib/core/include/core_message.h b/src/lib/core/include/core_message.h
index 1ffa846..aab7203 100644
--- a/src/lib/core/include/core_message.h
+++ b/src/lib/core/include/core_message.h
@@ -3,13 +3,13 @@
#define STASIS_CORE_MESSAGE_H
#define SYSERROR(MSG, ...) do { \
- fprintf(stderr, "%s:%d:%s():%s - ", path_basename(__FILE__), __LINE__, __FUNCTION__, (errno > 0) ? strerror(errno) : "info"); \
+ fprintf(stderr, STASIS_COLOR_RED "ERROR: " STASIS_COLOR_RESET STASIS_COLOR_WHITE "%s:%d:%s()" STASIS_COLOR_RESET ":%s - ", path_basename(__FILE__), __LINE__, __FUNCTION__, (errno > 0) ? strerror(errno) : "info"); \
fprintf(stderr, MSG LINE_SEP, __VA_ARGS__); \
} while (0)
#ifdef DEBUG
#define SYSDEBUG(MSG, ...) do { \
- fprintf(stderr, "DEBUG: %s:%d:%s(): ", path_basename(__FILE__), __LINE__, __FUNCTION__); \
+ fprintf(stderr, STASIS_COLOR_BLUE "DEBUG: " STASIS_COLOR_RESET STASIS_COLOR_WHITE "%s:%d:%s()" STASIS_COLOR_RESET ": ", path_basename(__FILE__), __LINE__, __FUNCTION__); \
fprintf(stderr, MSG LINE_SEP, __VA_ARGS__); \
} while (0)
#else
diff --git a/src/lib/core/ini.c b/src/lib/core/ini.c
index 6995eb2..c37030c 100644
--- a/src/lib/core/ini.c
+++ b/src/lib/core/ini.c
@@ -181,7 +181,9 @@ int ini_getval(struct INIFILE *ini, char *section_name, char *key, int type, int
break;
case INIVAL_TYPE_STR_ARRAY:
strncpy(tbufp, data_copy, sizeof(tbuf) - 1);
+ tbuf[sizeof(tbuf) - 1] = '\0';
guard_free(data_copy);
+
data_copy = calloc(STASIS_BUFSIZ, sizeof(*data_copy));
if (!data_copy) {
return -1;
@@ -345,6 +347,10 @@ int ini_data_append(struct INIFILE **ini, char *section_name, char *key, char *v
section->data_count++;
} else {
struct INIData *data = ini_data_get(*ini, section_name, key);
+ if (!data) {
+ SYSERROR("%s:%s: key does not exist", section_name, key);
+ return -1;
+ }
size_t value_len_old = strlen(data->value);
size_t value_len = strlen(value);
size_t value_len_new = value_len_old + value_len;
@@ -353,9 +359,9 @@ int ini_data_append(struct INIFILE **ini, char *section_name, char *key, char *v
if (!value_tmp) {
SYSERROR("Unable to increase data->value size to %zu bytes", value_len_new + 2);
return -1;
- } else {
- data->value = value_tmp;
}
+ data->value = value_tmp;
+
strncat(data->value, value, value_len_new - strlen(data->value));
}
return 0;
@@ -420,6 +426,7 @@ int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode) {
for (size_t x = 0; x < ini->section_count; x++) {
struct INISection *section = ini->section[x];
char *section_name = section->key;
+
fprintf(*stream, "[%s]" LINE_SEP, section_name);
for (size_t y = 0; y < ini->section[x]->data_count; y++) {
@@ -428,6 +435,7 @@ int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode) {
char *key = data->key;
char *value = data->value;
unsigned *hint = &data->type_hint;
+
memset(outvalue, 0, sizeof(outvalue));
if (key && value) {
@@ -440,6 +448,9 @@ int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode) {
xvalue = ini_getval_str(ini, section_name, key, (int) mode, &err);
value = xvalue;
}
+
+ const size_t buf_size = sizeof(outvalue);
+ size_t buf_len = 0;
char **parts = split(value, LINE_SEP, 0);
for (size_t p = 0; parts && parts[p] != NULL; p++) {
char *render = NULL;
@@ -454,15 +465,16 @@ int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode) {
return -1;
}
+ buf_len = strlen(outvalue);
if (*hint == INIVAL_TYPE_STR_ARRAY) {
- int leading_space = isspace(*render);
+ const int leading_space = isspace(*render);
if (leading_space) {
- snprintf(outvalue + strlen(outvalue), sizeof(outvalue) - strlen(outvalue), "%s" LINE_SEP, render);
+ snprintf(outvalue + buf_len, buf_size - buf_len, "%s" LINE_SEP, render);
} else {
- snprintf(outvalue + strlen(outvalue), sizeof(outvalue) - strlen(outvalue), " %s" LINE_SEP, render);
+ snprintf(outvalue + buf_len, buf_size - buf_len, " %s" LINE_SEP, render);
}
} else {
- snprintf(outvalue + strlen(outvalue), sizeof(outvalue) - strlen(outvalue), "%s", render);
+ snprintf(outvalue + buf_len, buf_size - buf_len, "%s", render);
}
if (mode == INI_WRITE_PRESERVE) {
guard_free(render);
@@ -470,7 +482,12 @@ int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode) {
}
guard_array_free(parts);
strip(outvalue);
- strncat(outvalue, LINE_SEP, sizeof(outvalue) - strlen(outvalue) - 1);
+
+ // update length of outvalue
+ buf_len = strlen(outvalue);
+
+ snprintf(outvalue + buf_len, buf_size - buf_len, "%s", LINE_SEP);
+
fprintf(*stream, "%s = %s%s", ini->section[x]->data[y]->key, *hint == INIVAL_TYPE_STR_ARRAY ? LINE_SEP : "", outvalue);
guard_free(value);
} else {
@@ -493,12 +510,9 @@ char *unquote(char *s) {
void ini_free(struct INIFILE **ini) {
for (size_t section = 0; section < (*ini)->section_count; section++) {
- SYSDEBUG("freeing section: %s", (*ini)->section[section]->key);
for (size_t data = 0; data < (*ini)->section[section]->data_count; data++) {
if ((*ini)->section[section]->data[data]) {
- SYSDEBUG("freeing data key: %s", (*ini)->section[section]->data[data]->key);
guard_free((*ini)->section[section]->data[data]->key);
- SYSDEBUG("freeing data value: %s", (*ini)->section[section]->data[data]->value);
guard_free((*ini)->section[section]->data[data]->value);
guard_free((*ini)->section[section]->data[data]);
}
@@ -526,6 +540,7 @@ struct INIFILE *ini_open(const char *filename) {
// Create an implicit section. [default] does not need to be present in the INI config
ini_section_create(&ini, "default");
strncpy(current_section, "default", sizeof(current_section) - 1);
+ current_section[sizeof(current_section) - 1] = '\0';
// Open the configuration file for reading
FILE *fp = fopen(filename, "r");
@@ -538,15 +553,15 @@ struct INIFILE *ini_open(const char *filename) {
unsigned hint = 0;
int multiline_data = 0;
int no_data = 0;
- char inikey[2][255];
+ char inikey[2][255] = {0};
char *key = inikey[0];
char *key_last = inikey[1];
char value[STASIS_BUFSIZ] = {0};
- memset(inikey, 0, sizeof(inikey));
-
// Read file
for (size_t i = 0; fgets(line, sizeof(line), fp) != NULL; i++) {
+ const size_t key_last_size = sizeof(inikey[1]);
+ const size_t key_size = sizeof(inikey[0]);
if (no_data && multiline_data) {
if (!isempty(line)) {
no_data = 0;
@@ -555,7 +570,7 @@ struct INIFILE *ini_open(const char *filename) {
}
memset(value, 0, sizeof(value));
} else {
- memset(key, 0, sizeof(inikey[0]));
+ memset(key, 0, key_size);
}
// Find pointer to first comment character
char *comment = strpbrk(line, ";#");
@@ -579,7 +594,7 @@ struct INIFILE *ini_open(const char *filename) {
// Test for section header: [string]
if (startswith(line, "[")) {
// The previous key is irrelevant now
- memset(key_last, 0, sizeof(inikey[1]));
+ memset(key_last, 0, key_last_size);
char *section_name = substring_between(line, "[]");
if (!section_name) {
@@ -600,6 +615,8 @@ struct INIFILE *ini_open(const char *filename) {
// Record the name of the section. This is used until another section is found.
memset(current_section, 0, sizeof(current_section));
strncpy(current_section, section_name, sizeof(current_section) - 1);
+ current_section[sizeof(current_section) - 1] = '\0';
+
guard_free(section_name);
memset(line, 0, sizeof(line));
continue;
@@ -619,18 +636,25 @@ struct INIFILE *ini_open(const char *filename) {
if (operator) {
size_t key_len = operator - line;
- memset(key, 0, sizeof(inikey[0]));
+ memset(key, 0, key_size);
+
strncpy(key, line, key_len);
+ key[key_size - 1] = '\0';
lstrip(key);
strip(key);
- memset(key_last, 0, sizeof(inikey[1]));
- strncpy(key_last, key, sizeof(inikey[1]) - 1);
+
+ memset(key_last, 0, key_last_size);
+ strncpy(key_last, key, key_last_size - 1);
+ key_last[key_last_size - 1] = '\0';
+
reading_value = 1;
if (strlen(operator) > 1) {
strncpy(value, &operator[1], sizeof(value) - 1);
} else {
strncpy(value, "", sizeof(value) - 1);
}
+ value[sizeof(value) - 1] = '\0';
+
if (isempty(value)) {
//printf("%s is probably long raw data\n", key);
hint = INIVAL_TYPE_STR_ARRAY;
@@ -643,8 +667,10 @@ struct INIFILE *ini_open(const char *filename) {
}
strip(value);
} else {
- strncpy(key, key_last, sizeof(inikey[0]) - 1);
+ strncpy(key, key_last, key_size - 1);
+ key[key_size - 1] = '\0';
strncpy(value, line, sizeof(value) - 1);
+ value[sizeof(value) - 1] = '\0';
}
memset(line, 0, sizeof(line));
diff --git a/src/lib/core/multiprocessing.c b/src/lib/core/multiprocessing.c
index 7b16af3..8fd8b93 100644
--- a/src/lib/core/multiprocessing.c
+++ b/src/lib/core/multiprocessing.c
@@ -261,7 +261,6 @@ void mp_pool_show_summary(struct MultiProcessingPool *pool) {
char duration[255] = {0};
seconds_to_human_readable(task->time_data.duration, duration, sizeof(duration));
printf("%-4s %10d %10s %-10s\n", status_str, task->parent_pid, duration, task->ident) ;
- //printf("%-4s %10d %7lds %-10s\n", status_str, task->parent_pid, task->elapsed, task->ident) ;
}
puts("");
}
diff --git a/src/lib/core/recipe.c b/src/lib/core/recipe.c
index cc96139..e5769bb 100644
--- a/src/lib/core/recipe.c
+++ b/src/lib/core/recipe.c
@@ -17,6 +17,7 @@ int recipe_clone(char *recipe_dir, char *url, char *gitref, char **result) {
}
}
strncpy(*result, destdir, PATH_MAX - 1);
+ (*result)[PATH_MAX - 1] = '\0';
if (!access(destdir, F_OK)) {
if (!strcmp(destdir, "/")) {
diff --git a/src/lib/core/relocation.c b/src/lib/core/relocation.c
index ea8b9c6..16376b3 100644
--- a/src/lib/core/relocation.c
+++ b/src/lib/core/relocation.c
@@ -8,8 +8,9 @@
* Replace all occurrences of `target` with `replacement` in `original`
*
* ~~~{.c}
- * char *str = calloc(100, sizeof(char));
- * strcpy(str, "This are a test.");
+ * size_t str_maxlen = 100;
+ * char *str = calloc(str_maxlen, sizeof(char));
+ * strncpy(str, "This are a test.", str_maxlen - 1);
* if (replace_text(str, "are", "is")) {
* fprintf(stderr, "string replacement failed\n");
* exit(1);
@@ -85,6 +86,7 @@ int replace_text(char *original, const char *target, const char *replacement, un
}
// replace original with contents of buffer
strncpy(original, buffer, buffer_len + 1);
+ original[buffer_len] = '\0';
return 0;
}
diff --git a/src/lib/core/str.c b/src/lib/core/str.c
index 368ab49..a08bd2b 100644
--- a/src/lib/core/str.c
+++ b/src/lib/core/str.c
@@ -63,6 +63,7 @@ void strchrdel(char *sptr, const char *chars) {
for (size_t i = 0; i < strlen(chars); i++) {
char ch[2] = {0};
strncpy(ch, &chars[i], 1);
+ ch[sizeof(ch) - 1] = '\0';
replace_text(sptr, ch, "", 0);
}
}
@@ -105,7 +106,7 @@ char** split(char *_sptr, const char* delim, size_t max)
// Separate the string into individual parts and store them in the result array
char *token = NULL;
char *sptr_tmp = sptr;
- size_t pos = 0;
+ ptrdiff_t pos = 0;
size_t i;
for (i = 0; (token = strsep(&sptr_tmp, delim)) != NULL; i++) {
// When max is zero, record all tokens
@@ -120,6 +121,7 @@ char** split(char *_sptr, const char* delim, size_t max)
return NULL;
}
strncpy(result[i], token, STASIS_BUFSIZ - 1);
+ result[i][STASIS_BUFSIZ - 1] = '\0';
}
// pos is non-zero when maximum split is reached
@@ -130,6 +132,7 @@ char** split(char *_sptr, const char* delim, size_t max)
return NULL;
}
strncpy(result[i], &orig[pos], STASIS_BUFSIZ - 1);
+ result[i][STASIS_BUFSIZ - 1] = '\0';
}
guard_free(sptr);
@@ -562,7 +565,10 @@ char *normalize_space(char *s) {
}
// Rewrite the input string
- strncpy(result, tmp_orig, strlen(result) + 1);
+ const size_t result_len = strlen(result) + 1;
+ strncpy(result, tmp_orig, result_len);
+ result[result_len] = '\0';
+
guard_free(tmp_orig);
return result;
}
@@ -581,8 +587,16 @@ char **strdup_array(char **array) {
// Create new array
result = calloc(elems + 1, sizeof(*result));
+ if (!result) {
+ SYSERROR("%s", "could not allocate memory for result array");
+ return NULL;
+ }
for (size_t i = 0; i < elems; i++) {
result[i] = strdup(array[i]);
+ if (!result[i]) {
+ guard_array_free(result);
+ break;
+ }
}
return result;
diff --git a/src/lib/core/strlist.c b/src/lib/core/strlist.c
index ff9c098..42d5b85 100644
--- a/src/lib/core/strlist.c
+++ b/src/lib/core/strlist.c
@@ -4,6 +4,10 @@
*/
#include "download.h"
#include "strlist.h"
+
+#include <float.h>
+#include <math.h>
+
#include "utils.h"
/**
@@ -85,6 +89,8 @@ int strlist_append_file(struct StrList *pStrList, char *_path, ReaderFn *readerF
int fd;
char tempfile[PATH_MAX] = {0};
strncpy(tempfile, "/tmp/.remote_file.XXXXXX", sizeof(tempfile) - 1);
+ tempfile[sizeof(tempfile) - 1] = '\0';
+
if ((fd = mkstemp(tempfile)) < 0) {
retval = -1;
goto fatal;
@@ -420,8 +426,10 @@ void strlist_set(struct StrList **pStrList, size_t index, char *value) {
(*pStrList)->data[index] = tmp;
}
- memset((*pStrList)->data[index], '\0', strlen(value) + 1);
- strncpy((*pStrList)->data[index], value, strlen(value));
+ const size_t len = strlen(value) + 1;
+ memset((*pStrList)->data[index], '\0', len);
+ strncpy((*pStrList)->data[index], value, len);
+ (*pStrList)->data[index][len] = '\0';
}
}
@@ -671,8 +679,8 @@ float strlist_item_as_float(struct StrList *pStrList, size_t index) {
char *error_p;
strlist_clear_error();
- float result = (float) strtof(strlist_item(pStrList, index), &error_p);
- if (!result && error_p && *error_p != 0) {
+ const float result = strtof(strlist_item(pStrList, index), &error_p);
+ if ((result == FLT_MIN || result == HUGE_VALF) && errno == ERANGE) {
strlist_set_error(STRLIST_E_INVALID_VALUE);
return 0;
}
@@ -690,8 +698,8 @@ double strlist_item_as_double(struct StrList *pStrList, size_t index) {
char *error_p;
strlist_clear_error();
- double result = (double) strtod(strlist_item(pStrList, index), &error_p);
- if (!result && error_p && *error_p != 0) {
+ const double result = strtod(strlist_item(pStrList, index), &error_p);
+ if ((result == DBL_MIN || result == HUGE_VAL) && errno == ERANGE) {
strlist_set_error(STRLIST_E_INVALID_VALUE);
return 0;
}
@@ -709,8 +717,8 @@ long double strlist_item_as_long_double(struct StrList *pStrList, size_t index)
char *error_p;
strlist_clear_error();
- long double result = (long double) strtold(strlist_item(pStrList, index), &error_p);
- if (!result && error_p && *error_p != 0) {
+ const long double result = strtold(strlist_item(pStrList, index), &error_p);
+ if ((result == DBL_MIN || result == HUGE_VALL) && errno == ERANGE) {
strlist_set_error(STRLIST_E_INVALID_VALUE);
return 0;
}
diff --git a/src/lib/core/system.c b/src/lib/core/system.c
index 5b47a62..06ca7bf 100644
--- a/src/lib/core/system.c
+++ b/src/lib/core/system.c
@@ -80,7 +80,6 @@ int shell(struct Process *proc, char *args) {
fclose(fp_err);
_exit(1);
}
- fclose(fp_err);
} else {
// redirect stderr to stdout
if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
@@ -179,9 +178,8 @@ char *shell_output(const char *command, int *status) {
char *tmp = realloc(result, sizeof(*result) * current_size);
if (!tmp) {
return NULL;
- } else if (tmp != result) {
- result = tmp;
}
+ result = tmp;
}
strncat(result, line, current_size - strlen(result) - 1);
memset(line, 0, sizeof(line));
diff --git a/src/lib/core/template.c b/src/lib/core/template.c
index 3a1b759..3b6f58d 100644
--- a/src/lib/core/template.c
+++ b/src/lib/core/template.c
@@ -98,7 +98,7 @@ void tpl_register(char *key, char **ptr) {
item->ptr = ptr;
if (!replacing) {
- SYSDEBUG("Registered tpl_item at index %u:\n\tkey=%s\n\tptr=%s", tpl_pool_used, item->key, *item->ptr);
+ SYSDEBUG("Registered tpl_item at index %u:\n\tkey=%s\n\tptr=%s", tpl_pool_used, item->key, *item->ptr ? *item->ptr : "NULL");
tpl_pool[tpl_pool_used] = item;
tpl_pool_used++;
}
@@ -236,6 +236,8 @@ char *tpl_render(char *str) {
} else if (do_func) { // {{ func:NAME(a, ...) }}
char func_name_temp[STASIS_NAME_MAX] = {0};
strncpy(func_name_temp, type_stop + 1, sizeof(func_name_temp) - 1);
+ func_name_temp[sizeof(func_name_temp) - 1] = '\0';
+
char *param_begin = strchr(func_name_temp, '(');
if (!param_begin) {
fprintf(stderr, "At position %zu in %s\nfunction name must be followed by a '('\n", off, key);
diff --git a/src/lib/core/template_func_proto.c b/src/lib/core/template_func_proto.c
index fc58e33..f28a1eb 100644
--- a/src/lib/core/template_func_proto.c
+++ b/src/lib/core/template_func_proto.c
@@ -81,6 +81,8 @@ int get_junitxml_file_entrypoint(void *frame, void *data_out) {
}
char nametmp[PATH_MAX] = {0};
strncpy(nametmp, cwd, sizeof(nametmp) - 1);
+ nametmp[sizeof(nametmp) - 1] = '\0';
+
char *name = path_basename(nametmp);
*output = calloc(PATH_MAX, sizeof(**output));
@@ -106,6 +108,7 @@ int get_basetemp_dir_entrypoint(void *frame, void *data_out) {
}
char nametmp[PATH_MAX] = {0};
strncpy(nametmp, cwd, sizeof(nametmp) - 1);
+ nametmp[sizeof(nametmp) - 1] = '\0';
char *name = path_basename(nametmp);
*output = calloc(PATH_MAX, sizeof(**output));
diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c
index 2b7f0ec..90dac1d 100644
--- a/src/lib/core/utils.c
+++ b/src/lib/core/utils.c
@@ -35,9 +35,11 @@ int popd() {
int rmtree(char *_path) {
int status = 0;
char path[PATH_MAX] = {0};
- strncpy(path, _path, sizeof(path) - 1);
struct dirent *d_entity;
+ strncpy(path, _path, sizeof(path) - 1);
+ path[sizeof(path) - 1] = '\0';
+
DIR *dir = opendir(path);
if (!dir) {
return 1;
@@ -106,6 +108,7 @@ char *expandpath(const char *_path) {
char *tmphome;
if ((tmphome = getenv(homes[i])) != NULL) {
strncpy(home, tmphome, PATH_MAX - 1);
+ home[PATH_MAX - 1] = '\0';
break;
}
}
@@ -285,6 +288,7 @@ char *find_program(const char *name) {
continue;
}
strncpy(result, abspath, sizeof(result) - 1);
+ result[sizeof(result) - 1] = '\0';
break;
}
path = path_orig;
@@ -451,6 +455,7 @@ void msg(unsigned type, char *fmt, ...) {
fprintf(stream, "%s", STASIS_COLOR_GREEN);
strncpy(status, " ", sizeof(status) - 1);
}
+ status[sizeof(status) - 1] = '\0';
if (type & STASIS_MSG_L1) {
snprintf(header, sizeof(header), "==>%s" STASIS_COLOR_RESET STASIS_COLOR_WHITE, status);
@@ -496,8 +501,15 @@ char *xmkstemp(FILE **fp, const char *mode) {
if (globals.tmpdir) {
strncpy(tmpdir, globals.tmpdir, sizeof(tmpdir) - 1);
} else {
- strncpy(tmpdir, "/tmp", sizeof(tmpdir) - 1);
+ strncpy(tmpdir, "/tmp/stasis", sizeof(tmpdir) - 1);
}
+ tmpdir[sizeof(tmpdir) - 1] = '\0';
+
+ if (mkdirs(tmpdir, 0700) < 0) {
+ SYSERROR("unable to create sub-directories in %s", tmpdir);
+ return NULL;
+ }
+
memset(t_name, 0, sizeof(t_name));
snprintf(t_name, sizeof(t_name), "%s/%s", tmpdir, "STASIS.XXXXXX");
@@ -658,6 +670,7 @@ int fix_tox_conf(const char *filename, char **result, size_t maxlen) {
// Create new temporary tox configuration file
char *tempfile = xmkstemp(&fptemp, "w+");
if (!tempfile) {
+ SYSERROR("%s", "unable to create temporary file");
return -1;
}
@@ -722,6 +735,7 @@ int fix_tox_conf(const char *filename, char **result, size_t maxlen) {
// Store path to modified config
strncpy(*result, tempfile, maxlen - 1);
+ (*result)[maxlen - 1] = '\0';
guard_free(tempfile);
ini_free(&toxini);
@@ -766,11 +780,12 @@ char *collapse_whitespace(char **s) {
int redact_sensitive(const char **to_redact, size_t to_redact_size, char *src, char *dest, size_t maxlen) {
const char *redacted = "***REDACTED***";
- char *tmp = calloc(strlen(redacted) + strlen(src) + 1, sizeof(*tmp));
+ char *tmp = calloc(maxlen + 1, sizeof(*tmp));
if (!tmp) {
return -1;
}
- strncpy(tmp, src, strlen(redacted) + strlen(src));
+ strncpy(tmp, src, maxlen);
+ tmp[maxlen] = '\0';
for (size_t i = 0; i < to_redact_size; i++) {
if (to_redact[i] && strstr(tmp, to_redact[i])) {
@@ -780,7 +795,8 @@ int redact_sensitive(const char **to_redact, size_t to_redact_size, char *src, c
}
memset(dest, 0, maxlen);
- strncpy(dest, tmp, maxlen - 1);
+ strncpy(dest, tmp, maxlen);
+ dest[maxlen] = '\0';
guard_free(tmp);
return 0;
@@ -834,6 +850,7 @@ int mkdirs(const char *_path, mode_t mode) {
char *token;
char pathbuf[PATH_MAX] = {0};
strncpy(pathbuf, _path, sizeof(pathbuf) - 1);
+ pathbuf[sizeof(pathbuf) - 1] = '\0';
char *path = pathbuf;
errno = 0;
diff --git a/src/lib/core/wheel.c b/src/lib/core/wheel.c
index 0bc4209..9f3b3c8 100644
--- a/src/lib/core/wheel.c
+++ b/src/lib/core/wheel.c
@@ -69,9 +69,15 @@ static ssize_t wheel_parse_wheel(struct Wheel * pkg, const char * data) {
char **pair = split(line, ":", 1);
if (pair) {
char *key = strdup(strip(pair[0]));
- char *value = strdup(lstrip(pair[1]));
+ if (!key) {
+ SYSERROR("%s", "could not allocate memory for pair wheel key");
+ return -1;
+ }
- if (!key || !value) {
+ char *value = strdup(lstrip(pair[1]));
+ if (!value) {
+ SYSERROR("%s", "could not allocate memory for wheel value");
+ guard_free(key);
return -1;
}
@@ -90,6 +96,8 @@ static ssize_t wheel_parse_wheel(struct Wheel * pkg, const char * data) {
pkg->wheel_version = strdup(value);
if (!pkg->wheel_version) {
// memory error
+ guard_free(key);
+ guard_free(value);
wheel_package_free(&pkg);
return -1;
}
@@ -99,6 +107,8 @@ static ssize_t wheel_parse_wheel(struct Wheel * pkg, const char * data) {
pkg->generator = strdup(value);
if (!pkg->generator) {
// memory error
+ guard_free(key);
+ guard_free(value);
wheel_package_free(&pkg);
return -1;
}
@@ -108,6 +118,8 @@ static ssize_t wheel_parse_wheel(struct Wheel * pkg, const char * data) {
pkg->root_is_pure_lib = strdup(value);
if (!pkg->root_is_pure_lib) {
// memory error
+ guard_free(key);
+ guard_free(value);
wheel_package_free(&pkg);
return -1;
}
@@ -117,6 +129,8 @@ static ssize_t wheel_parse_wheel(struct Wheel * pkg, const char * data) {
if (!pkg->tag) {
pkg->tag = strlist_init();
if (!pkg->tag) {
+ guard_free(key);
+ guard_free(value);
wheel_package_free(&pkg);
return -1;
}
diff --git a/src/lib/core/wheelinfo.c b/src/lib/core/wheelinfo.c
index ce8ea74..9d8a6af 100644
--- a/src/lib/core/wheelinfo.c
+++ b/src/lib/core/wheelinfo.c
@@ -7,6 +7,7 @@ struct WheelInfo *wheelinfo_get(const char *basepath, const char *name, char *to
char package_name[NAME_MAX];
strncpy(package_name, name, sizeof(package_name) - 1);
+ package_name[sizeof(package_name) - 1] = '\0';
tolower_s(package_name);
snprintf(package_path, sizeof(package_path), "%s/%s", basepath, package_name);
@@ -19,8 +20,11 @@ struct WheelInfo *wheelinfo_get(const char *basepath, const char *name, char *to
if (!strcmp(rec->d_name, ".") || !strcmp(rec->d_name, "..")) {
continue;
}
+
char filename[NAME_MAX];
strncpy(filename, rec->d_name, sizeof(filename) - 1);
+ filename[sizeof(filename) - 1] = '\0';
+
char *ext = strstr(filename, ".whl");
if (ext) {
*ext = '\0';