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

This commit is contained in:
zeertzjq 2023-03-05 07:09:28 +08:00 committed by GitHub
parent 7100a80253
commit 39842be8cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 8 deletions

View File

@ -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;
}

View File

@ -112,15 +112,20 @@ void decor_remove(buf_T *buf, int row, int row2, Decoration *decor)
decor_free(decor);
}
void decor_free(Decoration *decor)
void decor_clear(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);
}
void decor_free(Decoration *decor)
{
if (decor) {
decor_clear(decor);
xfree(decor);
}
}

View File

@ -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()