diff --git a/include/retdec/capstone2llvmir/exceptions.h b/include/retdec/capstone2llvmir/exceptions.h index 6fa6dc8dc..ddd1ad3ee 100644 --- a/include/retdec/capstone2llvmir/exceptions.h +++ b/include/retdec/capstone2llvmir/exceptions.h @@ -20,55 +20,55 @@ namespace capstone2llvmir { * Base class for all Capstone2LllvmIr errors. * This class itself should never be thrown. */ -class BaseError : public std::exception -{ - public: - virtual ~BaseError() = default; +class BaseError : public std::exception { +public: + virtual ~BaseError() = default; }; /** * An exception class encapsulating all Capstone errors. */ -class CapstoneError : public BaseError -{ - public: - CapstoneError(cs_err e); +class CapstoneError : public BaseError { +public: + CapstoneError(cs_err e); - std::string getMessage() const; - virtual const char* what() const noexcept override; + std::string getMessage() const; + virtual const char* what() const noexcept override; - private: - /// Capstone error. - cs_err _csError = CS_ERR_OK; +private: + /// Capstone error. + cs_err _csError = CS_ERR_OK; }; /** * An exception class related to Capstone mode setting errors. */ -class ModeSettingError : public BaseError -{ - public: - enum class eType - { - UNDEF, - /// Basic mode cannot be used with this arch. - BASIC_MODE, - /// Extra mode cannot be used with this arch. - EXTRA_MODE, - /// Translator cannnot change basic mode for this architecture. - BASIC_MODE_CHANGE - }; - - public: - ModeSettingError(cs_arch a, cs_mode m, eType t); - - std::string getMessage() const; - virtual const char* what() const noexcept override; - - private: - cs_arch _arch = CS_ARCH_ALL; - cs_mode _mode = CS_MODE_LITTLE_ENDIAN; - eType _type = eType::UNDEF; +class ModeSettingError : public BaseError { +public: + enum class eType + { + UNDEF, + /// Basic mode cannot be used with this arch. + BASIC_MODE, + /// Extra mode cannot be used with this arch. + EXTRA_MODE, + /// Translator cannnot change basic mode for this architecture. + BASIC_MODE_CHANGE + }; + +public: + ModeSettingError(cs_arch a, cs_mode m, eType t); + + std::string getMessage() const; + virtual const char* what() const noexcept override; + +private: + cs_arch _arch = CS_ARCH_ALL; + cs_mode _mode = CS_MODE_LITTLE_ENDIAN; + eType _type = eType::UNDEF; + std::string _whatMessage; + // Get message internal: generate message and store it to _whatMessage. + void _getMessage() noexcept; }; /** @@ -77,21 +77,23 @@ class ModeSettingError : public BaseError * * These exceptions may be suppressed and/or ignored. */ -class UnexpectedOperandsError : public BaseError -{ - public: - /** - * @param i Capstone instruction in which unexpected operand - * was encountered. - * @param comment Optional comment about the problem. - */ - UnexpectedOperandsError(cs_insn* i, const std::string& comment = ""); - - virtual const char* what() const noexcept override; - - private: - cs_insn* _insn = nullptr; - std::string _comment; +class UnexpectedOperandsError : public BaseError { +public: + /** + * @param i Capstone instruction in which unexpected operand + * was encountered. + * @param comment Optional comment about the problem. + */ + UnexpectedOperandsError(cs_insn* i, const std::string& comment = ""); + + virtual const char* what() const noexcept override; + +private: + cs_insn* _insn = nullptr; + std::string _comment; + std::string _whatMessage; + // Get message internal: generate message and store it to _whatMessage. + void _getMessage() noexcept; }; /** @@ -100,20 +102,21 @@ class UnexpectedOperandsError : public BaseError * These exceptions may be suppressed and/or ignored. Not all instructions are * handled, or will be handled in the future. */ -class UnhandledInstructionError : public BaseError -{ - public: - /** - * @param i Capstone instruction which is not handled. - * @param comment Optional comment about the problem. - */ - UnhandledInstructionError(cs_insn* i, const std::string& comment = ""); - - virtual const char* what() const noexcept override; - - private: - cs_insn* _insn = nullptr; - std::string _comment; +class UnhandledInstructionError : public BaseError { +public: + /** + * @param i Capstone instruction which is not handled. + * @param comment Optional comment about the problem. + */ + UnhandledInstructionError(cs_insn* i, const std::string& comment = ""); + + virtual const char* what() const noexcept override; + +private: + void _getMessage() noexcept; + cs_insn* _insn = nullptr; + std::string _comment; + std::string _whatMessage; }; /** @@ -122,16 +125,15 @@ class UnhandledInstructionError : public BaseError * These exceptions signal some operational problems in Capstone2LlvmIr library. * They should not be ignored. They should be reported to RetDec developers. */ -class GenericError : public BaseError -{ - public: - GenericError(const std::string& message); +class GenericError : public BaseError { +public: + GenericError(const std::string& message); - virtual const char* what() const noexcept override; + virtual const char* what() const noexcept override; - private: - /// Message returned by @c what() method. - std::string _whatMessage; +private: + /// Message returned by @c what() method. + std::string _whatMessage; }; } // namespace capstone2llvmir diff --git a/include/retdec/llvmir2hll/hll/hll_writers/c_hll_writer.h b/include/retdec/llvmir2hll/hll/hll_writers/c_hll_writer.h index ad2dd9d32..d773abe2f 100644 --- a/include/retdec/llvmir2hll/hll/hll_writers/c_hll_writer.h +++ b/include/retdec/llvmir2hll/hll/hll_writers/c_hll_writer.h @@ -135,7 +135,7 @@ class CHLLWriter: public HLLWriter { /// @} bool shouldEmitFunctionPrototypesHeader() const; - bool emitFunctionPrototypes(const FuncSet &funcs); + bool emitFunctionPrototypes(const FuncSet& funcs) override; bool emitStandardFunctionPrototypes(); bool emitFunctionPrototypesForNonLibraryFuncs(); bool emitFunctionPrototype(ShPtr func); diff --git a/src/capstone2llvmir/exceptions.cpp b/src/capstone2llvmir/exceptions.cpp index 9e9ae0823..211b1e32e 100644 --- a/src/capstone2llvmir/exceptions.cpp +++ b/src/capstone2llvmir/exceptions.cpp @@ -16,11 +16,7 @@ namespace capstone2llvmir { //============================================================================== // -CapstoneError::CapstoneError(cs_err e) : - _csError(e) -{ - -} +CapstoneError::CapstoneError(cs_err e): _csError(e) {} std::string CapstoneError::getMessage() const { @@ -29,7 +25,7 @@ std::string CapstoneError::getMessage() const const char* CapstoneError::what() const noexcept { - return getMessage().c_str(); + return cs_strerror(_csError); } // @@ -38,60 +34,54 @@ const char* CapstoneError::what() const noexcept //============================================================================== // -ModeSettingError::ModeSettingError( - cs_arch a, - cs_mode m, - eType t) - : - _arch(a), - _mode(m), - _type(t) +ModeSettingError::ModeSettingError(cs_arch a, cs_mode m, eType t): _arch(a), _mode(m), _type(t) { - + _getMessage(); } std::string ModeSettingError::getMessage() const { - std::string ms = capstoneModeToString(_mode) + " (" - + std::to_string(static_cast(_mode)) + ")"; - std::string as = capstoneArchToString(_arch) + " (" - + std::to_string(static_cast(_arch)) + ")"; + return _whatMessage; +} + +void ModeSettingError::_getMessage() noexcept +{ + std::string ms = capstoneModeToString(_mode) + " (" + std::to_string(static_cast(_mode)) + ")"; + std::string as = capstoneArchToString(_arch) + " (" + std::to_string(static_cast(_arch)) + ")"; std::string ret; switch (_type) { - case eType::BASIC_MODE: - { - ret = "Basic mode: " + ms + " cannot be used with " - "architecture: " + as; - break; - } - case eType::EXTRA_MODE: - { - ret = "Extra mode: " + ms + " cannot be used with " - "architecture: " + as; - break; - } - case eType::BASIC_MODE_CHANGE: - { - ret = "Translator cannot change basic mode to: " + ms + - " for architecture: " + as; - break; - } - case eType::UNDEF: - default: - { - ret = "Undefined type -- should not happen."; - break; - } - + case eType::BASIC_MODE: { + ret = "Basic mode: " + ms + + " cannot be used with " + "architecture: " + + as; + break; + } + case eType::EXTRA_MODE: { + ret = "Extra mode: " + ms + + " cannot be used with " + "architecture: " + + as; + break; + } + case eType::BASIC_MODE_CHANGE: { + ret = "Translator cannot change basic mode to: " + ms + " for architecture: " + as; + break; + } + case eType::UNDEF: + default: { + ret = "Undefined type -- should not happen."; + break; } - return ret; + } + _whatMessage = ret; } const char* ModeSettingError::what() const noexcept { - return getMessage().c_str(); + return _whatMessage.c_str(); } // @@ -100,28 +90,28 @@ const char* ModeSettingError::what() const noexcept //============================================================================== // -UnexpectedOperandsError::UnexpectedOperandsError( - cs_insn* i, - const std::string& comment) - : - _insn(i), - _comment(comment) +UnexpectedOperandsError::UnexpectedOperandsError(cs_insn* i, const std::string& comment): _insn(i), _comment(comment) { - + _getMessage(); } -const char* UnexpectedOperandsError::what() const noexcept +void UnexpectedOperandsError::_getMessage() noexcept { std::stringstream ret; - ret << "Unexpected operand @ " << std::hex << _insn->address - << " : " << _insn->mnemonic << " " << _insn->op_str; + ret << "Unexpected operand @ " << std::hex << _insn->address << " : " << _insn->mnemonic << " " << _insn->op_str; if (!_comment.empty()) { - ret << "\n" << "Comment: " << _comment; + ret << "\n" + << "Comment: " << _comment; } - return ret.str().c_str(); + _whatMessage = ret.str(); +} + +const char* UnexpectedOperandsError::what() const noexcept +{ + return _whatMessage.c_str(); } // @@ -130,28 +120,29 @@ const char* UnexpectedOperandsError::what() const noexcept //============================================================================== // -UnhandledInstructionError::UnhandledInstructionError( - cs_insn* i, - const std::string& comment) - : - _insn(i), - _comment(comment) +UnhandledInstructionError::UnhandledInstructionError(cs_insn* i, const std::string& comment): + _insn(i), _comment(comment) { - + _getMessage(); } -const char* UnhandledInstructionError::what() const noexcept +void UnhandledInstructionError::_getMessage() noexcept { std::stringstream ret; - ret << "Unhandled instruction @ " << std::hex << _insn->address - << " : " << _insn->mnemonic << " " << _insn->op_str; + ret << "Unhandled instruction @ " << std::hex << _insn->address << " : " << _insn->mnemonic << " " << _insn->op_str; if (!_comment.empty()) { - ret << "\n" << "Comment: " << _comment; + ret << "\n" + << "Comment: " << _comment; } - return ret.str().c_str(); + _whatMessage = ret.str(); +} + +const char* UnhandledInstructionError::what() const noexcept +{ + return _whatMessage.c_str(); } // @@ -160,11 +151,7 @@ const char* UnhandledInstructionError::what() const noexcept //============================================================================== // -GenericError::GenericError(const std::string& message) : - _whatMessage(message) -{ - -} +GenericError::GenericError(const std::string& message): _whatMessage(message) {} const char* GenericError::what() const noexcept { diff --git a/src/pdbparser/pdb_file.cpp b/src/pdbparser/pdb_file.cpp index 0c64f4156..f1c92a86a 100644 --- a/src/pdbparser/pdb_file.cpp +++ b/src/pdbparser/pdb_file.cpp @@ -149,7 +149,7 @@ bool PDBFile::save_streams_to_files(void) for (unsigned int i = 0; i < num_streams;i++) { char stream_filename[MAX_PATH+4]; - sprintf(stream_filename,"%s.%03d",pdb_filename,i); + snprintf(stream_filename,MAX_PATH+4,"%s.%03d",pdb_filename,i); FILE *fs = fopen(stream_filename,"wb"); if (fs == nullptr) return false; @@ -446,7 +446,6 @@ void PDBFile::parse_modules(void) unsigned int position = sizeof(NewDBIHdr); //0x40 unsigned int limit = sizeof(NewDBIHdr) + dbi_header_v700->cbGpModi; - int cnt = 0; MODI * entry; while (position < limit) @@ -481,7 +480,6 @@ void PDBFile::parse_modules(void) s // stream }; modules.push_back(new_module); - cnt++; // Go to next entry position += sizeof(MODI) + len; } diff --git a/src/pdbparser/pdb_symbols.cpp b/src/pdbparser/pdb_symbols.cpp index 20f3b05d7..9748a3339 100644 --- a/src/pdbparser/pdb_symbols.cpp +++ b/src/pdbparser/pdb_symbols.cpp @@ -8,6 +8,7 @@ #include #include #include +#include // Since C++11 for the macro of PRIx64 #include "retdec/pdbparser/pdb_symbols.h" @@ -58,7 +59,8 @@ void dump_local_variable(PDBLocalVariable &var) void PDBFunction::dump(void) { - printf("** Function [%s] at 0x%16lx\n", name, address); + printf("** Function [%s] at 0x%" PRIx64 "\n", name, address); + if (overload_index > 0) printf("\tFunction is overloaded. Index: %d\n", overload_index); printf("\tOffset : %08x\n", offset); @@ -100,12 +102,12 @@ void PDBFunction::dump(void) data[i].type_def->dump(true); size = data[i].type_def->size_bytes; } - printf(" Size: %d bytes [%s] at 0x%16lx\n", size, data[i].name, data[i].address); + printf(" Size: %d bytes [%s] at 0x%" PRIx64 "\n", size, data[i].name, data[i].address); } printf("\tLine number information:\n"); for (unsigned int i = 0; i < lines.size(); i++) { - printf("\t\tLine: %d Offset: %08x (%16lx)\n", lines[i].line, lines[i].offset, lines[i].offset + address); + printf("\t\tLine: %d Offset: %08x (%" PRIx64 ")\n", lines[i].line, lines[i].offset, lines[i].offset + address); } puts(""); } @@ -297,7 +299,6 @@ void PDBSymbols::parse_symbols(void) continue; PDBStream *stream = modules[m].stream; position = 4; - int cnt = 0; PDBFunction * new_function = nullptr; while (position < stream->size) { // Process all symbols in module stream @@ -371,11 +372,9 @@ void PDBSymbols::parse_symbols(void) break; } } - cnt++; position += symbol->size + 2; } - cnt = 0; while (position < stream->size) { // Process all big symbols in module stream PDBBigSymbol *symbol = reinterpret_cast(stream->data + position); @@ -397,7 +396,6 @@ void PDBSymbols::parse_symbols(void) break; } position += symbol->size + 8; - cnt++; } } parsed = true; @@ -768,7 +766,7 @@ void PDBSymbols::print_global_variables(void) puts("******* SYM global variables list *******"); for (PDBGlobalVarAddressMap::iterator it = global_variables.begin(); it != global_variables.end(); ++it) { - printf("Global variable [%s] at 0x%16lx\n", it->second.name, it->second.address); + printf("Global variable [%s] at 0x%" PRIx64 "\n", it->second.name, it->second.address); printf("\tOffset : %08x\n", it->second.offset); printf("\tSection: %04x\n", it->second.section); printf("\tModule : %d\n", it->second.module_index);