Free the old document text after rebuilding the new one, this was a use after free since before, after point into the old memory

This commit is contained in:
Alexandros Naskos 2020-05-07 16:04:57 +03:00
parent a0addc040d
commit cea1222b79

View File

@ -304,13 +304,15 @@ pub fn processJsonRpc(json: []const u8) !void {
.character = range.Object.getValue("end").?.Object.getValue("character").?.Integer .character = range.Object.getValue("end").?.Object.getValue("character").?.Integer
}; };
const before = document.text[0..try document.positionToIndex(start_pos)]; const old_text = document.text;
const after = document.text[try document.positionToIndex(end_pos)..document.text.len]; const before = old_text[0..try document.positionToIndex(start_pos)];
allocator.free(document.text); const after = old_text[try document.positionToIndex(end_pos)..document.text.len];
document.text = try std.mem.concat(allocator, u8, &[3][]const u8{ before, change.Object.getValue("text").?.String, after }); document.text = try std.mem.concat(allocator, u8, &[3][]const u8{ before, change.Object.getValue("text").?.String, after });
allocator.free(old_text);
} else { } else {
allocator.free(document.text); const old_text = document.text;
document.text = try std.mem.dupe(allocator, u8, change.Object.getValue("text").?.String); document.text = try std.mem.dupe(allocator, u8, change.Object.getValue("text").?.String);
allocator.free(old_text);
} }
} }