From 0cba91923be9326224f8434e4aae7691f7b6dad1 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 28 Jun 2015 14:05:05 +0200 Subject: [PATCH] src/daemon/common.c: Fix off-by-one error in escape_slashes(). This also adds a unit test for this issue. --- src/daemon/common.c | 2 +- src/daemon/common_test.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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); -- 2.30.2