diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2021-05-06 17:01:31 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2021-05-06 17:01:31 -0400 |
commit | be932c2f558b535ba2c2d10bf6d0df68040a1821 (patch) | |
tree | 480225221cf067ddd0f75465c0422c66809a2e03 /lib/cleanpath.c | |
parent | 41ba28892a04ab8a35a2751b4b7801414706f284 (diff) | |
download | cleanpath-be932c2f558b535ba2c2d10bf6d0df68040a1821.tar.gz |
Guard memory errors
Diffstat (limited to 'lib/cleanpath.c')
-rw-r--r-- | lib/cleanpath.c | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/lib/cleanpath.c b/lib/cleanpath.c index b849199..9c46c38 100644 --- a/lib/cleanpath.c +++ b/lib/cleanpath.c @@ -15,15 +15,27 @@ struct CleanPath *cleanpath_init(const char *path, const char *sep) { struct CleanPath *result; char *elem; size_t i; + result = NULL; if (!path || !sep) { - return NULL; + goto cleanpath_init__failure; } result = calloc(1, sizeof(*result)); + if (result == NULL) { + goto cleanpath_init__failure; + } + result->data = strdup(path); + if (result->data == NULL) { + return NULL; + } + result->data_len = strlen(result->data) + 2; // + 2 to handle an empty PATH result->sep = strdup(sep); + if (result->sep == NULL) { + goto cleanpath_init__failure; + } // Split string by separator elem = strtok(result->data, sep); @@ -33,6 +45,7 @@ struct CleanPath *cleanpath_init(const char *path, const char *sep) { } result->part_nelem = i; + cleanpath_init__failure: return result; } @@ -82,6 +95,10 @@ char *cleanpath_read(struct CleanPath *path) { char *result; result = calloc(path->data_len, sizeof(char)); + if (result == NULL) { + goto cleanpath_read__failure; + } + for (size_t i = 0; path->part[i] != NULL; i++) { // Ignore filtered paths if (*path->part[i] == '\0') { @@ -95,6 +112,7 @@ char *cleanpath_read(struct CleanPath *path) { } } + cleanpath_read__failure: return result; } |