mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 21:47:40 -05:00
fix: Regex crashes with too long input strings
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/api/localization_manager.hpp>
|
||||
|
||||
#include <regex>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <hex/ui/imgui_imhex_extensions.h>
|
||||
@@ -12,23 +12,24 @@
|
||||
namespace hex::plugin::builtin {
|
||||
|
||||
void drawRegexReplacer() {
|
||||
static auto regexInput = [] { std::string s; s.reserve(0xFFF); return s; }();
|
||||
static auto regexPattern = [] { std::string s; s.reserve(0xFFF); return s; }();
|
||||
static auto replacePattern = [] { std::string s; s.reserve(0xFFF); return s; }();
|
||||
static auto regexOutput = [] { std::string s; s.reserve(0xFFF); return s; }();
|
||||
static std::string inputString;
|
||||
static std::string regexPattern;
|
||||
static std::string replacePattern;
|
||||
static std::string outputString;
|
||||
|
||||
ImGui::PushItemWidth(-150_scaled);
|
||||
bool changed1 = ImGuiExt::InputTextIcon("hex.builtin.tools.regex_replacer.pattern"_lang, ICON_VS_REGEX, regexPattern);
|
||||
bool changed2 = ImGuiExt::InputTextIcon("hex.builtin.tools.regex_replacer.replace"_lang, ICON_VS_REGEX, replacePattern);
|
||||
bool changed3 = ImGui::InputTextMultiline("hex.builtin.tools.regex_replacer.input"_lang, regexInput, ImVec2(0, 0));
|
||||
bool changed3 = ImGui::InputTextMultiline("hex.builtin.tools.regex_replacer.input"_lang, inputString, ImVec2(0, 0));
|
||||
|
||||
if (changed1 || changed2 || changed3) {
|
||||
try {
|
||||
regexOutput = std::regex_replace(regexInput.data(), std::regex(regexPattern.data()), replacePattern.data());
|
||||
} catch (std::regex_error &) { }
|
||||
const auto regex = boost::regex(regexPattern);
|
||||
outputString = boost::regex_replace(inputString, regex, replacePattern);
|
||||
} catch (boost::regex_error &) { }
|
||||
}
|
||||
|
||||
ImGui::InputTextMultiline("hex.builtin.tools.regex_replacer.output"_lang, regexOutput.data(), regexOutput.size(), ImVec2(0, 0), ImGuiInputTextFlags_ReadOnly);
|
||||
ImGui::InputTextMultiline("hex.builtin.tools.regex_replacer.output"_lang, outputString.data(), outputString.size(), ImVec2(0, 0), ImGuiInputTextFlags_ReadOnly);
|
||||
|
||||
ImGui::PopItemWidth();
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@
|
||||
|
||||
#include <array>
|
||||
#include <ranges>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <llvm/Demangle/Demangle.h>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
namespace hex::plugin::builtin {
|
||||
|
||||
@@ -350,7 +350,7 @@ namespace hex::plugin::builtin {
|
||||
});
|
||||
|
||||
std::vector<Occurrence> result;
|
||||
std::regex regex(settings.pattern);
|
||||
boost::regex regex(settings.pattern);
|
||||
for (const auto &occurrence : stringOccurrences) {
|
||||
std::string string(occurrence.region.getSize(), '\x00');
|
||||
provider->read(occurrence.region.getStartAddress(), string.data(), occurrence.region.getSize());
|
||||
@@ -358,10 +358,10 @@ namespace hex::plugin::builtin {
|
||||
task.update();
|
||||
|
||||
if (settings.fullMatch) {
|
||||
if (std::regex_match(string, regex))
|
||||
if (boost::regex_match(string, regex))
|
||||
result.push_back(occurrence);
|
||||
} else {
|
||||
if (std::regex_search(string, regex))
|
||||
if (boost::regex_search(string, regex))
|
||||
result.push_back(occurrence);
|
||||
}
|
||||
}
|
||||
@@ -758,9 +758,9 @@ namespace hex::plugin::builtin {
|
||||
ImGuiExt::InputTextIcon("hex.builtin.view.find.regex.pattern"_lang, ICON_VS_REGEX, settings.pattern);
|
||||
|
||||
try {
|
||||
std::regex regex(settings.pattern);
|
||||
boost::regex regex(settings.pattern);
|
||||
m_settingsValid = true;
|
||||
} catch (const std::regex_error &) {
|
||||
} catch (const boost::regex_error &) {
|
||||
m_settingsValid = false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user