From 566e4f6f8bd7151498e89670c0ea469dad6f9d00 Mon Sep 17 00:00:00 2001 From: Eric Kilmer Date: Mon, 6 Apr 2020 10:48:33 -0400 Subject: [PATCH] Pass resource by const pointer instead of value (#127) * Pass resource by const pointer instead of value Found/Suggested by LGTM: This parameter of type resource is 128 bytes - consider passing a const pointer/reference instead. * Fix pepy * resource by reference * Change other iter* functions to be consistent pass by const reference --- dump-pe/main.cpp | 33 ++++++++++--------- .../include/parser-library/parse.h | 24 +++++++------- pe-parser-library/src/parse.cpp | 16 ++++----- pepy/pepy.cpp | 19 ++++++----- 4 files changed, 48 insertions(+), 44 deletions(-) diff --git a/dump-pe/main.cpp b/dump-pe/main.cpp index 357809d..48dbc54 100644 --- a/dump-pe/main.cpp +++ b/dump-pe/main.cpp @@ -31,7 +31,10 @@ THE SOFTWARE. using namespace peparse; -int printExps(void *N, VA funcAddr, std::string &mod, std::string &func) { +int printExps(void *N, + const VA &funcAddr, + const std::string &mod, + const std::string &func) { static_cast(N); auto address = static_cast(funcAddr); @@ -47,7 +50,7 @@ int printExps(void *N, VA funcAddr, std::string &mod, std::string &func) { } int printImports(void *N, - VA impAddr, + const VA &impAddr, const std::string &modName, const std::string &symName) { static_cast(N); @@ -59,7 +62,7 @@ int printImports(void *N, return 0; } -int printRelocs(void *N, VA relocAddr, reloc_type type) { +int printRelocs(void *N, const VA &relocAddr, const reloc_type &type) { static_cast(N); std::cout << "TYPE: "; @@ -99,12 +102,12 @@ int printRelocs(void *N, VA relocAddr, reloc_type type) { } int printSymbols(void *N, - std::string &strName, - uint32_t &value, - int16_t §ionNumber, - uint16_t &type, - uint8_t &storageClass, - uint8_t &numberOfAuxSymbols) { + const std::string &strName, + const uint32_t &value, + const int16_t §ionNumber, + const uint16_t &type, + const uint8_t &storageClass, + const uint8_t &numberOfAuxSymbols) { static_cast(N); std::cout << "Symbol Name: " << strName << "\n"; @@ -227,7 +230,7 @@ int printSymbols(void *N, return 0; } -int printRich(void *N, rich_entry r) { +int printRich(void *N, const rich_entry &r) { static_cast(N); std::cout << std::dec; std::cout << std::setw(10) << "ProdId:" << std::setw(7) << r.ProductId; @@ -239,7 +242,7 @@ int printRich(void *N, rich_entry r) { return 0; } -int printRsrc(void *N, resource r) { +int printRsrc(void *N, const resource &r) { static_cast(N); if (r.type_str.length()) @@ -264,10 +267,10 @@ int printRsrc(void *N, resource r) { } int printSecs(void *N, - VA secBase, - std::string &secName, - image_section_header s, - bounded_buffer *data) { + const VA &secBase, + const std::string &secName, + const image_section_header &s, + const bounded_buffer *data) { static_cast(N); static_cast(s); diff --git a/pe-parser-library/include/parser-library/parse.h b/pe-parser-library/include/parser-library/parse.h index 0a20d85..2e839df 100644 --- a/pe-parser-library/include/parser-library/parse.h +++ b/pe-parser-library/include/parser-library/parse.h @@ -190,38 +190,38 @@ parsed_pe *ParsePEFromFile(const char *filePath); void DestructParsedPE(parsed_pe *p); // iterate over Rich header entries -typedef int (*iterRich)(void *, rich_entry); +typedef int (*iterRich)(void *, const rich_entry &); void IterRich(parsed_pe *pe, iterRich cb, void *cbd); // iterate over the resources -typedef int (*iterRsrc)(void *, resource); +typedef int (*iterRsrc)(void *, const resource &); void IterRsrc(parsed_pe *pe, iterRsrc cb, void *cbd); // iterate over the imports by RVA and string -typedef int (*iterVAStr)(void *, VA, const std::string &, const std::string &); +typedef int (*iterVAStr)(void *, const VA &, const std::string &, const std::string &); void IterImpVAString(parsed_pe *pe, iterVAStr cb, void *cbd); // iterate over relocations in the PE file -typedef int (*iterReloc)(void *, VA, reloc_type); +typedef int (*iterReloc)(void *, const VA &, const reloc_type &); void IterRelocs(parsed_pe *pe, iterReloc cb, void *cbd); // Iterate over symbols (symbol table) in the PE file typedef int (*iterSymbol)(void *, - std::string &, - std::uint32_t &, - std::int16_t &, - std::uint16_t &, - std::uint8_t &, - std::uint8_t &); + const std::string &, + const std::uint32_t &, + const std::int16_t &, + const std::uint16_t &, + const std::uint8_t &, + const std::uint8_t &); void IterSymbols(parsed_pe *pe, iterSymbol cb, void *cbd); // iterate over the exports -typedef int (*iterExp)(void *, VA, std::string &, std::string &); +typedef int (*iterExp)(void *, const VA &, const std::string &, const std::string &); void IterExpVA(parsed_pe *pe, iterExp cb, void *cbd); // iterate over sections typedef int (*iterSec)( - void *, VA secBase, std::string &, image_section_header, bounded_buffer *b); + void *, const VA &, const std::string &, const image_section_header &, const bounded_buffer *); void IterSec(parsed_pe *pe, iterSec cb, void *cbd); // get byte at VA in PE diff --git a/pe-parser-library/src/parse.cpp b/pe-parser-library/src/parse.cpp index af82412..6eb8bda 100644 --- a/pe-parser-library/src/parse.cpp +++ b/pe-parser-library/src/parse.cpp @@ -597,7 +597,7 @@ bool getSecForVA(const std::vector
&secs, VA v, section &sec) { } void IterRich(parsed_pe *pe, iterRich cb, void *cbd) { - for (rich_entry r : pe->peHeader.rich.Entries) { + for (rich_entry &r : pe->peHeader.rich.Entries) { if (cb(cbd, r) != 0) { break; } @@ -607,13 +607,11 @@ void IterRich(parsed_pe *pe, iterRich cb, void *cbd) { void IterRsrc(parsed_pe *pe, iterRsrc cb, void *cbd) { parsed_pe_internal *pint = pe->internal; - for (resource r : pint->rsrcs) { + for (const resource &r : pint->rsrcs) { if (cb(cbd, r) != 0) { break; } } - - return; } bool parse_resource_id(bounded_buffer *data, @@ -2455,7 +2453,7 @@ void DestructParsedPE(parsed_pe *p) { void IterImpVAString(parsed_pe *pe, iterVAStr cb, void *cbd) { std::vector &l = pe->internal->imports; - for (importent i : l) { + for (importent &i : l) { if (cb(cbd, i.addr, i.moduleName, i.symbolName) != 0) { break; } @@ -2468,7 +2466,7 @@ void IterImpVAString(parsed_pe *pe, iterVAStr cb, void *cbd) { void IterRelocs(parsed_pe *pe, iterReloc cb, void *cbd) { std::vector &l = pe->internal->relocs; - for (reloc r : l) { + for (reloc &r : l) { if (cb(cbd, r.shiftedAddr, r.type) != 0) { break; } @@ -2481,7 +2479,7 @@ void IterRelocs(parsed_pe *pe, iterReloc cb, void *cbd) { void IterSymbols(parsed_pe *pe, iterSymbol cb, void *cbd) { std::vector &l = pe->internal->symbols; - for (symbol s : l) { + for (symbol &s : l) { if (cb(cbd, s.strName, s.value, @@ -2500,7 +2498,7 @@ void IterSymbols(parsed_pe *pe, iterSymbol cb, void *cbd) { void IterExpVA(parsed_pe *pe, iterExp cb, void *cbd) { std::vector &l = pe->internal->exports; - for (exportent i : l) { + for (exportent &i : l) { if (cb(cbd, i.addr, i.moduleName, i.symbolName) != 0) { break; } @@ -2513,7 +2511,7 @@ void IterExpVA(parsed_pe *pe, iterExp cb, void *cbd) { void IterSec(parsed_pe *pe, iterSec cb, void *cbd) { parsed_pe_internal *pint = pe->internal; - for (section s : pint->secs) { + for (section &s : pint->secs) { if (cb(cbd, s.sectionBase, s.sectionName, s.sec, s.sectionData) != 0) { break; } diff --git a/pepy/pepy.cpp b/pepy/pepy.cpp index 5903ffc..c895ac5 100644 --- a/pepy/pepy.cpp +++ b/pepy/pepy.cpp @@ -806,10 +806,10 @@ static PyObject *pepy_data_converter(bounded_buffer *data) { } int section_callback(void *cbd, - VA base, - std::string &name, - image_section_header s, - bounded_buffer *data) { + const VA &base, + const std::string &name, + const image_section_header &s, + const bounded_buffer *data) { uint32_t buflen; PyObject *sect; PyObject *tuple; @@ -880,7 +880,7 @@ static PyObject *pepy_parsed_get_sections(PyObject *self, PyObject *args) { return ret; } -int resource_callback(void *cbd, resource r) { +int resource_callback(void *cbd, const resource &r) { PyObject *rsrc; PyObject *tuple; PyObject *list = (PyObject *) cbd; @@ -940,7 +940,7 @@ static PyObject *pepy_parsed_get_resources(PyObject *self, PyObject *args) { } int import_callback(void *cbd, - VA addr, + const VA &addr, const std::string &name, const std::string &sym) { PyObject *imp; @@ -987,7 +987,10 @@ static PyObject *pepy_parsed_get_imports(PyObject *self, PyObject *args) { return ret; } -int export_callback(void *cbd, VA addr, std::string &mod, std::string &func) { +int export_callback(void *cbd, + const VA &addr, + const std::string &mod, + const std::string &func) { PyObject *exp; PyObject *tuple; PyObject *list = (PyObject *) cbd; @@ -1036,7 +1039,7 @@ static PyObject *pepy_parsed_get_exports(PyObject *self, PyObject *args) { return ret; } -int reloc_callback(void *cbd, VA addr, reloc_type type) { +int reloc_callback(void *cbd, const VA &addr, const reloc_type &type) { PyObject *reloc; PyObject *tuple; PyObject *list = (PyObject *) cbd;