vim-patch:7.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.

b9644433d2
This commit is contained in:
James McCoy 2017-03-06 12:14:39 -05:00
parent 7217360e34
commit 4e3a2784ec
3 changed files with 22 additions and 12 deletions

View File

@ -274,9 +274,9 @@ void trunc_string(char_u *s, char_u *buf, int room, int buflen)
for (;;) { for (;;) {
do { do {
half = half - (*mb_head_off)(s, s + half - 1) - 1; 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); n = ptr2cells(s + half);
if (len + n > room) { if (len + n > room || half == 0) {
break; break;
} }
len += n; len += n;

View File

@ -372,7 +372,7 @@ static int included_patches[] = {
2071, 2071,
// 2070 NA // 2070 NA
// 2069, // 2069,
// 2068, 2068,
2067, 2067,
2066, 2066,
2065, 2065,

View File

@ -4,22 +4,28 @@ local ffi = helpers.ffi
local eq = helpers.eq local eq = helpers.eq
local to_cstr = helpers.to_cstr local to_cstr = helpers.to_cstr
local cimp = helpers.cimport('./src/nvim/message.h') local cimp = helpers.cimport('./src/nvim/message.h', './src/nvim/memory.h',
'./src/nvim/strings.h')
describe('trunc_string', function() describe('trunc_string', function()
local buffer = ffi.typeof('char_u[40]') local buflen = 40
local function test_inplace(s, expected, room)
local function test_inplace(s, expected) room = room and room or 20
local buf = buffer() local buf = cimp.xmalloc(ffi.sizeof('char_u') * buflen)
ffi.C.strcpy(buf, s) ffi.C.strcpy(buf, s)
cimp.trunc_string(buf, buf, 20, 40) cimp.trunc_string(buf, buf, room, buflen)
eq(expected, ffi.string(buf)) eq(expected, ffi.string(buf))
cimp.xfree(buf)
end end
local function test_copy(s, expected) local function test_copy(s, expected, room)
local buf = buffer() room = room and room or 20
cimp.trunc_string(to_cstr(s), buf, 20, 40) local buf = cimp.xmalloc(ffi.sizeof('char_u') * buflen)
local str = cimp.vim_strsave(to_cstr(s))
cimp.trunc_string(str, buf, room, buflen)
eq(expected, ffi.string(buf)) eq(expected, ffi.string(buf))
cimp.xfree(buf)
cimp.xfree(str)
end end
local permutations = { local permutations = {
@ -37,6 +43,10 @@ describe('trunc_string', function()
t.func('a short text', 'a short text') t.func('a short text', 'a short text')
end) end)
it('with a string of length == 1/2 room', function()
t.func('a text that fits', 'a text that fits', 34)
end)
it('with a string exactly the truncate size', function() it('with a string exactly the truncate size', function()
t.func('a text tha just fits', 'a text tha just fits') t.func('a text tha just fits', 'a text tha just fits')
end) end)