Skip to content

Commit

Permalink
fix: Use after free in Text Editor when copying ErrorHoverBoxes
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Dec 9, 2024
1 parent e1dfdd9 commit a1e399a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
51 changes: 30 additions & 21 deletions lib/third_party/imgui/ColorTextEditor/include/TextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,35 +144,43 @@ class TextEditor
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);
}
};

class ErrorGotoBox : public ActionableBox {
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>;
Expand All @@ -184,18 +192,19 @@ class TextEditor
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>;
Expand Down
6 changes: 3 additions & 3 deletions lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a1e399a

Please sign in to comment.