From b546d0c8d8957064424f37f1cae7bc5fc3695f4a Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Tue, 11 Feb 2025 13:57:47 -0500 Subject: Fix partial initialization of `DisassemblyTextLine` The usage of `DisassemblyTextLine` in the FFI was unsound, we would forget to initialize some fields causing a myriad of issues where round-tripping through the FFI was losing information. --- flowgraphnode.cpp | 37 ++++++------------------------------- 1 file changed, 6 insertions(+), 31 deletions(-) (limited to 'flowgraphnode.cpp') diff --git a/flowgraphnode.cpp b/flowgraphnode.cpp index e1e2351a..08fc79a6 100644 --- a/flowgraphnode.cpp +++ b/flowgraphnode.cpp @@ -19,6 +19,7 @@ // IN THE SOFTWARE. #include "binaryninjaapi.h" +#include "ffi.h" using namespace BinaryNinja; using namespace std; @@ -110,19 +111,7 @@ const vector& FlowGraphNode::GetLines() size_t count; BNDisassemblyTextLine* lines = BNGetFlowGraphNodeLines(m_object, &count); - vector result; - result.reserve(count); - for (size_t i = 0; i < count; i++) - { - DisassemblyTextLine line; - line.addr = lines[i].addr; - line.instrIndex = lines[i].instrIndex; - line.highlight = lines[i].highlight; - line.tokens = InstructionTextToken::ConvertInstructionTextTokenList(lines[i].tokens, lines[i].count); - line.tags = Tag::ConvertTagList(lines[i].tags, lines[i].tagCount); - result.push_back(line); - } - + vector result = ParseAPIObjectList(lines, count); BNFreeDisassemblyTextLines(lines, count); m_cachedLines = result; return m_cachedLines; @@ -131,25 +120,11 @@ const vector& FlowGraphNode::GetLines() void FlowGraphNode::SetLines(const vector& lines) { - BNDisassemblyTextLine* buf = new BNDisassemblyTextLine[lines.size()]; - for (size_t i = 0; i < lines.size(); i++) - { - buf[i].addr = lines[i].addr; - buf[i].instrIndex = lines[i].instrIndex; - buf[i].highlight = lines[i].highlight; - buf[i].tokens = InstructionTextToken::CreateInstructionTextTokenList(lines[i].tokens); - buf[i].count = lines[i].tokens.size(); - buf[i].tags = Tag::CreateTagList(lines[i].tags, &(buf[i].tagCount)); - } - - BNSetFlowGraphNodeLines(m_object, buf, lines.size()); + size_t inCount = 0; + BNDisassemblyTextLine* inLines = AllocAPIObjectList(lines, &inCount); + BNSetFlowGraphNodeLines(m_object, inLines, inCount); - for (size_t i = 0; i < lines.size(); i++) - { - InstructionTextToken::FreeInstructionTextTokenList(buf[i].tokens, buf[i].count); - Tag::FreeTagList(buf[i].tags, buf[i].tagCount); - } - delete[] buf; + FreeAPIObjectList(inLines, inCount); m_cachedLines = lines; m_cachedLinesValid = true; -- cgit v1.3.1