From: Florian Forster Date: Sun, 28 Jun 2015 12:05:05 +0000 (+0200) Subject: src/daemon/common.c: Fix off-by-one error in escape_slashes(). X-Git-Tag: collectd-5.6.0~667 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=0cba91923be9326224f8434e4aae7691f7b6dad1;p=collectd.git src/daemon/common.c: Fix off-by-one error in escape_slashes(). This also adds a unit test for this issue. --- diff --git a/src/daemon/common.c b/src/daemon/common.c index dc6e4e1e..9e0648ca 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -514,7 +514,7 @@ int escape_slashes (char *buffer, size_t buffer_size) buffer_len--; } - for (i = 0; i < buffer_len - 1; i++) + for (i = 0; i < buffer_len; i++) { if (buffer[i] == '/') buffer[i] = '_'; diff --git a/src/daemon/common_test.c b/src/daemon/common_test.c index f75621a6..ff33c9eb 100644 --- a/src/daemon/common_test.c +++ b/src/daemon/common_test.c @@ -189,6 +189,30 @@ DEF_TEST(strjoin) return (0); } +DEF_TEST(escape_slashes) +{ + struct { + char *str; + char *want; + } cases[] = { + {"foo/bar/baz", "foo_bar_baz"}, + {"/like/a/path", "like_a_path"}, + {"trailing/slash/", "trailing_slash_"}, + {"foo//bar", "foo__bar"}, + }; + size_t i; + + for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) { + char buffer[32]; + + strncpy (buffer, cases[i].str, sizeof (buffer)); + OK(escape_slashes (buffer, sizeof (buffer)) == 0); + STREQ(cases[i].want, buffer); + } + + return 0; +} + DEF_TEST(strunescape) { char buffer[16]; @@ -294,6 +318,7 @@ int main (void) RUN_TEST(sstrdup); RUN_TEST(strsplit); RUN_TEST(strjoin); + RUN_TEST(escape_slashes); RUN_TEST(strunescape); RUN_TEST(parse_values);