diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-29 01:35:09 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-29 01:35:09 -0500 |
commit | 4595ada2f69b42670c85a63c7d2344af63f2afe7 (patch) | |
tree | 0d528d8177aceefcf74fb7306fc0fc7cc3c41ece /src | |
parent | 8ae4f8f5985445b1ce3547975f407847c0fee0f7 (diff) | |
download | spmc-4595ada2f69b42670c85a63c7d2344af63f2afe7.tar.gz |
Minor fixes:
* size_t in place of int
* Moved some variables closer to their execution scope
* Add some error checks
Diffstat (limited to 'src')
-rw-r--r-- | src/checksum.c | 2 | ||||
-rw-r--r-- | src/compat.c | 2 | ||||
-rw-r--r-- | src/config.c | 30 | ||||
-rw-r--r-- | src/config_global.c | 1 | ||||
-rw-r--r-- | src/find.c | 5 | ||||
-rw-r--r-- | src/fs.c | 20 | ||||
-rw-r--r-- | src/install.c | 4 | ||||
-rw-r--r-- | src/internal_cmd.c | 8 | ||||
-rw-r--r-- | src/manifest.c | 20 | ||||
-rw-r--r-- | src/relocation.c | 13 | ||||
-rw-r--r-- | src/rpath.c | 16 | ||||
-rw-r--r-- | src/shell.c | 9 | ||||
-rw-r--r-- | src/spm.c | 46 | ||||
-rw-r--r-- | src/strings.c | 6 | ||||
-rw-r--r-- | src/version_spec.c | 25 |
15 files changed, 117 insertions, 90 deletions
diff --git a/src/checksum.c b/src/checksum.c index 4484a6d..d511298 100644 --- a/src/checksum.c +++ b/src/checksum.c @@ -23,6 +23,7 @@ char *md5sum(const char *filename) { } char *result = calloc((MD5_DIGEST_LENGTH * 2) + 1, sizeof(char)); if (!result) { + fclose(fp); perror("MD5 result"); return NULL; } @@ -59,6 +60,7 @@ char *sha256sum(const char *filename) { } char *result = calloc((SHA256_DIGEST_LENGTH * 2) + 1, sizeof(char)); if (!result) { + fclose(fp); perror("SHA256 result"); return NULL; } diff --git a/src/compat.c b/src/compat.c index 082a602..062c939 100644 --- a/src/compat.c +++ b/src/compat.c @@ -1,7 +1,7 @@ -#include <string.h> #include "config.h" #ifndef HAVE_STRSEP +#include <string.h> // credit: Dan Cross via https://unixpapa.com/incnote/string.html char *strsep(char **sp, char *sep) { diff --git a/src/config.c b/src/config.c index 7b62311..9766e9b 100644 --- a/src/config.c +++ b/src/config.c @@ -24,14 +24,27 @@ ConfigItem **config_read(const char *filename) { const char sep = '='; char *line = (char *)calloc(CONFIG_BUFFER_SIZE, sizeof(char)); + if (!line) { + perror("config line buffer"); + fprintf(SYSERROR); + return NULL; + } FILE *fp = fopen(filename, "r"); if (!fp) { // errno will be set, so die, and let the caller handle it + free(line); return NULL; } - int record_initial = 2; + size_t record_initial = 2; + size_t record = 0; ConfigItem **config = (ConfigItem **) calloc(record_initial, sizeof(ConfigItem *)); - int record = 0; + if (!config) { + perror("ConfigItem"); + fprintf(SYSERROR); + free(line); + fclose(fp); + return NULL; + } while (fgets(line, CONFIG_BUFFER_SIZE, fp) != NULL) { char *lptr = line; @@ -53,7 +66,7 @@ ConfigItem **config_read(const char *filename) { // Get a pointer to the key pair separator char *sep_pos = strchr(lptr, sep); if (!sep_pos) { - printf("invalid entry on line %d: missing '%c': '%s'\n", record, sep, lptr); + printf("invalid entry on line %zu: missing '%c': '%s'\n", record, sep, lptr); continue; } @@ -68,6 +81,12 @@ ConfigItem **config_read(const char *filename) { config[record]->key = (char *)calloc(key_length + 1, sizeof(char)); config[record]->value = (char *)calloc(value_length + 1, sizeof(char)); + if (!config[record] || !config[record]->key || !config[record]->value) { + perror("ConfigItem record"); + fprintf(SYSERROR); + return NULL; + } + // Shortcut our array at this point. Things get pretty ugly otherwise. char *key = config[record]->key; char *value = config[record]->value; @@ -117,6 +136,11 @@ ConfigItem **config_read(const char *filename) { record++; // Expand config by another record config = (ConfigItem **)reallocarray(config, record + record_initial + 1, sizeof(ConfigItem *)); + if (!config) { + perror("ConfigItem array"); + fprintf(SYSERROR); + return NULL; + } } free(line); return config; diff --git a/src/config_global.c b/src/config_global.c index 0dfa6db..4305942 100644 --- a/src/config_global.c +++ b/src/config_global.c @@ -98,7 +98,6 @@ char *get_package_manifest(void) { char *ucd = get_user_conf_dir(); sprintf(template, "%s%c%s", ucd, DIRSEP, "manifest.dat"); - char *wtf = template; if (access(template, F_OK) != 0) { fprintf(stderr, "Package manifest not found: %s\n", template); @@ -39,6 +39,7 @@ char *find_file(const char *root, const char *filename) { } if (!(rootpath = realpath(root, NULL))) { + free(path); return NULL; } @@ -103,11 +104,13 @@ int find_in_file(const char *filename, const char *pattern) { } long int file_len = get_file_size(filename); - if (file_len < 0) { + if (file_len == -1) { + fclose(fp); return -1; } char *buffer = (char *)calloc((size_t) file_len, sizeof(char)); if (!buffer) { + fclose(fp); return -1; } size_t pattern_len = strlen(pattern); @@ -85,12 +85,12 @@ int rmdirs(const char *_path) { FSTree *data = fstree(_path); if (data->files) { - for (int i = 0; data->files[i] != NULL; i++) { + for (size_t i = 0; data->files[i] != NULL; i++) { remove(data->files[i]); } } if (data->dirs) { - for (int i = data->dirs_length - 1; i != 0; i--) { + for (size_t i = data->dirs_length - 1; i != 0; i--) { remove(data->dirs[i]); } } @@ -177,7 +177,7 @@ char *expandpath(const char *_path) { } // A broken runtime environment means we can't do anything else here - if (!home) { + if (isempty(home)) { return NULL; } @@ -256,14 +256,14 @@ char *dirname(const char *_path) { */ char *basename(char *path) { char *result = NULL; - char *last = strrchr(path, DIRSEP); - if (!last) { - return NULL; - } + char *last = NULL; + if ((last = strrchr(path, DIRSEP)) == NULL) { + return result; + } // Perform a lookahead ensuring the string is valid beyond the last separator - if ((last + 1) != NULL) { - result = last + 1; + if (last++ != NULL) { + result = last; } return result; @@ -409,7 +409,7 @@ char *human_readable_size(uint64_t n) { memset(r, '\0', sizeof(r)); for (i = 0; i < sizeof(unit); i++) { - if (labs(result) < 1024) { + if (fabs(result) < 1024) { break; } result /= 1024.0; diff --git a/src/install.c b/src/install.c index 8f56363..1fb4513 100644 --- a/src/install.c +++ b/src/install.c @@ -77,7 +77,7 @@ int install(const char *destroot, const char *_package) { chdir(tmpdir); { // Rewrite binary prefixes - RelocationEntry **b_record = prefixes_read(SPM_META_PREFIX_BIN); + b_record= prefixes_read(SPM_META_PREFIX_BIN); if (b_record) { for (int i = 0; b_record[i] != NULL; i++) { relocate(b_record[i]->path, b_record[i]->prefix, destroot); @@ -85,7 +85,7 @@ int install(const char *destroot, const char *_package) { } // Rewrite text prefixes - RelocationEntry **t_record = prefixes_read(SPM_META_PREFIX_TEXT); + t_record = prefixes_read(SPM_META_PREFIX_TEXT); if (t_record) { for (int i = 0; t_record[i] != NULL; i++) { file_replace_text(t_record[i]->path, t_record[i]->prefix, destroot); diff --git a/src/internal_cmd.c b/src/internal_cmd.c index c28bcbb..7869c53 100644 --- a/src/internal_cmd.c +++ b/src/internal_cmd.c @@ -58,9 +58,9 @@ int internal_cmd(int argc, char **argv) { char *outfile = argv[2]; char *tree = argv[3]; - int prefix_start = 4; - int prefixes; - for (int i = prefix_start; i < argc; i++) { + size_t prefix_start = 4; + size_t prefixes = 0; + for (size_t i = prefix_start; i < argc; i++) { prefixes = i; } @@ -76,7 +76,7 @@ int internal_cmd(int argc, char **argv) { return -1; } if (!prefixes) { - fprintf(stderr, "error: missing prefix string(s) (%d, %d)\n", prefix_start, prefixes); + fprintf(stderr, "error: missing prefix string(s)\n"); mkprefix_usage(); return -1; } diff --git a/src/manifest.c b/src/manifest.c index 672d136..240ce8e 100644 --- a/src/manifest.c +++ b/src/manifest.c @@ -18,7 +18,7 @@ Manifest *manifest_from(const char *package_dir) { info->packages = (ManifestPackage **) calloc(info->records + 1, sizeof(ManifestPackage *)); printf("Initializing package manifest:\n"); - for (int i = 0; i < fsdata->files_length; i++) { + for (size_t i = 0; i < fsdata->files_length; i++) { float percent = (((float)i + 1) / fsdata->files_length) * 100; printf("[%3.0f%%] %s\n", percent, basename(fsdata->files[i])); Dependencies *deps = NULL; @@ -46,7 +46,7 @@ Manifest *manifest_from(const char *package_dir) { // hyphen below int delims = num_chars(fsdata->files[i], '-'); if (delims > PACKAGE_MIN_DELIM) { - for (int t = strlen(fsdata->files[i]); t != 0; t--) { + for (size_t t = strlen(fsdata->files[i]); t != 0; t--) { if (fsdata->files[i][t] == '-') { delims--; if (delims == 0) { @@ -64,7 +64,7 @@ Manifest *manifest_from(const char *package_dir) { replace_text(fsdata->files[i], "*", "-"); // Populate `ManifestPackage` record - info->packages[i]->size = get_file_size(fsdata->files[i]); + info->packages[i]->size = (size_t) get_file_size(fsdata->files[i]); strncpy(info->packages[i]->archive, basename(fsdata->files[i]), PACKAGE_MEMBER_SIZE); strncpy(info->packages[i]->name, basename(parts[0]), PACKAGE_MEMBER_SIZE); strncpy(info->packages[i]->version, parts[1], PACKAGE_MEMBER_SIZE); @@ -111,11 +111,11 @@ int manifest_write(Manifest *info) { if (SPM_GLOBAL.verbose) { for (int i = 0; i < info->records; i++) { printf("%-20s: %s\n" - "%-20s: %lu\n" + "%-20s: %zu\n" "%-20s: %s\n" "%-20s: %s\n" "%-20s: %s\n" - "%-20s: %d\n", + "%-20s: %zu\n", "archive", info->packages[i]->archive, "size", info->packages[i]->size, "name", info->packages[i]->name, @@ -140,11 +140,11 @@ int manifest_write(Manifest *info) { printf("[%3.0f%%] %s\n", percent, info->packages[i]->archive); reqs = join(info->packages[i]->requirements, ","); sprintf(dptr, "%s|" // archive - "%lu|" // size + "%zu|" // size "%s|" // name "%s|" // version "%s|" // revision - "%d|" // requirements_records + "%zu|" // requirements_records "%s" // requirements , info->packages[i]->archive, info->packages[i]->size, @@ -174,7 +174,7 @@ Manifest *manifest_read(void) { perror(filename); return NULL; } - int total_records = 0; + size_t total_records = 0; char data[BUFSIZ]; char *dptr = data; memset(dptr, '\0', BUFSIZ); @@ -188,7 +188,7 @@ Manifest *manifest_read(void) { info->packages = (ManifestPackage **)calloc(total_records + 1, sizeof(ManifestPackage *)); // Begin parsing the manifest - int i = 0; + size_t i = 0; while (fgets(dptr, BUFSIZ, fp) != NULL) { dptr = strip(dptr); char *garbage; @@ -200,7 +200,7 @@ Manifest *manifest_read(void) { strncpy(info->packages[i]->name, parts[2], strlen(parts[2])); strncpy(info->packages[i]->version, parts[3], strlen(parts[3])); strncpy(info->packages[i]->revision, parts[4], strlen(parts[4])); - info->packages[i]->requirements_records = atoi(parts[5]); + info->packages[i]->requirements_records = (size_t) atoi(parts[5]); info->packages[i]->requirements = NULL; if (strncmp(parts[6], "*", 2) != 0) { diff --git a/src/relocation.c b/src/relocation.c index ad65f11..a1c0056 100644 --- a/src/relocation.c +++ b/src/relocation.c @@ -59,6 +59,7 @@ int file_replace_text(char *filename, const char *spattern, const char *sreplace sprintf(tempfile, "%s.spmfrt", filename); FILE *tfp = NULL; if ((tfp = fopen(tempfile, "w+")) == NULL) { + fclose(fp); perror(tempfile); return -1; } @@ -124,9 +125,8 @@ void prefixes_free(RelocationEntry **entry) { * @return success=array of RelocationEntry, failure=NULL */ RelocationEntry **prefixes_read(const char *filename) { - size_t i = 0; - int record_count = 0; - int parity = 0; + size_t record_count = 0; + size_t parity = 0; FILE *fp = fopen(filename, "r"); if (!fp) { fprintf(SYSERROR); @@ -151,7 +151,7 @@ RelocationEntry **prefixes_read(const char *filename) { parity = record_count % 2; if (parity != 0) { - fprintf(stderr, "%s: records are not divisible by 2 (got: %d %% 2 = %d)\n", filename, record_count, parity); + fprintf(stderr, "%s: records are not divisible by 2 (got: %zu %% 2 = %zu)\n", filename, record_count, parity); return NULL; } record_count /= 2; @@ -169,8 +169,8 @@ RelocationEntry **prefixes_read(const char *filename) { int do_prefix = 0; int do_path = 0; + size_t i = 0; while (fgets(line, BUFSIZ, fp) != NULL) { - char *wtf = line; if (isempty(line)) { continue; } @@ -255,13 +255,14 @@ int prefixes_write(const char *output_file, int mode, char **prefix, const char FSTree *fsdata = fstree(tree); if (!fsdata) { + fclose(fp); fprintf(SYSERROR); return -1; } for (int i = 0; i < fsdata->files_length; i++) { for (int p = 0; prefix[p] != NULL; p++) { - int proceed = 0; if (find_in_file(fsdata->files[i], prefix[p]) == 0) { + int proceed = 0; if (mode == PREFIX_WRITE_BIN) { proceed = file_is_binary(fsdata->files[i]); } else if (mode == PREFIX_WRITE_TEXT) { diff --git a/src/rpath.c b/src/rpath.c index 99d3403..1d32ae7 100644 --- a/src/rpath.c +++ b/src/rpath.c @@ -43,9 +43,6 @@ int has_rpath(const char *_filename) { return -1; } - Process *proc_info = NULL; - char *rpath = NULL; - // sanitize input path strchrdel(filename, "&;|"); @@ -87,7 +84,6 @@ char *rpath_get(const char *_filename) { return NULL; } - Process *proc_info = NULL; char *rpath = NULL; // sanitize input path @@ -145,7 +141,7 @@ char *rpath_generate(const char *_filename) { * @return */ int rpath_set(const char *filename, char *_rpath) { - int returncode; + int returncode = 0; char *rpath_new = rpath_generate(filename); if (!rpath_new) { @@ -164,14 +160,18 @@ int rpath_set(const char *filename, char *_rpath) { return 0; } - Process *pe = patchelf(filename, "--set-rpath"); - if (pe) { + char args[PATH_MAX]; + memset(args, '\0', PATH_MAX); + sprintf(args, "--set-rpath \"%s\"", _rpath); + + Process *pe = patchelf(filename, args); + if (pe != NULL) { returncode = pe->returncode; } shell_free(pe); free(rpath_new); free(rpath_orig); - return pe->returncode; + return returncode; } /** diff --git a/src/shell.c b/src/shell.c index 6ce3ad2..6b28e64 100644 --- a/src/shell.c +++ b/src/shell.c @@ -32,9 +32,6 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { va_list args; va_start(args, fmt); - size_t bytes_read = 0; - size_t i = 0; - size_t new_buf_size = 0; clockid_t clkid = CLOCK_REALTIME; FILE *proc = NULL; @@ -68,6 +65,8 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { proc = popen(cmd, "r"); if (!proc) { + free(outbuf); + va_end(args); return; } @@ -81,10 +80,12 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { } if (option & SHELL_OUTPUT) { + size_t bytes_read = 0; + size_t i = 0; + size_t new_buf_size; (*proc_info)->output = (char *)calloc(BUFSIZ, sizeof(char)); while ((*outbuf = fgetc(proc)) != EOF) { - if (i >= BUFSIZ) { new_buf_size = BUFSIZ + (i + bytes_read); (*proc_info)->output = (char *)realloc((*proc_info)->output, new_buf_size); @@ -12,18 +12,16 @@ int RUNTIME_SEARCH = 0; const int PACKAGE_MAX = 0xff; void usage(const char *program_name) { - printf( - "usage: %s [-hVvBIrLS]\n" - " -h, --help show this help message\n" - " -V, --version show version\n" - " -v, --verbose show more information\n" - " -B, --build build package(s)\n" - " -I, --install install package(s)\n" - " -r, --root installation prefix (requires --install)\n" - " -L, --list list available packages\n" - " -S, --search search for a package\n" - , program_name - ); + printf("usage: %s [-hVvBIrLS]\n" + " -h, --help show this help message\n" + " -V, --version show version\n" + " -v, --verbose show more information\n" + " -B, --build build package(s)\n" + " -I, --install install package(s)\n" + " -r, --root installation prefix (requires --install)\n" + " -L, --list list available packages\n" + " -S, --search search for a package\n" + , program_name); } int main(int argc, char *argv[]) { @@ -180,9 +178,13 @@ int main(int argc, char *argv[]) { } package = manifest_search(manifest, packages[i]); + if (!package) { + fprintf(stderr, "Package not found: %s\n", packages[i]); + continue; + } // If the package has dependencies listed, append them to `deps` now - if (package && package->requirements) { + if (package->requirements) { for (int p = 0; package->requirements[p] != NULL; p++) { dep_append(&deps, package->requirements[p]); } @@ -256,29 +258,29 @@ int main(int argc, char *argv[]) { memset(ver, '\0', sizeof(ver)); // Parse the argument string - int p = 0; + int c = 0; // Populate name - for (int j = 0; package_search_str[p] != '\0'; j++, p++) { - if (isrelational(package_search_str[p])) { + for (int j = 0; package_search_str[c] != '\0'; j++, c++) { + if (isrelational(package_search_str[c])) { break; } - name[j] = package_search_str[p]; + name[j] = package_search_str[c]; } if (RUNTIME_SEARCH) { // Populate op - for (int j = 0; package_search_str[p] != '\0'; j++, p++) { - if (!isrelational(package_search_str[p])) { + for (int j = 0; package_search_str[c] != '\0'; j++, c++) { + if (!isrelational(package_search_str[c])) { break; } - op[j] = package_search_str[p]; + op[j] = package_search_str[c]; } if (strlen(op)) { // Populate version - for (int j = 0; package_search_str[p] != '\0'; j++, p++) { - ver[j] = package_search_str[p]; + for (int j = 0; package_search_str[c] != '\0'; j++, c++) { + ver[j] = package_search_str[c]; } } else { // No operator, so find all versions instead diff --git a/src/strings.c b/src/strings.c index d6da7b6..782d66c 100644 --- a/src/strings.c +++ b/src/strings.c @@ -390,7 +390,7 @@ char **strdeldup(char **arr) { return NULL; } - int records; + size_t records; // Determine the length of the array for (records = 0; arr[records] != NULL; records++); @@ -453,7 +453,7 @@ char *strip(char *sptr) { *sptr = '\0'; return sptr; } - for (size_t i = len - 1; i >= 0; i--) { + for (size_t i = len - 1; ; i--) { if (isspace(sptr[i]) || isblank(sptr[i])) { sptr[i] = '\0'; } @@ -523,7 +523,7 @@ int isrelational(char ch) { */ void print_banner(const char *s, int len) { size_t s_len = strlen(s); - if (!s || !s_len) { + if (!s_len) { return; } for (int i = 0; i < (len / s_len); i++) { diff --git a/src/version_spec.c b/src/version_spec.c index 1bd31ae..57b86c4 100644 --- a/src/version_spec.c +++ b/src/version_spec.c @@ -253,7 +253,7 @@ int version_spec_from(const char *op) { } } return flags; -}; +} /** * @@ -278,24 +278,19 @@ static int _find_by_spec_compare(const void *a, const void *b) { * @return */ ManifestPackage **find_by_spec(Manifest *manifest, const char *name, const char *op, const char *version_str) { - int64_t version_a = 0; - int64_t version_b = 0; - int spec = VERSION_NOOP; - int record = 0; + size_t record = 0; ManifestPackage **list = (ManifestPackage **) calloc(manifest->records + 1, sizeof(ManifestPackage *)); + if (!list) { + perror("ManifestPackage array"); + fprintf(SYSERROR); + return NULL; + } for (int i = 0; i < manifest->records; i++) { if (strcmp(manifest->packages[i]->name, name) == 0) { - int is_equal = 0, - is_not_equal = 0, - is_less_than = 0, - is_greater_than = 0, - is_compat = 0; - - version_a = version_from(manifest->packages[i]->version); - version_b = version_from(version_str); - - spec = version_spec_from(op); + int64_t version_a = version_from(manifest->packages[i]->version); + int64_t version_b = version_from(version_str); + int spec = version_spec_from(op); int res = 0; if (spec & VERSION_GT && spec & VERSION_EQ) { |