From 603c12da48dcbd1072ccbaf7454fac8bff68bf7b Mon Sep 17 00:00:00 2001 From: Martin Olivier Date: Sun, 14 Jul 2024 17:47:26 +0200 Subject: [PATCH] fix: linter --- .github/workflows/CI.yml | 2 +- include/dylib.hpp | 29 ++++++--------- src/dylib.cpp | 78 +++++++++++++++++----------------------- src/linux.cpp | 31 ++++++++-------- src/mac.cpp | 60 +++++++++++++++---------------- 5 files changed, 89 insertions(+), 111 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index e0773b5..67d4601 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -107,4 +107,4 @@ jobs: run: pip install cpplint - name: Run cpplint - run: cpplint --linelength=140 --filter=-whitespace/indent,-whitespace/parens include/dylib.hpp + run: cpplint --linelength=140 --filter=-whitespace/indent,-whitespace/parens,-build/include_subdir src/* include/* diff --git a/include/dylib.hpp b/include/dylib.hpp index dd0d69a..7576088 100644 --- a/include/dylib.hpp +++ b/include/dylib.hpp @@ -5,7 +5,7 @@ * @link https://github.com/martin-olivier/dylib * * @author Martin Olivier - * @copyright (c) 2023 Martin Olivier + * @copyright (c) 2024 Martin Olivier * * This library is released under MIT license */ @@ -37,11 +37,9 @@ * The dylib class can hold a dynamic library instance and interact with it * by getting its symbols like functions or global variables */ -class dylib -{ +class dylib { public: - struct filename_components - { + struct filename_components { static constexpr const char *prefix = DYLIB_WIN_OTHER("", "lib"); static constexpr const char *suffix = DYLIB_WIN_MAC_OTHER(".dll", ".dylib", ".so"); }; @@ -59,8 +57,7 @@ class dylib * * @param message the error message */ - class exception : public std::runtime_error - { + class exception : public std::runtime_error { public: explicit exception(const std::string &message) : std::runtime_error(message) {} }; @@ -70,8 +67,7 @@ class dylib * * @param message the error message */ - class load_error : public exception - { + class load_error : public exception { public: explicit load_error(const std::string &message) : exception(message) {} }; @@ -81,8 +77,7 @@ class dylib * * @param message the error message */ - class symbol_error : public exception - { + class symbol_error : public exception { public: explicit symbol_error(const std::string &message) : exception(message) {} }; @@ -170,8 +165,7 @@ class dylib * @return a pointer to the requested function */ template - T *get_function(const char *symbol_name) const - { + T *get_function(const char *symbol_name) const { #if (defined(__GNUC__) && __GNUC__ >= 8) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-function-type" @@ -183,8 +177,7 @@ class dylib } template - T *get_function(const std::string &symbol_name) const - { + T *get_function(const std::string &symbol_name) const { return get_function(symbol_name.c_str()); } @@ -199,14 +192,12 @@ class dylib * @return a reference to the requested variable */ template - T &get_variable(const char *symbol_name) const - { + T &get_variable(const char *symbol_name) const { return *reinterpret_cast(get_symbol(symbol_name)); } template - T &get_variable(const std::string &symbol_name) const - { + T &get_variable(const std::string &symbol_name) const { return get_variable(symbol_name.c_str()); } diff --git a/src/dylib.cpp b/src/dylib.cpp index 3d5a01a..531e58e 100644 --- a/src/dylib.cpp +++ b/src/dylib.cpp @@ -1,3 +1,12 @@ +/** + * @file dylib.cpp + * + * @author Martin Olivier + * @copyright (c) 2024 Martin Olivier + * + * This library is released under MIT license + */ + #if (defined(_WIN32) || defined(_WIN64)) #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN @@ -32,8 +41,7 @@ std::vector get_symbols(int fd, bool demangle); -static dylib::native_handle_type open_lib(const char *path) noexcept -{ +static dylib::native_handle_type open_lib(const char *path) noexcept { #if (defined(_WIN32) || defined(_WIN64)) return LoadLibraryA(path); #else @@ -41,18 +49,15 @@ static dylib::native_handle_type open_lib(const char *path) noexcept #endif } -static dylib::native_symbol_type locate_symbol(dylib::native_handle_type lib, const char *name) noexcept -{ +static dylib::native_symbol_type locate_symbol(dylib::native_handle_type lib, const char *name) noexcept { return DYLIB_WIN_OTHER(GetProcAddress, dlsym)(lib, name); } -static void close_lib(dylib::native_handle_type lib) noexcept -{ +static void close_lib(dylib::native_handle_type lib) noexcept { DYLIB_WIN_OTHER(FreeLibrary, dlclose)(lib); } -static std::string get_error_description() noexcept -{ +static std::string get_error_description() noexcept { #if (defined(_WIN32) || defined(_WIN64)) constexpr const size_t buf_size = 512; auto error_code = GetLastError(); @@ -70,20 +75,17 @@ static std::string get_error_description() noexcept } dylib::dylib(dylib &&other) noexcept - : m_handle(other.m_handle) -{ + : m_handle(other.m_handle) { other.m_handle = nullptr; } -dylib &dylib::operator=(dylib &&other) noexcept -{ +dylib &dylib::operator=(dylib &&other) noexcept { if (this != &other) std::swap(m_handle, other.m_handle); return *this; } -dylib::dylib(const char *dir_path, const char *lib_name, bool decorations) -{ +dylib::dylib(const char *dir_path, const char *lib_name, bool decorations) { if (!dir_path || !lib_name) throw std::invalid_argument("Null parameter"); @@ -104,34 +106,29 @@ dylib::dylib(const char *dir_path, const char *lib_name, bool decorations) m_fd = open((final_path + final_name).c_str(), O_RDONLY); if (m_fd < 0) - { throw load_error("Could not load library"); - } } -dylib::~dylib() -{ +dylib::~dylib() { if (m_handle) close_lib(m_handle); if (m_fd > -1) close(m_fd); } -std::string get_demangled_name(const char *symbol) -{ +std::string get_demangled_name(const char *symbol) { std::string result; size_t size = strlen(symbol); int status; char *buf; - buf = (char *)malloc(size); + buf = reinterpret_cast(malloc(size)); if (buf == NULL) throw std::bad_alloc(); buf = abi::__cxa_demangle(symbol, buf, &size, &status); - if (buf) - { + if (buf) { result = buf; free(buf); } @@ -139,8 +136,7 @@ std::string get_demangled_name(const char *symbol) return result; } -dylib::native_symbol_type dylib::get_symbol(const char *symbol_name) const -{ +dylib::native_symbol_type dylib::get_symbol(const char *symbol_name) const { std::vector matching_symbols; std::vector all_symbols; @@ -151,12 +147,10 @@ dylib::native_symbol_type dylib::get_symbol(const char *symbol_name) const auto symbol = locate_symbol(m_handle, symbol_name); - if (symbol == nullptr) - { + if (symbol == nullptr) { all_symbols = symbols({.demangle = false}); - for (auto &sym : all_symbols) - { + for (auto &sym : all_symbols) { auto demangled = get_demangled_name(sym.c_str()); if (demangled.empty()) @@ -168,16 +162,13 @@ dylib::native_symbol_type dylib::get_symbol(const char *symbol_name) const matching_symbols.push_back(sym); } - if (matching_symbols.size() == 0) + if (matching_symbols.size() == 0) { throw symbol_error("Could not get symbol '" + std::string(symbol_name) + "'\n" + get_error_description()); - else if (matching_symbols.size() == 1) - { + } else if (matching_symbols.size() == 1) { symbol = locate_symbol(m_handle, matching_symbols.front().c_str()); if (symbol == nullptr) throw symbol_error("Could not get symbol '" + std::string(symbol_name) + "'\n" + get_error_description()); - } - else - { + } else { throw symbol_error( "Could not get symbol '" + std::string(symbol_name) + "': multiple matches"); } @@ -186,33 +177,28 @@ dylib::native_symbol_type dylib::get_symbol(const char *symbol_name) const return symbol; } -dylib::native_symbol_type dylib::get_symbol(const std::string &symbol_name) const -{ +dylib::native_symbol_type dylib::get_symbol(const std::string &symbol_name) const { return get_symbol(symbol_name.c_str()); } -bool dylib::has_symbol(const char *symbol_name) const noexcept -{ +bool dylib::has_symbol(const char *symbol_name) const noexcept { if (!m_handle || !symbol_name) return false; return locate_symbol(m_handle, symbol_name) != nullptr; } -bool dylib::has_symbol(const std::string &symbol) const noexcept -{ +bool dylib::has_symbol(const std::string &symbol) const noexcept { return has_symbol(symbol.c_str()); } -dylib::native_handle_type dylib::native_handle() noexcept -{ +dylib::native_handle_type dylib::native_handle() noexcept { return m_handle; } -std::vector dylib::symbols(symbols_params params) const -{ +std::vector dylib::symbols(symbols_params params) const { try { return get_symbols(m_fd, params.demangle); } catch (const std::string &e) { throw symbol_error(e); } -} \ No newline at end of file +} diff --git a/src/linux.cpp b/src/linux.cpp index 4233451..00fcc51 100644 --- a/src/linux.cpp +++ b/src/linux.cpp @@ -1,3 +1,12 @@ +/** + * @file linux.cpp + * + * @author Martin Olivier + * @copyright (c) 2024 Martin Olivier + * + * This library is released under MIT license + */ + #include #include #include @@ -12,8 +21,7 @@ std::string get_demangled_name(const char *symbol); -std::vector get_symbols(int fd, bool demangle) -{ +std::vector get_symbols(int fd, bool demangle) { std::vector result; if (elf_version(EV_CURRENT) == EV_NONE) @@ -31,15 +39,13 @@ std::vector get_symbols(int fd, bool demangle) Elf_Scn *scn = NULL; GElf_Shdr shdr; - while ((scn = elf_nextscn(elf, scn)) != NULL) - { + while ((scn = elf_nextscn(elf, scn)) != NULL) { if (gelf_getshdr(scn, &shdr) != &shdr) { elf_end(elf); throw std::string("gelf_getshdr() failed"); } - if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) - { + if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) { Elf_Data *data = NULL; data = elf_getdata(scn, data); if (!data) { @@ -48,8 +54,7 @@ std::vector get_symbols(int fd, bool demangle) } int count = shdr.sh_size / shdr.sh_entsize; - for (int i = 0; i < count; i++) - { + for (int i = 0; i < count; i++) { GElf_Sym sym; if (!gelf_getsym(data, i, &sym)) { elf_end(elf); @@ -59,21 +64,19 @@ std::vector get_symbols(int fd, bool demangle) const char *name = elf_strptr(elf, shdr.sh_link, sym.st_name); if (!name) continue; - + if (strcmp(name, "") == 0) continue; - if (demangle) - { + if (demangle) { std::string demangled = get_demangled_name(name); - if (!demangled.empty()) - { + if (!demangled.empty()) { if (std::find(result.begin(), result.end(), name) == result.end()) result.push_back(demangled); continue; } } - + if (std::find(result.begin(), result.end(), name) != result.end()) continue; result.push_back(name); diff --git a/src/mac.cpp b/src/mac.cpp index 14ab616..ab9adc2 100644 --- a/src/mac.cpp +++ b/src/mac.cpp @@ -1,3 +1,12 @@ +/** + * @file mac.cpp + * + * @author Martin Olivier + * @copyright (c) 2024 Martin Olivier + * + * This library is released under MIT license + */ + #include #include #include @@ -12,14 +21,14 @@ std::string get_demangled_name(const char *symbol); -std::vector get_symbols_at_off(int fd, bool demangle, off_t offset, bool is_64_bit) -{ +std::vector get_symbols_at_off(int fd, bool demangle, off_t offset, bool is_64_bit) { std::vector result; lseek(fd, offset, SEEK_SET); struct mach_header_64 mh64; struct mach_header mh; + if (is_64_bit) read(fd, &mh64, sizeof(mh64)); else @@ -29,32 +38,27 @@ std::vector get_symbols_at_off(int fd, bool demangle, off_t offset, off_t load_commands_offset = is_64_bit ? sizeof(struct mach_header_64) : sizeof(struct mach_header); lseek(fd, offset + load_commands_offset, SEEK_SET); - for (uint32_t i = 0; i < ncmds; i++) - { + for (uint32_t i = 0; i < ncmds; i++) { struct load_command lc; read(fd, &lc, sizeof(lc)); off_t current_command_offset = lseek(fd, 0, SEEK_CUR); - if (lc.cmd == LC_SYMTAB) - { + if (lc.cmd == LC_SYMTAB) { struct symtab_command symtab; lseek(fd, current_command_offset - sizeof(lc), SEEK_SET); read(fd, &symtab, sizeof(symtab)); struct nlist_64 *symbols64 = NULL; struct nlist *symbols = NULL; - if (is_64_bit) - { - symbols64 = (struct nlist_64 *)malloc(symtab.nsyms * sizeof(struct nlist_64)); + if (is_64_bit) { + symbols64 = reinterpret_cast(malloc(symtab.nsyms * sizeof(struct nlist_64))); if (symbols == nullptr) throw std::bad_alloc(); lseek(fd, offset + symtab.symoff, SEEK_SET); read(fd, symbols64, symtab.nsyms * sizeof(struct nlist_64)); - } - else - { - symbols = (struct nlist *)malloc(symtab.nsyms * sizeof(struct nlist)); + } else { + symbols = reinterpret_cast(malloc(symtab.nsyms * sizeof(struct nlist))); if (symbols == nullptr) throw std::bad_alloc(); @@ -62,15 +66,14 @@ std::vector get_symbols_at_off(int fd, bool demangle, off_t offset, read(fd, symbols, symtab.nsyms * sizeof(struct nlist)); } - char *strtab = (char *)malloc(symtab.strsize); + char *strtab = reinterpret_cast(malloc(symtab.strsize)); if (strtab == nullptr) throw std::bad_alloc(); lseek(fd, offset + symtab.stroff, SEEK_SET); read(fd, strtab, symtab.strsize); - for (uint32_t j = 0; j < symtab.nsyms; j++) - { + for (uint32_t j = 0; j < symtab.nsyms; j++) { uint32_t strx; if (is_64_bit) strx = symbols64[j].n_un.n_strx; @@ -84,11 +87,9 @@ std::vector get_symbols_at_off(int fd, bool demangle, off_t offset, if (strcmp(name, "") == 0) continue; - if (demangle) - { + if (demangle) { std::string demangled = get_demangled_name(name); - if (!demangled.empty()) - { + if (!demangled.empty()) { if (std::find(result.begin(), result.end(), name) == result.end()) result.push_back(demangled); continue; @@ -113,8 +114,7 @@ std::vector get_symbols_at_off(int fd, bool demangle, off_t offset, return result; } -std::vector get_symbols(int fd, bool demangle) -{ +std::vector get_symbols(int fd, bool demangle) { std::vector result; std::vector tmp; uint32_t magic; @@ -123,18 +123,16 @@ std::vector get_symbols(int fd, bool demangle) read(fd, &magic, sizeof(magic)); lseek(fd, 0, SEEK_SET); - if (magic == FAT_MAGIC || magic == FAT_CIGAM) - { + if (magic == FAT_MAGIC || magic == FAT_CIGAM) { struct fat_header fat_header; read(fd, &fat_header, sizeof(fat_header)); - struct fat_arch *fat_arches = (struct fat_arch *)malloc(sizeof(struct fat_arch) * ntohl(fat_header.nfat_arch)); + struct fat_arch *fat_arches = reinterpret_cast(malloc(sizeof(struct fat_arch) * ntohl(fat_header.nfat_arch))); if (fat_arches == nullptr) throw std::bad_alloc(); read(fd, fat_arches, sizeof(struct fat_arch) * ntohl(fat_header.nfat_arch)); - for (uint32_t i = 0; i < ntohl(fat_header.nfat_arch); i++) - { + for (uint32_t i = 0; i < ntohl(fat_header.nfat_arch); i++) { tmp = get_symbols_at_off( fd, demangle, @@ -144,13 +142,13 @@ std::vector get_symbols(int fd, bool demangle) } free(fat_arches); - } - else if (magic == MH_MAGIC_64 || magic == MH_CIGAM_64) + } else if (magic == MH_MAGIC_64 || magic == MH_CIGAM_64) { result = get_symbols_at_off(fd, demangle, 0, true); - else if (magic == MH_MAGIC || magic == MH_CIGAM) + } else if (magic == MH_MAGIC || magic == MH_CIGAM) { result = get_symbols_at_off(fd, demangle, 0, false); - else + } else { throw std::string("Unsupported file format"); + } return result; }