Skip to content

Commit

Permalink
fix(extmarks): don't leak memory on error (neovim#22507)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeertzjq authored and yesean committed Mar 25, 2023
1 parent 996e355 commit 137ce32
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/nvim/api/extmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
return (Integer)id;

error:
clear_virttext(&decor.virt_text);
xfree(decor.sign_text);
decor_clear(&decor);
return 0;
}

Expand Down
17 changes: 11 additions & 6 deletions src/nvim/decoration.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,20 @@ void decor_remove(buf_T *buf, int row, int row2, Decoration *decor)
decor_free(decor);
}

void decor_clear(Decoration *decor)
{
clear_virttext(&decor->virt_text);
for (size_t i = 0; i < kv_size(decor->virt_lines); i++) {
clear_virttext(&kv_A(decor->virt_lines, i).line);
}
kv_destroy(decor->virt_lines);
xfree(decor->sign_text);
}

void decor_free(Decoration *decor)
{
if (decor) {
clear_virttext(&decor->virt_text);
for (size_t i = 0; i < kv_size(decor->virt_lines); i++) {
clear_virttext(&kv_A(decor->virt_lines, i).line);
}
kv_destroy(decor->virt_lines);
xfree(decor->sign_text);
decor_clear(decor);
xfree(decor);
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/functional/api/extmark_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ describe('API/extmarks', function()
eq("Invalid 'id': expected positive Integer", pcall_err(set_extmark, ns, {}, 0, 0, { end_col = 1, end_row = 1 }))
eq("Invalid mark position: expected 2 Integer items", pcall_err(get_extmarks, ns, {}, {-1, -1}))
eq("Invalid mark position: expected mark id Integer or 2-item Array", pcall_err(get_extmarks, ns, true, {-1, -1}))
-- No memory leak with virt_text, virt_lines, sign_text
eq("right_gravity is not a boolean", pcall_err(set_extmark, ns, marks[2], 0, 0, {
virt_text = {{'foo', 'Normal'}},
virt_lines = {{{'bar', 'Normal'}}},
sign_text = 'a',
right_gravity = 'baz',
}))
end)

it("can end extranges past final newline using end_col = 0", function()
Expand Down

0 comments on commit 137ce32

Please sign in to comment.