From: Timo Sirainen Date: Mon, 20 Nov 2017 13:53:55 +0000 (+0200) Subject: lib: Improve test-path-util unit test to catch off-by-one memory allocation errors X-Git-Tag: 2.3.0.rc1~421 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=71acd1d082704ebabd034d3e73110b39fa5b7ef2;p=thirdparty%2Fdovecot%2Fcore.git lib: Improve test-path-util unit test to catch off-by-one memory allocation errors --- diff --git a/src/lib/test-path-util.c b/src/lib/test-path-util.c index 71fe6a9fca..f379e03c4d 100644 --- a/src/lib/test-path-util.c +++ b/src/lib/test-path-util.c @@ -206,6 +206,36 @@ static void test_link_alloc(void) tmpdir = o_tmpdir; } +static void test_link_alloc2(void) +{ + const char *o_tmpdir; + + /* try enough different sized base directory lengths so the code + hits the different reallocations and tests for off-by-one errors */ + string_t *basedir = t_str_new(256); + str_append(basedir, cwd); + str_append(basedir, "/"TEMP_DIRNAME); + str_append_c(basedir, '/'); + size_t base_len = str_len(basedir); + + o_tmpdir = tmpdir; + /* path_normalize() initially allocates 128 bytes, so we'll test paths + up to that length+1. */ + unsigned char buf[128+1]; + memset(buf, 'x', sizeof(buf)); + for (size_t i = 1; i <= sizeof(buf); i++) { + str_truncate(basedir, base_len); + str_append_n(basedir, buf, i); + tmpdir = str_c(basedir); + (void)mkdir(str_c(basedir), 0700); + + create_links(tmpdir); + test_link1(); + test_link_loop(); + } + tmpdir = o_tmpdir; +} + static void test_cleanup(void) { const char *error; @@ -244,6 +274,7 @@ void test_path_util(void) test_link_loop(); test_abspath_vs_normpath(); test_link_alloc(); + test_link_alloc2(); test_cleanup(); alarm(0); test_end();