From a6f7e80ea31242581b0825ce8ef393740d44d52f Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Mon, 29 May 2023 23:47:39 +0200 Subject: [PATCH] fix memory leak on error in `makeInnerScope` --- src/analysis.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index 9a4f417..335fecb 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -2693,18 +2693,18 @@ fn makeInnerScope(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index) er if (container_decl.ast.enum_token != null) { if (std.mem.eql(u8, name, "_")) return; - const Documentation = @TypeOf(@as(types.CompletionItem, undefined).documentation); - var doc: Documentation = if (try getDocComments(allocator, tree, decl, .markdown)) |docs| .{ .MarkupContent = types.MarkupContent{ .kind = .markdown, .value = docs } } else null; + const doc = try getDocComments(allocator, tree, decl, .markdown); + errdefer if (doc) |d| allocator.free(d); var gop_res = try context.doc_scope.enum_completions.getOrPut(allocator, .{ .label = name, .kind = .Constant, .insertText = name, .insertTextFormat = .PlainText, - .documentation = doc, + .documentation = if (doc) |d| .{ .MarkupContent = types.MarkupContent{ .kind = .markdown, .value = d } } else null, }); if (gop_res.found_existing) { - if (doc) |d| allocator.free(d.MarkupContent.value); + if (doc) |d| allocator.free(d); } } }