diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-04-29 13:19:45 -0400 |
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-04-29 13:19:45 -0400 |
| commit | fca452625bf6af7efb1d1870a36a74df86051ec4 (patch) | |
| tree | 6010f8c1c675df655c2ceb617939d4b65f0bf6ed | |
| parent | b2d119008dc22778bddcaadfabc59b31d408216e (diff) | |
| download | stasis-fca452625bf6af7efb1d1870a36a74df86051ec4.tar.gz | |
split: free on error
| -rw-r--r-- | src/lib/core/str.c | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/src/lib/core/str.c b/src/lib/core/str.c index a08bd2b..d66a819 100644 --- a/src/lib/core/str.c +++ b/src/lib/core/str.c @@ -77,7 +77,6 @@ char** split(char *_sptr, const char* delim, size_t max) // Duplicate the input string and save a copy of the pointer to be freed later char *orig = _sptr; char *sptr = strdup(orig); - if (!sptr) { return NULL; } @@ -118,6 +117,8 @@ char** split(char *_sptr, const char* delim, size_t max) } result[i] = calloc(STASIS_BUFSIZ, sizeof(char)); if (!result[i]) { + guard_free(sptr); + guard_array_n_free(result, i); return NULL; } strncpy(result[i], token, STASIS_BUFSIZ - 1); @@ -129,6 +130,8 @@ char** split(char *_sptr, const char* delim, size_t max) // append the remaining string contents to array result[i] = calloc(STASIS_BUFSIZ, sizeof(char)); if (!result[i]) { + guard_free(sptr); + guard_array_n_free(result, i); return NULL; } strncpy(result[i], &orig[pos], STASIS_BUFSIZ - 1); @@ -210,7 +213,7 @@ char *join_ex(char *separator, ...) { result = calloc(size + 1, sizeof(char)); for (size_t i = 0; i < argc; i++) { // Append argument to string - strncat(result, argv[i], size - strlen(result)); // no -1 because +1 above + strncat(result, argv[i], size - (result ? strlen(result) - 1 : 0)); // no -1 because +1 above // Do not append a trailing separator when we reach the last argument if (i < (argc - 1)) { |
