summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2016-07-19 12:33:44 +0200
committerBram Moolenaar <Bram@vim.org>2016-07-19 12:33:44 +0200
commitb9644433d2728e99fab874e5e33147ad95d23a31 (patch)
treee54ad05240a3009af2bf30c811ab5314ef4268d0
parent16ec3c9be3fcdc38530bddb12978bc5a7b98c0f6 (diff)
downloadvim-git-b9644433d2728e99fab874e5e33147ad95d23a31.tar.gz
patch 7.4.2068v7.4.2068
Problem: Not all arguments of trunc_string() are tested. Memory access error when running the message tests. Solution: Add another test case. (Yegappan Lakshmanan) Make it easy to run unittests with valgrind. Fix the access error.
-rw-r--r--src/Makefile16
-rw-r--r--src/message.c4
-rw-r--r--src/message_test.c44
-rw-r--r--src/version.c2
4 files changed, 51 insertions, 15 deletions
diff --git a/src/Makefile b/src/Makefile
index c888661eb..619a21484 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -602,6 +602,10 @@ AUTOCONF = autoconf
# PURIFY - remove the # to use the "purify" program (hoi Nia++!)
#PURIFY = purify
+# VALGRIND - remove the # to use valgrind for memory leaks and access errors.
+# Used for the unittest targets.
+# VALGRIND = valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind.$@
+
# NBDEBUG - debugging the netbeans interface.
#EXTRA_DEFS = -DNBDEBUG
@@ -1567,6 +1571,7 @@ MESSAGE_TEST_TARGET = message_test$(EXEEXT)
UNITTEST_SRC = $(JSON_TEST_SRC) $(MEMFILE_TEST_SRC) $(MESSAGE_TEST_SRC)
UNITTEST_TARGETS = $(JSON_TEST_TARGET) $(MEMFILE_TEST_TARGET) $(MESSAGE_TEST_TARGET)
+RUN_UNITTESTS = run_json_test run_memfile_test run_message_test
# All sources, also the ones that are not configured
ALL_SRC = $(BASIC_SRC) $(ALL_GUI_SRC) $(UNITTEST_SRC) $(EXTRA_SRC)
@@ -1987,19 +1992,16 @@ unittesttargets:
$(MAKE) -f Makefile $(UNITTEST_TARGETS)
# Execute the unittests one by one.
-unittest unittests: $(UNITTEST_TARGETS)
- @for t in $(UNITTEST_TARGETS); do \
- ./$$t || exit 1; echo $$t passed; \
- done
+unittest unittests: $(RUN_UNITTESTS)
run_json_test: $(JSON_TEST_TARGET)
- ./$(JSON_TEST_TARGET)
+ $(VALGRIND) ./$(JSON_TEST_TARGET) || exit 1; echo $* passed;
run_memfile_test: $(MEMFILE_TEST_TARGET)
- ./$(MEMFILE_TEST_TARGET)
+ $(VALGRIND) ./$(MEMFILE_TEST_TARGET) || exit 1; echo $* passed;
run_message_test: $(MESSAGE_TEST_TARGET)
- ./$(MESSAGE_TEST_TARGET)
+ $(VALGRIND) ./$(MESSAGE_TEST_TARGET) || exit 1; echo $* passed;
# Run individual OLD style test, assuming that Vim was already compiled.
test1 \
diff --git a/src/message.c b/src/message.c
index fe72e43a6..7aec2d613 100644
--- a/src/message.c
+++ b/src/message.c
@@ -298,9 +298,9 @@ trunc_string(
{
do
half = half - (*mb_head_off)(s, s + half - 1) - 1;
- while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
+ while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
n = ptr2cells(s + half);
- if (len + n > room)
+ if (len + n > room || half == 0)
break;
len += n;
i = half;
diff --git a/src/message_test.c b/src/message_test.c
index 41b887579..ec231e8ef 100644
--- a/src/message_test.c
+++ b/src/message_test.c
@@ -28,37 +28,69 @@
static void
test_trunc_string(void)
{
- char_u buf[40];
+ char_u *buf; /*allocated every time to find uninit errors */
+ char_u *s;
/* in place */
+ buf = alloc(40);
STRCPY(buf, "text");
trunc_string(buf, buf, 20, 40);
assert(STRCMP(buf, "text") == 0);
+ vim_free(buf);
+ buf = alloc(40);
STRCPY(buf, "a short text");
trunc_string(buf, buf, 20, 40);
assert(STRCMP(buf, "a short text") == 0);
+ vim_free(buf);
+ buf = alloc(40);
STRCPY(buf, "a text tha just fits");
trunc_string(buf, buf, 20, 40);
assert(STRCMP(buf, "a text tha just fits") == 0);
+ vim_free(buf);
+ buf = alloc(40);
STRCPY(buf, "a text that nott fits");
trunc_string(buf, buf, 20, 40);
assert(STRCMP(buf, "a text t...nott fits") == 0);
+ vim_free(buf);
/* copy from string to buf */
- trunc_string((char_u *)"text", buf, 20, 40);
+ buf = alloc(40);
+ s = vim_strsave((char_u *)"text");
+ trunc_string(s, buf, 20, 40);
assert(STRCMP(buf, "text") == 0);
-
- trunc_string((char_u *)"a short text", buf, 20, 40);
+ vim_free(buf);
+ vim_free(s);
+
+ buf = alloc(40);
+ s = vim_strsave((char_u *)"a text that fits");
+ trunc_string(s, buf, 34, 40);
+ assert(STRCMP(buf, "a text that fits") == 0);
+ vim_free(buf);
+ vim_free(s);
+
+ buf = alloc(40);
+ s = vim_strsave((char_u *)"a short text");
+ trunc_string(s, buf, 20, 40);
assert(STRCMP(buf, "a short text") == 0);
+ vim_free(buf);
+ vim_free(s);
- trunc_string((char_u *)"a text tha just fits", buf, 20, 40);
+ buf = alloc(40);
+ s = vim_strsave((char_u *)"a text tha just fits");
+ trunc_string(s, buf, 20, 40);
assert(STRCMP(buf, "a text tha just fits") == 0);
+ vim_free(buf);
+ vim_free(s);
- trunc_string((char_u *)"a text that nott fits", buf, 20, 40);
+ buf = alloc(40);
+ s = vim_strsave((char_u *)"a text that nott fits");
+ trunc_string(s, buf, 20, 40);
assert(STRCMP(buf, "a text t...nott fits") == 0);
+ vim_free(buf);
+ vim_free(s);
}
int
diff --git a/src/version.c b/src/version.c
index 55a99be95..6f80d19c5 100644
--- a/src/version.c
+++ b/src/version.c
@@ -759,6 +759,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 2068,
+/**/
2067,
/**/
2066,