Merge #12275 from erw7/profile-fix-use-after-free

viml/profile: fix missing fixes when merging vim-patch:8.1.0130

Fix #12255

### Steps to reproduce using `nvim -u NORC`

```
nvim -u NORC
:function Test()
:endfunction
:profile start log1
:profile func Test
:call Test()
:profile stop
:profile start log2
:profile func Test
:call Test()
:profile stop
```

### Actual behaviour

#### log1
```
FUNCTION Test()
Called 1 times
...
```

#### log2
```
FUNCTION Test()
Called 2 times
...
```

### Expected behaviour

#### log1
```
FUNCTION Test()
Called 1 times
...
```

#### log2
```
FUNCTION Test()
Called 1 times
...
```
This commit is contained in:
Justin M. Keyes 2020-05-09 17:22:08 -04:00 committed by GitHub
commit 9816173fb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 17 deletions

View File

@ -1038,16 +1038,17 @@ static void profile_reset(void)
if (!HASHITEM_EMPTY(hi)) { if (!HASHITEM_EMPTY(hi)) {
n--; n--;
ufunc_T *uf = HI2UF(hi); ufunc_T *uf = HI2UF(hi);
if (uf->uf_profiling) { if (uf->uf_prof_initialized) {
uf->uf_profiling = 0; uf->uf_profiling = 0;
uf->uf_tm_count = 0; uf->uf_tm_count = 0;
uf->uf_tm_total = profile_zero(); uf->uf_tm_total = profile_zero();
uf->uf_tm_self = profile_zero(); uf->uf_tm_self = profile_zero();
uf->uf_tm_children = profile_zero(); uf->uf_tm_children = profile_zero();
XFREE_CLEAR(uf->uf_tml_count); for (int i = 0; i < uf->uf_lines.ga_len; i++) {
XFREE_CLEAR(uf->uf_tml_total); uf->uf_tml_count[i] = 0;
XFREE_CLEAR(uf->uf_tml_self); uf->uf_tml_total[i] = uf->uf_tml_self[i] = 0;
}
uf->uf_tml_start = profile_zero(); uf->uf_tml_start = profile_zero();
uf->uf_tml_children = profile_zero(); uf->uf_tml_children = profile_zero();

View File

@ -6,6 +6,9 @@ local eval = helpers.eval
local command = helpers.command local command = helpers.command
local eq, neq = helpers.eq, helpers.neq local eq, neq = helpers.eq, helpers.neq
local tempfile = helpers.tmpname() local tempfile = helpers.tmpname()
local source = helpers.source
local matches = helpers.matches
local read_file = helpers.read_file
-- tmpname() also creates the file on POSIX systems. Remove it again. -- tmpname() also creates the file on POSIX systems. Remove it again.
-- We just need the name, ignoring any race conditions. -- We just need the name, ignoring any race conditions.
@ -32,20 +35,67 @@ describe(':profile', function()
end end
end) end)
it('dump', function() describe('dump', function()
eq(0, eval('v:profiling')) it('works', function()
command('profile start ' .. tempfile) eq(0, eval('v:profiling'))
eq(1, eval('v:profiling')) command('profile start ' .. tempfile)
assert_file_exists_not(tempfile) eq(1, eval('v:profiling'))
command('profile dump') assert_file_exists_not(tempfile)
assert_file_exists(tempfile) command('profile dump')
assert_file_exists(tempfile)
end)
it('not resetting the profile', function()
source([[
function! Test()
endfunction
]])
command('profile start ' .. tempfile)
assert_file_exists_not(tempfile)
command('profile func Test')
command('call Test()')
command('profile dump')
assert_file_exists(tempfile)
local profile = read_file(tempfile)
matches('Called 1 time', profile)
command('call Test()')
command('profile dump')
assert_file_exists(tempfile)
profile = read_file(tempfile)
matches('Called 2 time', profile)
command('profile stop')
end)
end) end)
it('stop', function() describe('stop', function()
command('profile start ' .. tempfile) it('works', function()
assert_file_exists_not(tempfile) command('profile start ' .. tempfile)
command('profile stop') assert_file_exists_not(tempfile)
assert_file_exists(tempfile) command('profile stop')
eq(0, eval('v:profiling')) assert_file_exists(tempfile)
eq(0, eval('v:profiling'))
end)
it('resetting the profile', function()
source([[
function! Test()
endfunction
]])
command('profile start ' .. tempfile)
assert_file_exists_not(tempfile)
command('profile func Test')
command('call Test()')
command('profile stop')
assert_file_exists(tempfile)
local profile = read_file(tempfile)
matches('Called 1 time', profile)
command('profile start ' .. tempfile)
command('profile func Test')
command('call Test()')
command('profile stop')
assert_file_exists(tempfile)
profile = read_file(tempfile)
matches('Called 1 time', profile)
end)
end) end)
end) end)