diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a313f26..06d9390d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,7 +103,7 @@ find_package(PkgConfig) if(PkgConfig_FOUND) pkg_check_modules(Audit audit) - pkg_check_modules(Radare r_main) + pkg_check_modules(Radare IMPORTED_TARGET r_main>=5.8.0) endif() @@ -268,7 +268,7 @@ endif() if (USE_RADARE) if (Radare_FOUND) target_compile_definitions(lo2s PUBLIC HAVE_RADARE) - target_link_libraries(lo2s PRIVATE ${Radare_LIBRARIES}) + target_link_libraries(lo2s PRIVATE PkgConfig::Radare) target_sources(lo2s PRIVATE src/radare.cpp ) diff --git a/src/radare.cpp b/src/radare.cpp index 1df1a992..2e344dd7 100644 --- a/src/radare.cpp +++ b/src/radare.cpp @@ -23,11 +23,11 @@ namespace lo2s { -Radare::Radare() : r_lib_(r_lib_new(NULL, NULL)), r_anal_(r_anal_new()), r_asm_(r_asm_new()) +Radare::Radare() : r_lib_(r_lib_new(nullptr, nullptr)), r_anal_(r_anal_new()), r_asm_(r_asm_new()) { r_unref(r_anal_->config); - r_asm_->num = r_num_new(NULL, NULL, NULL); + r_asm_->num = r_num_new(nullptr, nullptr, nullptr); r_anal_->config = r_ref_ptr(r_asm_->config); r_anal_bind(r_anal_, &r_asm_->analb); @@ -41,20 +41,24 @@ Radare::Radare() : r_lib_(r_lib_new(NULL, NULL)), r_anal_(r_anal_new()), r_asm_( std::string Radare::single_instruction(char* buf) { - auto len = strlen(buf); - if (len == 0) + if (buf == nullptr) { - throw Error("empty instruction"); + throw Error("code->assembly is NULL"); + } + + auto it = buf; + + while (*it != '\0' && *it != '\n') + { + it++; } - for (size_t i = 0; i < len; i++) + + if (it == buf) { - if (buf[i] == '\n') - { - buf[i] = '\0'; - break; - } + throw Error("empty instruction"); } - return std::string(buf); + + return std::string(buf, it - 1); } std::string Radare::operator()(Address ip, std::istream& obj) @@ -77,6 +81,12 @@ std::string Radare::operator()(Address ip, std::istream& obj) r_asm_set_pc(r_asm_, offset); auto code = r_asm_mdisassemble(r_asm_, (unsigned char*)buffer, read_bytes); + + if (code == nullptr) + { + throw Error("could not disassemble instruction"); + } + auto ret = single_instruction(code->assembly); r_asm_code_free(code);