1
0
mirror of synced 2024-09-25 20:18:26 +02:00

patterns: Fixed memory leak when using format attribute

This commit is contained in:
WerWolv 2022-03-03 12:11:47 +01:00
parent 2880ca00da
commit 949a26ca0e
16 changed files with 51 additions and 49 deletions

View File

@ -49,7 +49,7 @@ namespace hex::pl {
evaluator->dataOffset() = std::visit(overloaded {
[this](const std::string &) -> u64 { LogConsole::abortEvaluation("placement offset cannot be a string", this); },
[this](const std::shared_ptr<Pattern> &) -> u64 { LogConsole::abortEvaluation("placement offset cannot be a custom type", this); },
[this](Pattern *) -> u64 { LogConsole::abortEvaluation("placement offset cannot be a custom type", this); },
[](auto &&offset) -> u64 { return offset; } },
offset->getValue());
}
@ -101,7 +101,7 @@ namespace hex::pl {
if (auto literal = dynamic_cast<ASTNodeLiteral *>(sizeNode.get())) {
entryCount = std::visit(overloaded {
[this](const std::string &) -> i128 { LogConsole::abortEvaluation("cannot use string to index array", this); },
[this](const std::shared_ptr<Pattern> &) -> i128 { LogConsole::abortEvaluation("cannot use custom type to index array", this); },
[this](Pattern *) -> i128 { LogConsole::abortEvaluation("cannot use custom type to index array", this); },
[](auto &&size) -> i128 { return size; } },
literal->getValue());
} else if (auto whileStatement = dynamic_cast<ASTNodeWhileStatement *>(sizeNode.get())) {
@ -197,7 +197,7 @@ namespace hex::pl {
if (auto literal = dynamic_cast<ASTNodeLiteral *>(sizeNode.get())) {
auto entryCount = std::visit(overloaded {
[this](const std::string &) -> u128 { LogConsole::abortEvaluation("cannot use string to index array", this); },
[this](const std::shared_ptr<Pattern> &) -> u128 { LogConsole::abortEvaluation("cannot use custom type to index array", this); },
[this](Pattern *) -> u128 { LogConsole::abortEvaluation("cannot use custom type to index array", this); },
[](auto &&size) -> u128 { return size; } },
literal->getValue());

View File

@ -53,7 +53,7 @@ namespace hex::pl {
u8 bitSize = std::visit(overloaded {
[this](const std::string &) -> u8 { LogConsole::abortEvaluation("bitfield field size cannot be a string", this); },
[this](const std::shared_ptr<Pattern> &) -> u8 { LogConsole::abortEvaluation("bitfield field size cannot be a custom type", this); },
[this](Pattern *) -> u8 { LogConsole::abortEvaluation("bitfield field size cannot be a custom type", this); },
[](auto &&offset) -> u8 { return static_cast<u8>(offset); } },
dynamic_cast<ASTNodeLiteral *>(literal.get())->getValue());

View File

@ -30,7 +30,7 @@ namespace hex::pl {
auto &typePattern = typePatterns.front();
return std::unique_ptr<ASTNode>(std::visit(overloaded {
[&, this](const std::shared_ptr<Pattern> &value) -> ASTNode * { LogConsole::abortEvaluation(hex::format("cannot cast custom type '{}' to '{}'", value->getTypeName(), Token::getTypeName(type)), this); },
[&, this](Pattern *value) -> ASTNode * { LogConsole::abortEvaluation(hex::format("cannot cast custom type '{}' to '{}'", value->getTypeName(), Token::getTypeName(type)), this); },
[&, this](const std::string &) -> ASTNode * { LogConsole::abortEvaluation(hex::format("cannot cast string to '{}'", Token::getTypeName(type)), this); },
[&, this](auto &&value) -> ASTNode * {
auto endianAdjustedValue = hex::changeEndianess(value, typePattern->getSize(), typePattern->getEndian());

View File

@ -76,19 +76,19 @@ namespace hex::pl {
return std::unique_ptr<ASTNode>(std::visit(overloaded {
// TODO: :notlikethis:
[this](u128 left, const std::shared_ptr<Pattern> &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](i128 left, const std::shared_ptr<Pattern> &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](double left, const std::shared_ptr<Pattern> &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](char left, const std::shared_ptr<Pattern> &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](bool left, const std::shared_ptr<Pattern> &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::string &left, const std::shared_ptr<Pattern> &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::shared_ptr<Pattern> &left, u128 right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::shared_ptr<Pattern> &left, i128 right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::shared_ptr<Pattern> &left, double right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::shared_ptr<Pattern> &left, char right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::shared_ptr<Pattern> &left, bool right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::shared_ptr<Pattern> &left, const std::string &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::shared_ptr<Pattern> &left, const std::shared_ptr<Pattern> &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](u128 left, Pattern *const &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](i128 left, Pattern *const &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](double left, Pattern *const &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](char left, Pattern *const &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](bool left, Pattern *const &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::string &left, Pattern *const &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](Pattern *const &left, u128 right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](Pattern *const &left, i128 right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](Pattern *const &left, double right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](Pattern *const &left, char right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](Pattern *const &left, bool right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](Pattern *const &left, const std::string &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](Pattern *const &left, Pattern *const &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](auto &&left, const std::string &right) -> ASTNode * { LogConsole::abortEvaluation("invalid operand used in mathematical expression", this); },
[this](const std::string &left, auto &&right) -> ASTNode * {

View File

@ -119,7 +119,7 @@ namespace hex::pl {
readVariable(evaluator, value, pattern.get());
literal = u128(hex::extract(bitfieldFieldPattern->getBitOffset() + (bitfieldFieldPattern->getBitSize() - 1), bitfieldFieldPattern->getBitOffset(), value));
} else {
literal = pattern->clone();
literal = pattern.get();
}
if (auto transformFunc = pattern->getTransformFunction(); transformFunc.has_value()) {
@ -205,7 +205,7 @@ namespace hex::pl {
std::visit(overloaded {
[this](const std::string &) { LogConsole::abortEvaluation("cannot use string to index array", this); },
[this](const std::shared_ptr<Pattern> &) { LogConsole::abortEvaluation("cannot use custom type to index array", this); },
[this](Pattern *) { LogConsole::abortEvaluation("cannot use custom type to index array", this); },
[&, this](auto &&index) {
if (auto dynamicArrayPattern = dynamic_cast<PatternArrayDynamic *>(currPattern.get())) {
if (index >= searchScope.size() || index < 0)
@ -231,25 +231,25 @@ namespace hex::pl {
currPattern = pointerPattern->getPointedAtPattern()->clone();
}
std::shared_ptr<Pattern> indexPattern;
Pattern *indexPattern;
if (currPattern->isLocal()) {
auto stackLiteral = evaluator->getStack()[currPattern->getOffset()];
if (auto stackPattern = std::get_if<std::shared_ptr<Pattern>>(&stackLiteral); stackPattern != nullptr)
if (auto stackPattern = std::get_if<Pattern *>(&stackLiteral); stackPattern != nullptr)
indexPattern = *stackPattern;
else
return hex::moveToVector<std::unique_ptr<Pattern>>(std::move(currPattern));
} else
indexPattern = currPattern->clone();
indexPattern = currPattern.get();
if (auto structPattern = dynamic_cast<PatternStruct *>(indexPattern.get()))
if (auto structPattern = dynamic_cast<PatternStruct *>(indexPattern))
searchScope = structPattern->getMembers();
else if (auto unionPattern = dynamic_cast<PatternUnion *>(indexPattern.get()))
else if (auto unionPattern = dynamic_cast<PatternUnion *>(indexPattern))
searchScope = unionPattern->getMembers();
else if (auto bitfieldPattern = dynamic_cast<PatternBitfield *>(indexPattern.get()))
else if (auto bitfieldPattern = dynamic_cast<PatternBitfield *>(indexPattern))
searchScope = bitfieldPattern->getFields();
else if (auto dynamicArrayPattern = dynamic_cast<PatternArrayDynamic *>(indexPattern.get()))
else if (auto dynamicArrayPattern = dynamic_cast<PatternArrayDynamic *>(indexPattern))
searchScope = dynamicArrayPattern->getEntries();
else if (auto staticArrayPattern = dynamic_cast<PatternArrayStatic *>(indexPattern.get()))
else if (auto staticArrayPattern = dynamic_cast<PatternArrayStatic *>(indexPattern))
searchScope = { staticArrayPattern->getTemplate() };
}
@ -272,7 +272,7 @@ namespace hex::pl {
[&](std::string &assignmentValue) {
if constexpr (isString) value = assignmentValue;
},
[&](std::shared_ptr<Pattern> &assignmentValue) { readVariable(evaluator, value, assignmentValue.get()); },
[&](Pattern *assignmentValue) { readVariable(evaluator, value, assignmentValue); },
[&](auto &&assignmentValue) { value = assignmentValue; } },
literal);
} else {

View File

@ -45,7 +45,7 @@ namespace hex::pl {
evaluator->dataOffset() = std::visit(overloaded {
[this](const std::string &) -> u64 { LogConsole::abortEvaluation("placement offset cannot be a string", this); },
[this](const std::shared_ptr<Pattern> &) -> u64 { LogConsole::abortEvaluation("placement offset cannot be a custom type", this); },
[this](Pattern *) -> u64 { LogConsole::abortEvaluation("placement offset cannot be a custom type", this); },
[](auto &&offset) -> u64 { return offset; } },
offset->getValue());
}

View File

@ -233,7 +233,7 @@ namespace hex::pl {
}
protected:
void createDefaultEntry(const std::string &value, const Token::Literal &literal) const {
void createDefaultEntry(const std::string &value, Token::Literal &&literal) const {
ImGui::TableNextRow();
ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap);
ImGui::TableNextColumn();
@ -258,7 +258,7 @@ namespace hex::pl {
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", this->getFormattedName());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", formatDisplayValue(value, literal));
ImGui::TextFormatted("{}", formatDisplayValue(value, std::move(literal)));
}
void drawCommentTooltip() const {

View File

@ -58,7 +58,7 @@ namespace hex::pl {
ImGui::TextUnformatted("]");
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this->clone()));
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this));
}
if (open) {

View File

@ -49,7 +49,7 @@ namespace hex::pl {
ImGui::TextUnformatted("]");
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this->clone()));
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this));
}
if (open) {

View File

@ -43,7 +43,7 @@ namespace hex::pl {
u8 numBytes = (this->m_bitSize / 8) + 1;
u64 extractedValue = hex::extract(this->m_bitOffset + (this->m_bitSize - 1), this->m_bitOffset, value);
ImGui::TextFormatted("{}", this->formatDisplayValue(hex::format("{0} (0x{1:X})", extractedValue, extractedValue), this->clone()));
ImGui::TextFormatted("{}", this->formatDisplayValue(hex::format("{0} (0x{1:X})", extractedValue, extractedValue), this));
}
}
@ -120,7 +120,7 @@ namespace hex::pl {
valueString += hex::format("{0:02X} ", i);
valueString += "}";
ImGui::TextFormatted("{}", this->formatDisplayValue(valueString, this->clone()));
ImGui::TextFormatted("{}", this->formatDisplayValue(valueString, this));
}
if (open) {

View File

@ -34,7 +34,7 @@ namespace hex::pl {
return false;
},
[](std::string &) { return false; },
[](std::shared_ptr<Pattern> &) { return false; } },
[](Pattern *) { return false; } },
entryValueLiteral);
if (matches)
break;
@ -63,7 +63,7 @@ namespace hex::pl {
ImGui::SameLine();
ImGui::TextUnformatted(Pattern::getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue(hex::format("{} (0x{:0{}X})", valueString.c_str(), value, this->getSize() * 2), this->clone()));
ImGui::TextFormatted("{}", this->formatDisplayValue(hex::format("{} (0x{:0{}X})", valueString.c_str(), value, this->getSize() * 2), this));
}
[[nodiscard]] std::string getFormattedName() const override {

View File

@ -45,7 +45,7 @@ namespace hex::pl {
ImGui::SameLine();
ImGui::TextUnformatted(this->getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this->clone()));
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this));
}
if (open) {

View File

@ -46,7 +46,7 @@ namespace hex::pl {
ImGui::TextUnformatted(Pattern::getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this->clone()));
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this));
}
if (open) {

View File

@ -139,7 +139,7 @@ namespace hex::pl {
std::string m_identifier;
};
using Literal = std::variant<char, bool, u128, i128, double, std::string, std::shared_ptr<Pattern>>;
using Literal = std::variant<char, bool, u128, i128, double, std::string, Pattern *>;
using ValueTypes = std::variant<Keyword, Identifier, Operator, Literal, ValueType, Separator>;
Token(Type type, auto value, u32 lineNumber) : type(type), value(value), lineNumber(lineNumber) {
@ -164,7 +164,7 @@ namespace hex::pl {
static u128 literalToUnsigned(const pl::Token::Literal &literal) {
return std::visit(overloaded {
[](const std::string &) -> u128 { LogConsole::abortEvaluation("expected integral type, got string"); },
[](const std::shared_ptr<Pattern> &) -> u128 { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](Pattern *) -> u128 { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](auto &&result) -> u128 { return result; } },
literal);
}
@ -172,7 +172,7 @@ namespace hex::pl {
static i128 literalToSigned(const pl::Token::Literal &literal) {
return std::visit(overloaded {
[](const std::string &) -> i128 { LogConsole::abortEvaluation("expected integral type, got string"); },
[](const std::shared_ptr<Pattern> &) -> i128 { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](Pattern *) -> i128 { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](auto &&result) -> i128 { return result; } },
literal);
}
@ -180,7 +180,7 @@ namespace hex::pl {
static double literalToFloatingPoint(const pl::Token::Literal &literal) {
return std::visit(overloaded {
[](const std::string &) -> double { LogConsole::abortEvaluation("expected integral type, got string"); },
[](const std::shared_ptr<Pattern> &) -> double { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](Pattern *) -> double { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](auto &&result) -> double { return result; } },
literal);
}
@ -188,7 +188,7 @@ namespace hex::pl {
static bool literalToBoolean(const pl::Token::Literal &literal) {
return std::visit(overloaded {
[](const std::string &) -> bool { LogConsole::abortEvaluation("expected integral type, got string"); },
[](const std::unique_ptr<Pattern> &) -> bool { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](Pattern *) -> bool { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](auto &&result) -> bool { return result != 0; } },
literal);
}
@ -203,7 +203,7 @@ namespace hex::pl {
[](i128 result) -> std::string { return hex::to_string(result); },
[](bool result) -> std::string { return result ? "true" : "false"; },
[](char result) -> std::string { return { 1, result }; },
[](const std::shared_ptr<Pattern> &) -> std::string { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](Pattern *) -> std::string { LogConsole::abortEvaluation("expected integral type, got custom type"); },
[](auto &&result) -> std::string { return std::to_string(result); } },
literal);
}

View File

@ -15,6 +15,8 @@
#include <hex/pattern_language/patterns/pattern_string.hpp>
#include <hex/pattern_language/patterns/pattern_enum.hpp>
#include <hex/helpers/logger.hpp>
namespace hex::pl {
void Evaluator::createParameterPack(const std::string &name, const std::vector<Token::Literal> &values) {
@ -56,7 +58,7 @@ namespace hex::pl {
pattern = std::unique_ptr<Pattern>(new PatternCharacter(this, 0));
else if (std::get_if<std::string>(&value.value()) != nullptr)
pattern = std::unique_ptr<Pattern>(new PatternString(this, 0, 1));
else if (auto patternValue = std::get_if<std::shared_ptr<Pattern>>(&value.value()); patternValue != nullptr) {
else if (auto patternValue = std::get_if<Pattern *>(&value.value()); patternValue != nullptr) {
pattern = (*patternValue)->clone();
referenceType = true;
} else
@ -127,7 +129,7 @@ namespace hex::pl {
else
LogConsole::abortEvaluation(hex::format("cannot cast type 'string' to type '{}'", pattern->getTypeName()));
},
[&](const std::shared_ptr<Pattern> &value) -> Token::Literal {
[&](Pattern *value) -> Token::Literal {
if (value->getTypeName() == pattern->getTypeName())
return value;
else

View File

@ -25,7 +25,7 @@ namespace hex::plugin::builtin {
auto &param = params[i];
std::visit(overloaded {
[&](const std::shared_ptr<pl::Pattern> &value) {
[&](pl::Pattern *value) {
formatArgs.push_back(value->toString(ctx->getProvider()));
},
[&](auto &&value) {