diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-06-09 08:12:02 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-06-09 08:12:02 -0400 |
commit | e4f615faf36e0163a5b550df9916a1e6198770b9 (patch) | |
tree | 13c54155e33b228a5a3661a0aea859bfd2de4616 /src/utils.c | |
parent | 3357f87a5efe478be2433787c29de640dc21b33d (diff) | |
download | stasis-e4f615faf36e0163a5b550df9916a1e6198770b9.tar.gz |
Address shortcomings and bugs flushed out by unit tests
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/src/utils.c b/src/utils.c index 7cc3e6e..7b77020 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,9 +6,15 @@ const ssize_t dirstack_max = sizeof(dirstack) / sizeof(dirstack[0]); ssize_t dirstack_len = 0; int pushd(const char *path) { + if (access(path, F_OK)) { + // the requested path doesn't exist. + return -1; + } if (dirstack_len + 1 > dirstack_max) { + // the stack is full return -1; } + dirstack[dirstack_len] = realpath(".", NULL); dirstack_len++; return chdir(path); @@ -151,7 +157,7 @@ char *path_dirname(char *path) { if (!pos) { return "."; } - *path = '\0'; + *pos = '\0'; return path; } @@ -702,29 +708,25 @@ char *collapse_whitespace(char **s) { * @return 0 on success, -1 on error */ int redact_sensitive(const char **to_redact, char *src, char *dest, size_t maxlen) { - char **parts = split(src, " ", 0); - if (!parts) { - fprintf(stderr, "Unable to split source string\n"); + const char *redacted = "***REDACTED***"; + + char *tmp = calloc(strlen(redacted) + strlen(src) + 1, sizeof(*tmp)); + if (!tmp) { return -1; } + strcpy(tmp, src); for (size_t i = 0; to_redact[i] != NULL; i++) { - for (size_t p = 0; parts[p] != NULL; p++) { - if (strstr(parts[p], to_redact[i])) { - replace_text(parts[p], to_redact[i], "***REDACTED***", REPLACE_TRUNCATE_AFTER_MATCH); - } + if (strstr(tmp, to_redact[i])) { + replace_text(tmp, to_redact[i], redacted, 0); + break; } } - char *dest_tmp = join(parts, " "); - if (!dest_tmp) { - fprintf(stderr, "Unable to join message array\n"); - return -1; - } - strncpy(dest, dest_tmp, maxlen); + memset(dest, 0, maxlen); + strncpy(dest, tmp, maxlen - 1); + guard_free(tmp); - GENERIC_ARRAY_FREE(parts); - guard_free(dest_tmp); return 0; } |