diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/config.c | 3 | ||||
-rw-r--r-- | src/config_global.c | 6 | ||||
-rw-r--r-- | src/deps.c | 11 | ||||
-rw-r--r-- | src/environment.c | 6 | ||||
-rw-r--r-- | src/find.c | 28 | ||||
-rw-r--r-- | src/fs.c | 5 | ||||
-rw-r--r-- | src/internal_cmd.c | 12 | ||||
-rw-r--r-- | src/manifest.c | 23 | ||||
-rw-r--r-- | src/mime.c | 6 | ||||
-rw-r--r-- | src/rpath.c | 2 | ||||
-rw-r--r-- | src/spm.c | 10 | ||||
-rw-r--r-- | src/strings.c | 2 |
12 files changed, 68 insertions, 46 deletions
diff --git a/src/config.c b/src/config.c index 9766e9b..26938e7 100644 --- a/src/config.c +++ b/src/config.c @@ -135,10 +135,11 @@ ConfigItem **config_read(const char *filename) { // increment record count record++; // Expand config by another record - config = (ConfigItem **)reallocarray(config, record + record_initial + 1, sizeof(ConfigItem *)); + config = (ConfigItem **)reallocarray(config, (record + record_initial), sizeof(ConfigItem *)); if (!config) { perror("ConfigItem array"); fprintf(SYSERROR); + free(line); return NULL; } } diff --git a/src/config_global.c b/src/config_global.c index bda63d6..49a751e 100644 --- a/src/config_global.c +++ b/src/config_global.c @@ -301,6 +301,12 @@ void free_global_config(void) { if (SPM_GLOBAL.mirror_list) { mirror_list_free(SPM_GLOBAL.mirror_list); } + + free(SPM_GLOBAL.fs.binpath); + free(SPM_GLOBAL.fs.includepath); + free(SPM_GLOBAL.fs.libpath); + free(SPM_GLOBAL.fs.datapath); + free(SPM_GLOBAL.fs.manpath); } /** @@ -42,13 +42,8 @@ int dep_init(Dependencies **deps) { * @param deps */ void dep_free(Dependencies **deps) { - if ((*deps) != NULL) { - return; - } - for (size_t i = 0; i < (*deps)->__size; i++) { - if ((*deps)->list[i] != NULL) { - free((*deps)->list[i]); - } + for (size_t i = 0; i < (*deps)->records; i++) { + free((*deps)->list[i]); } free((*deps)); } @@ -82,7 +77,7 @@ int dep_append(Dependencies **deps, char *_name) { } (*deps)->__size++; - (*deps)->list = (char **)realloc((*deps)->list, sizeof(char *) * (*deps)->__size); + (*deps)->list = (char **)realloc((*deps)->list, (sizeof(char *) * (*deps)->__size)); if (!(*deps)->list) { free(name); return -1; diff --git a/src/environment.c b/src/environment.c index 2d9ebd4..7f25876 100644 --- a/src/environment.c +++ b/src/environment.c @@ -356,10 +356,7 @@ void runtime_set(RuntimeEnv *env, const char *_key, const char *_value) { char *key = strdup(_key); ssize_t key_offset = runtime_contains(env, key); char *value = runtime_expand_var(env, _value); - char *arr[] = { - key, value, NULL - }; - char *now = join(arr, "="); + char *now = join((char *[]) {key, value, NULL}, "="); if (key_offset != -1) { free(env->env[key_offset]); @@ -368,7 +365,6 @@ void runtime_set(RuntimeEnv *env, const char *_key, const char *_value) { else { env->num_alloc++; env->env = reallocarray(env->env, sizeof(char *), env->num_alloc); - //env->env[env->num_inuse] = (char *)calloc(strlen(now) + 1, sizeof(char)); env->env[env->num_inuse] = now; env->num_inuse++; } @@ -25,31 +25,26 @@ char *find_file(const char *root, const char *filename) { glob_t results; int glob_flags = 0; int match = 0; - char *rootpath = NULL; - char *path = NULL; + char path[PATH_MAX]; + memset(path, '\0', PATH_MAX); // GUARD if (!root || !filename || strstr(filename, "..") || strstr(filename, "./")) { return NULL; } - if (!(path = (char *)calloc(PATH_MAX + 1, sizeof(char)))) { + if (realpath(root, path) == NULL) { + perror("Cannot determine realpath()"); fprintf(SYSERROR); - exit(errno); - } - - if (!(rootpath = realpath(root, NULL))) { - free(path); return NULL; } - strcat(path, rootpath); strcat(path, "/"); strcat(path, filename); // Save a little time if the file exists if (access(path, F_OK) != -1) { - return path; + return strdup(path); } // Inject wildcard @@ -62,22 +57,15 @@ char *find_file(const char *root, const char *filename) { if (match == GLOB_NOSPACE || match == GLOB_ABORTED) { fprintf(SYSERROR); } + globfree(&results); return NULL; } - // Resize path to the length of the first match - char *want = results.gl_pathv[0]; - if (!(path = (char *)realloc(path, sizeof(char) * (strlen(want) + 1)))) { - fprintf(SYSERROR); - exit(errno); - } - // Replace path string with wanted path string - strncpy(path, want, strlen(want)); + strcpy(path, results.gl_pathv[0]); - free(rootpath); globfree(&results); - return path; + return strdup(path); } /** @@ -143,11 +143,13 @@ void fstree_free(FSTree *fsdata) { for (int i = 0; fsdata->files[i] != NULL; i++) { free(fsdata->files[i]); } + free(fsdata->files); } if (fsdata->dirs != NULL) { for (int i = 0; fsdata->dirs[i] != NULL; i++) { free(fsdata->dirs[i]); } + free(fsdata->dirs); } free(fsdata); } @@ -341,13 +343,14 @@ int rsync(const char *_args, const char *_source, const char *_destination) { returncode = proc->returncode; if (returncode != 0 && proc->output) { - fprintf(stderr, proc->output); + fprintf(stderr, "%s\n", proc->output); } shell_free(proc); if (args) { free(args); } + free(args_combined); free(source); free(destination); return returncode; diff --git a/src/internal_cmd.c b/src/internal_cmd.c index bcd0231..17d49af 100644 --- a/src/internal_cmd.c +++ b/src/internal_cmd.c @@ -101,6 +101,7 @@ int mkmanifest_interface(int argc, char **argv) { return -1; } Manifest *manifest = NULL; + int result = 0; char *pkgdir = NULL; char path[PATH_MAX]; memset(path, '\0', PATH_MAX); @@ -128,7 +129,14 @@ int mkmanifest_interface(int argc, char **argv) { return -2; } - return manifest_write(manifest, path); + result = manifest_write(manifest, path); + if (result != 0) { + manifest_free(manifest); + return -3; + } + + manifest_free(manifest); + return result; } /** @@ -219,7 +227,7 @@ int rpath_autoset_interface(int argc, char **argv) { */ void internal_command_list(void) { printf("possible commands:\n"); - for (int i = 0; internal_commands[i] != NULL; i++) { + for (size_t i = 0; internal_commands[i] != NULL; i++) { // TODO: fix double increment warning (i++ becomes i+2?) printf(" %-20s - %-20s\n", internal_commands[i], internal_commands[i + 1]); i++; } diff --git a/src/manifest.c b/src/manifest.c index 90c8087..d4f00fb 100644 --- a/src/manifest.c +++ b/src/manifest.c @@ -43,6 +43,13 @@ Manifest *manifest_from(const char *package_dir) { Manifest *info = (Manifest *)calloc(1, sizeof(Manifest)); info->records = fsdata->files_length; info->packages = (ManifestPackage **) calloc(info->records + 1, sizeof(ManifestPackage *)); + if (info->packages == NULL) { + perror("Failed to allocate package array"); + fprintf(SYSERROR); + free(info); + fstree_free(fsdata); + return NULL; + } printf("Initializing package manifest:\n"); for (size_t i = 0; i < fsdata->files_length; i++) { @@ -52,10 +59,19 @@ Manifest *manifest_from(const char *package_dir) { dep_init(&deps); if (dep_all(&deps, basename(fsdata->files[i])) < 0) { dep_free(&deps); + // TODO: Why is this freed *and* the program continues? I don't know why this is here. } // Initialize package record info->packages[i] = (ManifestPackage *) calloc(1, sizeof(ManifestPackage)); + if (info->packages[i] == NULL) { + perror("Failed to allocate package record"); + fprintf(SYSERROR); + dep_free(&deps); + fstree_free(fsdata); + free(info); + return NULL; + } // Copy dependencies if (deps->records) { @@ -91,6 +107,7 @@ Manifest *manifest_from(const char *package_dir) { split_free(parts); } + fstree_free(fsdata); return info; } @@ -102,7 +119,6 @@ void manifest_free(Manifest *info) { for (size_t i = 0; i < info->records; i++) { manifest_package_free(info->packages[i]); } - free(info->packages); free(info); } @@ -199,6 +215,7 @@ int manifest_write(Manifest *info, const char *outfile) { checksum_sha256 ? checksum_sha256 : SPM_MANIFEST_NODATA); fprintf(fp, "%s\n", dptr); free(reqs); + free(archive); if (checksum_sha256 != NULL) free(checksum_sha256); } @@ -364,10 +381,10 @@ Manifest *manifest_read(char *file_or_url) { // Record manifest's origin memset(info->origin, '\0', PACKAGE_MEMBER_ORIGIN_SIZE); if (remote_manifest != NULL) { - strncpy(info->origin, remote_manifest, PACKAGE_MEMBER_ORIGIN_SIZE); + strcpy(info->origin, remote_manifest); } else { - strncpy(info->origin, path, PACKAGE_MEMBER_ORIGIN_SIZE); + strcpy(info->origin, path); } free(remote_manifest); @@ -47,14 +47,17 @@ Mime *file_mimetype(const char *filename) { Process *proc = file_command(filename); if (proc->returncode != 0) { + shell_free(proc); return NULL; } output = split(proc->output, ":"); if (!output || output[1] == NULL) { + shell_free(proc); return NULL; } parts = split(output[1], ";"); if (!parts || !parts[0] || !parts[1]) { + shell_free(proc); return NULL; } @@ -65,7 +68,7 @@ Mime *file_mimetype(const char *filename) { charset = lstrip(charset); charset[strlen(charset) - 1] = '\0'; - char *origin = strdup(realpath(filename, NULL)); + char *origin = realpath(filename, NULL); type = (Mime *)calloc(1, sizeof(Mime)); type->origin = origin; @@ -74,6 +77,7 @@ Mime *file_mimetype(const char *filename) { split_free(output); split_free(parts); + shell_free(proc); return type; } diff --git a/src/rpath.c b/src/rpath.c index 04d810e..108e3f2 100644 --- a/src/rpath.c +++ b/src/rpath.c @@ -210,7 +210,7 @@ char *rpath_autodetect(const char *filename) { // Change directory to the requested root chdir(start); - char *visit = calloc(PATH_MAX, sizeof(char)); // Current directory + char *visit = NULL; // Current directory char tmp[PATH_MAX]; // Current directory with lib directory appended char relative[PATH_MAX]; // Generated relative path to lib directory char sep[2]; // Holds the platform's directory separator @@ -79,7 +79,9 @@ int main(int argc, char *argv[], char *arge[]) { else if (strcmp(arg, "--cmd") == 0) { int c = argc - i; char **a = &argv[i]; - exit(internal_cmd(c, a)); + int retval = internal_cmd(c, a); + free_global_config(); + exit(retval); } else if (strcmp(arg, "-B") == 0 || strcmp(arg, "--build") == 0) { int c = argc - i; @@ -166,7 +168,8 @@ int main(int argc, char *argv[], char *arge[]) { runtime_set(rt, "PATH", "$SPM_BIN:$PATH"); runtime_set(rt, "MANPATH", "$SPM_MAN:$MANPATH"); - if (exists(join((char *[]) {spm_binpath, "gcc"}, DIRSEPS)) == 0) { + char *spm_ccpath = join((char *[]) {spm_binpath, "gcc"}, DIRSEPS); + if (exists(spm_ccpath) == 0) { runtime_set(rt, "CC", "$SPM_BIN/gcc"); } @@ -179,6 +182,7 @@ int main(int argc, char *argv[], char *arge[]) { free(spm_libpath); free(spm_datapath); free(spm_manpath); + free(spm_ccpath); if (RUNTIME_INSTALL) { Dependencies *deps = NULL; @@ -277,8 +281,8 @@ int main(int argc, char *argv[], char *arge[]) { runtime_free(rt); exit(errno); } - manifest_free(manifest); } + manifest_free(manifest); dep_free(&deps); } diff --git a/src/strings.c b/src/strings.c index 29d5b65..017af26 100644 --- a/src/strings.c +++ b/src/strings.c @@ -236,7 +236,7 @@ char *join(char **arr, const char *separator) { total_bytes += strlen(arr[i]); records++; } - total_bytes += records * (strlen(separator) + 1); + total_bytes += (records * strlen(separator)) + 1; result = (char *)calloc(total_bytes, sizeof(char)); for (int i = 0; i < records; i++) { |