Skip to content

Commit ac112f0

Browse files
committed
patch 8.0.1372: profile log may be truncated halfway a character
Problem: Profile log may be truncated halfway a character. Solution: Find the start of the character. (Ozaki Kiichi, closes #2385)
1 parent feeb4d0 commit ac112f0

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/ex_cmds2.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,26 @@ script_dump_profile(FILE *fd)
18341834
{
18351835
if (vim_fgets(IObuff, IOSIZE, sfd))
18361836
break;
1837+
/* When a line has been truncated, append NL, taking care
1838+
* of multi-byte characters . */
1839+
if (IObuff[IOSIZE - 2] != NUL && IObuff[IOSIZE - 2] != NL)
1840+
{
1841+
int n = IOSIZE - 2;
1842+
# ifdef FEAT_MBYTE
1843+
if (enc_utf8)
1844+
{
1845+
/* Move to the first byte of this char.
1846+
* utf_head_off() doesn't work, because it checks
1847+
* for a truncated character. */
1848+
while (n > 0 && (IObuff[n] & 0xc0) == 0x80)
1849+
--n;
1850+
}
1851+
else if (has_mbyte)
1852+
n -= mb_head_off(IObuff, IObuff + n);
1853+
# endif
1854+
IObuff[n] = NL;
1855+
IObuff[n + 1] = NUL;
1856+
}
18371857
if (i < si->sn_prl_ga.ga_len
18381858
&& (pp = &PRL_ITEM(si, i))->snp_count > 0)
18391859
{

src/testdir/test_profile.vim

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,44 @@ func Test_profile_errors()
181181
call assert_fails("profile pause", 'E750:')
182182
call assert_fails("profile continue", 'E750:')
183183
endfunc
184+
185+
func Test_profile_truncate_mbyte()
186+
if !has('multi_byte') || &enc !=# 'utf-8'
187+
return
188+
endif
189+
190+
let lines = [
191+
\ 'scriptencoding utf-8',
192+
\ 'func! Foo()',
193+
\ ' return [',
194+
\ ' \ "' . join(map(range(0x4E00, 0x4E00 + 340), 'nr2char(v:val)'), '') . '",',
195+
\ ' \ "' . join(map(range(0x4F00, 0x4F00 + 340), 'nr2char(v:val)'), '') . '",',
196+
\ ' \ ]',
197+
\ 'endfunc',
198+
\ 'call Foo()',
199+
\ ]
200+
201+
call writefile(lines, 'Xprofile_file.vim')
202+
call system(v:progpath
203+
\ . ' -es --clean --cmd "set enc=utf-8"'
204+
\ . ' -c "profile start Xprofile_file.log"'
205+
\ . ' -c "profile file Xprofile_file.vim"'
206+
\ . ' -c "so Xprofile_file.vim"'
207+
\ . ' -c "qall!"')
208+
call assert_equal(0, v:shell_error)
209+
210+
split Xprofile_file.log
211+
if &fenc != ''
212+
call assert_equal('utf-8', &fenc)
213+
endif
214+
/func! Foo()
215+
let lnum = line('.')
216+
call assert_match('^\s*return \[$', getline(lnum + 1))
217+
call assert_match("\u4F52$", getline(lnum + 2))
218+
call assert_match("\u5052$", getline(lnum + 3))
219+
call assert_match('^\s*\\ \]$', getline(lnum + 4))
220+
bwipe!
221+
222+
call delete('Xprofile_file.vim')
223+
call delete('Xprofile_file.log')
224+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ static char *(features[]) =
771771

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1372,
774776
/**/
775777
1371,
776778
/**/

0 commit comments

Comments
 (0)