1
0
mirror of synced 2025-01-11 05:42:15 +01:00

fix: Use after free in Text Editor when copying ErrorHoverBoxes

This commit is contained in:
WerWolv 2024-12-09 14:39:46 +01:00
parent e1dfdd9400
commit a1e399aa1a
2 changed files with 33 additions and 24 deletions

View File

@ -144,20 +144,22 @@ public:
public:
ActionableBox()=default;
explicit ActionableBox(const ImRect &box) : mBox(box) {}
std::function<void()> mCallback;
virtual bool trigger() {
return ImGui::IsMouseHoveringRect(mBox.Min,mBox.Max);
}
void setCallback(const std::function<void()> &callback) { mCallback = callback; }
virtual void callback() {}
};
class CursorChangeBox : public ActionableBox {
public:
CursorChangeBox()=default;
explicit CursorChangeBox(const ImRect &box) : ActionableBox(box) {
setCallback([]() {
ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
});
}
void callback() override {
ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
}
};
@ -165,14 +167,20 @@ public:
Coordinates mPos;
public:
ErrorGotoBox()=default;
ErrorGotoBox(const ImRect &box, const Coordinates &pos, TextEditor *editor) : ActionableBox(box), mPos(pos) {
setCallback( [this,editor]() {
editor->JumpToCoords(mPos);
});
ErrorGotoBox(const ImRect &box, const Coordinates &pos, TextEditor *editor) : ActionableBox(box), mPos(pos), mEditor(editor) {
}
bool trigger() override {
return ActionableBox::trigger() && ImGui::IsMouseClicked(0);
}
void callback() override {
mEditor->JumpToCoords(mPos);
}
private:
TextEditor *mEditor;
};
using ErrorGotoBoxes = std::map<Coordinates, ErrorGotoBox>;
@ -184,18 +192,19 @@ public:
public:
ErrorHoverBox()=default;
ErrorHoverBox(const ImRect &box, const Coordinates &pos,const char *errorText) : ActionableBox(box), mPos(pos), mErrorText(errorText) {
setCallback([this]() {
ImGui::BeginTooltip();
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.2f, 0.2f, 1.0f));
ImGui::Text("Error at line %d:", mPos.mLine);
ImGui::PopStyleColor();
ImGui::Separator();
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 0.5f, 0.2f, 1.0f));
ImGui::TextUnformatted(mErrorText.c_str());
ImGui::PopStyleColor();
ImGui::EndTooltip();
}
);
}
void callback() override {
ImGui::BeginTooltip();
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.2f, 0.2f, 1.0f));
ImGui::Text("Error at line %d:", mPos.mLine);
ImGui::PopStyleColor();
ImGui::Separator();
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 0.5f, 0.2f, 1.0f));
ImGui::TextUnformatted(mErrorText.c_str());
ImGui::PopStyleColor();
ImGui::EndTooltip();
}
};
using ErrorHoverBoxes = std::map<Coordinates, ErrorHoverBox>;

View File

@ -1016,12 +1016,12 @@ void TextEditor::Render() {
}
if (mCursorBoxes.find(gotoKey) != mCursorBoxes.end()) {
auto box = mCursorBoxes[gotoKey];
if (box.trigger()) box.mCallback();
if (box.trigger()) box.callback();
}
if (mErrorGotoBoxes.find(gotoKey) != mErrorGotoBoxes.end()) {
auto box = mErrorGotoBoxes[gotoKey];
if (box.trigger()) box.mCallback();
if (box.trigger()) box.callback();
}
// Render colorized text
@ -1060,7 +1060,7 @@ void TextEditor::Render() {
Coordinates key = Coordinates(lineNo + 1, i + 1);
if (mErrorHoverBoxes.find(key) != mErrorHoverBoxes.end()) {
auto box = mErrorHoverBoxes[key];
if (box.trigger()) box.mCallback();
if (box.trigger()) box.callback();
}
prevColor = color;