Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(extmarks): don't leak memory on error #22507

Merged
merged 1 commit into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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