From c429ae2f0790c3486e7e420f4f2883c6a530ccf0 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Thu, 6 May 2021 00:34:05 -0400 Subject: No sep ending (#2) * Be extra sure not to inject a separator at the end of the output string --- README.md | 3 +++ lib/cleanpath.c | 2 +- tests/test_no_sep_ending.c | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/test_no_sep_ending.c diff --git a/README.md b/README.md index 38ebcbb..3ee681a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ # cleanpath +[![CMake](https://github.com/jhunkeler/cleanpath/actions/workflows/cmake.yml/badge.svg)](https://github.com/jhunkeler/cleanpath/actions/workflows/cmake.yml) + `cleanpath` is a utility that filters unwanted elements from an environment variable. + # Installation ```shell diff --git a/lib/cleanpath.c b/lib/cleanpath.c index f94c66d..c7f8bc9 100644 --- a/lib/cleanpath.c +++ b/lib/cleanpath.c @@ -102,7 +102,7 @@ char *cleanpath_read(struct CleanPath *path) { strcat(result, path->part[i]); // Do not append path separator on final element - if (path->part[i + 1] != NULL) { + if (path->part[i + 1] != NULL && *path->part[i + 1] != '\0') { strcat(result, path->sep); } } diff --git a/tests/test_no_sep_ending.c b/tests/test_no_sep_ending.c new file mode 100644 index 0000000..6b89c2b --- /dev/null +++ b/tests/test_no_sep_ending.c @@ -0,0 +1,19 @@ +#include "cleanpath.h" +#include "framework.h" + +int main() { + const char *input = "/usr/bin:/usr/sbin:/sbin:/bin:"; + struct CleanPath *path; + char *result; + size_t len; + + result = NULL; + path = cleanpath_init(input, TEST_SEP); + cleanpath_filter(path, CLEANPATH_FILTER_REGEX, "/bin"); + result = cleanpath_read(path); + cleanpath_free(path); + + len = strlen(result); + puts(result); + myassert(len && *(result + (len - 1)) != TEST_SEP[0], "Result ends with a separator:\n'%s'", result); +} \ No newline at end of file -- cgit