diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-10-04 15:48:19 -0400 | 
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-10-04 15:48:19 -0400 | 
| commit | 19461d9a2b98aa706fd5e829a70627f11742a5d5 (patch) | |
| tree | 8cf70c6afd5165499f1a21ed1e4b2f257b2e95be /src | |
| parent | 6e72330a3cc8d571b14f2234ed8fc2778b5e9b86 (diff) | |
| download | stasis-19461d9a2b98aa706fd5e829a70627f11742a5d5.tar.gz | |
Clean up get_wheel_info
* Add error handling
* Fix resource leaks that occurred on error
* Add wheel_free() function
Diffstat (limited to 'src')
| -rw-r--r-- | src/wheel.c | 54 | 
1 files changed, 51 insertions, 3 deletions
| diff --git a/src/wheel.c b/src/wheel.c index b96df57..4692d0a 100644 --- a/src/wheel.c +++ b/src/wheel.c @@ -1,6 +1,6 @@  #include "wheel.h" -struct Wheel *get_wheel_file(const char *basepath, const char *name, char *to_match[], unsigned match_mode) { +struct Wheel *get_wheel_info(const char *basepath, const char *name, char *to_match[], unsigned match_mode) {      DIR *dp;      struct dirent *rec;      struct Wheel *result = NULL; @@ -47,13 +47,41 @@ struct Wheel *get_wheel_file(const char *basepath, const char *name, char *to_ma          }          result = calloc(1, sizeof(*result)); +        if (!result) { +            SYSERROR("Unable to allocate %zu bytes for wheel struct", sizeof(*result)); +            closedir(dp); +            return NULL; +        } +          result->path_name = realpath(package_path, NULL); +        if (!result->path_name) { +            SYSERROR("Unable to resolve absolute path to %s: %s", filename, strerror(errno)); +            wheel_free(&result); +            closedir(dp); +            return NULL; +        }          result->file_name = strdup(rec->d_name); +        if (!result->file_name) { +            SYSERROR("Unable to allocate bytes for %s: %s", rec->d_name, strerror(errno)); +            wheel_free(&result); +            closedir(dp); +            return NULL; +        }          size_t parts_total;          char **parts = split(filename, "-", 0); +        if (!parts) { +            // This shouldn't happen unless a wheel file is present in the +            // directory with a malformed file name, or we've managed to +            // exhaust the system's memory +            SYSERROR("%s has no '-' separators! (Delete this file and try again)", filename); +            wheel_free(&result); +            closedir(dp); +            return NULL; +        } +          for (parts_total = 0; parts[parts_total] != NULL; parts_total++); -        if (parts_total < 6) { +        if (parts_total == 5) {              // no build tag              result->distribution = strdup(parts[0]);              result->version = strdup(parts[1]); @@ -61,7 +89,7 @@ struct Wheel *get_wheel_file(const char *basepath, const char *name, char *to_ma              result->python_tag = strdup(parts[2]);              result->abi_tag = strdup(parts[3]);              result->platform_tag = strdup(parts[4]); -        } else { +        } else if (parts_total == 6) {              // has build tag              result->distribution = strdup(parts[0]);              result->version = strdup(parts[1]); @@ -69,6 +97,13 @@ struct Wheel *get_wheel_file(const char *basepath, const char *name, char *to_ma              result->python_tag = strdup(parts[3]);              result->abi_tag = strdup(parts[4]);              result->platform_tag = strdup(parts[5]); +        } else { +            SYSERROR("Unknown wheel name format: %s. Expected 5 or 6 strings " +                     "separated by '-', but got %zu instead", filename, parts_total); +            GENERIC_ARRAY_FREE(parts); +            wheel_free(&result); +            closedir(dp); +            return NULL;          }          GENERIC_ARRAY_FREE(parts);          break; @@ -76,3 +111,16 @@ struct Wheel *get_wheel_file(const char *basepath, const char *name, char *to_ma      closedir(dp);      return result;  } + +void wheel_free(struct Wheel **wheel) { +    struct Wheel *w = (*wheel); +    guard_free(w->path_name); +    guard_free(w->file_name); +    guard_free(w->distribution); +    guard_free(w->version); +    guard_free(w->build_tag); +    guard_free(w->python_tag); +    guard_free(w->abi_tag); +    guard_free(w->python_tag); +    guard_free(w); +} | 
