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

View File

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