From dcd9cc4f62138ef94c6c496fc214045f44e62d4d Mon Sep 17 00:00:00 2001 From: Josh Ferrell Date: Thu, 30 May 2024 22:20:36 -0400 Subject: Fix crash in AnalysisMergeConflict::GetPathItem --- binaryninjaapi.h | 7 ++++++- binaryninjacore.h | 1 + collaboration.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index bb243e52..b5ce7010 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -18409,13 +18409,18 @@ namespace BinaryNinja::Collaboration Ref GetFirstSnapshot(); Ref GetSecondSnapshot(); - std::any GetPathItem(const std::string& path); + template T GetPathItem(const std::string& key); bool Success(std::nullopt_t value); bool Success(std::optional value); bool Success(const std::optional& value); }; + template<> std::any AnalysisMergeConflict::GetPathItem(const std::string& path); + template<> std::string AnalysisMergeConflict::GetPathItem(const std::string& path); + template<> uint64_t AnalysisMergeConflict::GetPathItem(const std::string& path); + template<> nlohmann::json AnalysisMergeConflict::GetPathItem(const std::string& path); + class TypeArchiveMergeConflict : public CoreRefCountObject { public: diff --git a/binaryninjacore.h b/binaryninjacore.h index 77471530..d9636c02 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -7490,6 +7490,7 @@ extern "C" BINARYNINJACOREAPI BNSnapshot* BNAnalysisMergeConflictGetBaseSnapshot(BNAnalysisMergeConflict* conflict); BINARYNINJACOREAPI BNSnapshot* BNAnalysisMergeConflictGetFirstSnapshot(BNAnalysisMergeConflict* conflict); BINARYNINJACOREAPI BNSnapshot* BNAnalysisMergeConflictGetSecondSnapshot(BNAnalysisMergeConflict* conflict); + BINARYNINJACOREAPI char* BNAnalysisMergeConflictGetPathItemString(BNAnalysisMergeConflict* conflict, const char* path); BINARYNINJACOREAPI void* BNAnalysisMergeConflictGetPathItem(BNAnalysisMergeConflict* conflict, const char* path); BINARYNINJACOREAPI bool BNAnalysisMergeConflictSuccess(BNAnalysisMergeConflict* conflict, const char* value); diff --git a/collaboration.cpp b/collaboration.cpp index 7b0057c3..b4f7d659 100644 --- a/collaboration.cpp +++ b/collaboration.cpp @@ -2101,15 +2101,65 @@ Ref AnalysisMergeConflict::GetSecondSnapshot() } -std::any AnalysisMergeConflict::GetPathItem(const std::string& path) +template <> +std::any AnalysisMergeConflict::GetPathItem(const std::string& path) { void* val = BNAnalysisMergeConflictGetPathItem(m_object, path.c_str()); if (val == nullptr) - return {}; + throw SyncException(fmt::format("Failed to find merge conflict path item \"{}\"", path)); return *(std::any*)val; } +template <> +uint64_t AnalysisMergeConflict::GetPathItem(const std::string& path) +{ + std::any anyVal = GetPathItem(path); + try + { + return std::any_cast(anyVal); + } + catch (const std::exception& e) + { + throw SyncException(fmt::format( + "Failed to cast merge conflict path item \"{}\" from \"{}\" to \"{}\": {}", + path, anyVal.type().name(), typeid(uint64_t).name(), e.what() + )); + } +} + + +template <> +std::string AnalysisMergeConflict::GetPathItem(const std::string& path) +{ + char* val = BNAnalysisMergeConflictGetPathItemString(m_object, path.c_str()); + if (val == nullptr) + throw SyncException(fmt::format("Failed to find merge conflict path item \"{}\"", path)); + + std::string strVal = val; + BNFreeString(val); + return strVal; +} + + +template <> +nlohmann::json AnalysisMergeConflict::GetPathItem(const std::string& path) +{ + std::any anyVal = GetPathItem(path); + try + { + return std::any_cast(anyVal); + } + catch (const std::exception& e) + { + throw SyncException(fmt::format( + "Failed to cast merge conflict path item \"{}\" from \"{}\" to \"{}\": {}", + path, anyVal.type().name(), typeid(nlohmann::json).name(), e.what() + )); + } +} + + bool AnalysisMergeConflict::Success(std::nullopt_t value) { return BNAnalysisMergeConflictSuccess(m_object, nullptr); -- cgit v1.3.1