From 5f0c38f0f5a8abf0af52ffdfaeb70e58933d63f7 Mon Sep 17 00:00:00 2001 From: Andreas Waidler Date: Fri, 29 Apr 2011 21:54:20 +0200 Subject: [PATCH] item: checking against overflows in title. --- src/fs.c | 9 ++++++++- tests_dev/tests.c | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/fs.c b/src/fs.c index fd5cf4e..0e009c8 100644 --- a/src/fs.c +++ b/src/fs.c @@ -70,7 +70,14 @@ void set_item_desc(const char* desc) void set_item_title(const char* title) { - strncpy(item_title, title, TEXT_BUFFER_SIZE); + strncpy(item_title, title, TEXT_BUFFER_SIZE - 1); + + if (strlen(title) >= TEXT_BUFFER_SIZE) { + item_title[TEXT_BUFFER_SIZE - 4] = '.'; + item_title[TEXT_BUFFER_SIZE - 3] = '.'; + item_title[TEXT_BUFFER_SIZE - 2] = '.'; + } + sanitize(item_title); } diff --git a/tests_dev/tests.c b/tests_dev/tests.c index 03e8a01..a003304 100644 --- a/tests_dev/tests.c +++ b/tests_dev/tests.c @@ -17,6 +17,7 @@ #include #include +#include "../src/config.h" #include "../src/text.h" #include "../src/layer.h" #include "../src/fs.h" @@ -40,7 +41,9 @@ int test_stack_set_next(); int test_set_item_title(); int test_get_item_title(); int test_item_sanitize_title(); -int test_item_overflow(); +int test_item_title_fill_minus_one(); +int test_item_title_fill(); +int test_item_title_fill_plus_one(); unsigned int failures = 0; @@ -61,7 +64,9 @@ int main() TEST(test_set_item_title); TEST(test_get_item_title); TEST(test_item_sanitize_title); - TEST(test_item_overflow); + TEST(test_item_title_fill_minus_one); + TEST(test_item_title_fill); + TEST(test_item_title_fill_plus_one); return failures; } @@ -180,21 +185,45 @@ int test_item_sanitize_title() return strcmp("..\\..\\foo", get_item_title()) == 0; } -int test_item_overflow() +int test_item_title_fill_minus_one() { - #define MAXLEN 8192 + char exp[TEXT_BUFFER_SIZE] = { 0 }; + char buf[TEXT_BUFFER_SIZE] = { 0 }; - char buf[BUFSIZE]; - char exp[MAXLEN + 1]; + memset(exp, ' ', TEXT_BUFFER_SIZE - 2); + memset(buf, ' ', TEXT_BUFFER_SIZE - 2); - memset(buf, ' ', BUFSIZE); - memset(exp, ' ', MAXLEN + 1); - /* FIXME */ - /* exp[MAXLEN - 3] = '.'; */ - /* exp[MAXLEN - 2] = '.'; */ - /* exp[MAXLEN - 1] = '.'; */ - exp[MAXLEN] = 0; + set_item_title(buf); + + return strcmp(exp, get_item_title()) == 0; +} + +int test_item_title_fill() +{ + char exp[TEXT_BUFFER_SIZE] = { 0 }; + char buf[TEXT_BUFFER_SIZE] = { 0 }; + + memset(exp, ' ', TEXT_BUFFER_SIZE - 1); + memset(buf, ' ', TEXT_BUFFER_SIZE - 1); set_item_title(buf); + + return strcmp(exp, get_item_title()) == 0; +} + +int test_item_title_fill_plus_one() +{ + char exp[TEXT_BUFFER_SIZE] = { 0 }; + char buf[TEXT_BUFFER_SIZE + 1] = { 0 }; + + memset(exp, ' ', TEXT_BUFFER_SIZE - 1); + memset(buf, ' ', TEXT_BUFFER_SIZE); + + exp[TEXT_BUFFER_SIZE - 4] = '.'; + exp[TEXT_BUFFER_SIZE - 3] = '.'; + exp[TEXT_BUFFER_SIZE - 2] = '.'; + + set_item_title(buf); + return strcmp(exp, get_item_title()) == 0; } -- 2.11.4.GIT