From c3f240624c41ceaf7c04411813a16d44e8b52ded Mon Sep 17 00:00:00 2001 From: Kasper Sauramo Date: Thu, 27 Mar 2025 12:15:26 +0200 Subject: [PATCH] clear clang errors --- CMakeLists.txt | 3 +++ include/hiload/hiload.h | 4 ++-- src/files.c | 7 ++++--- src/files.h | 4 +--- src/filewatcher/filewatcher.h | 2 +- src/hiload.c | 26 ++++++-------------------- src/memory.c | 7 ++++--- src/memory.h | 2 +- src/string/string.c | 6 +++--- src/string/string.h | 4 ++-- src/symbols.c | 16 ++++------------ src/types.h | 3 +++ 12 files changed, 34 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 88e94e6..09601ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(Hiload) # I just like to have this with my tooling. Also might be required for proper hotreloading. set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED) # Conditionally define _GNU_SOURCE for Linux systems if(UNIX AND NOT APPLE) @@ -14,6 +15,8 @@ endif() # Common compile options add_compile_options("-fvisibility=hidden") +# TODO: Get -Wpadded back somehow. +add_compile_options("-Wno-declaration-after-statement" "-Wno-padded") # Handle 3rd party dependencies # ############################# diff --git a/include/hiload/hiload.h b/include/hiload/hiload.h index 245ef51..07706d9 100644 --- a/include/hiload/hiload.h +++ b/include/hiload/hiload.h @@ -24,12 +24,12 @@ int hi_init(unsigned n, const char **enabled_modules); /** * Frees memory and cleans up */ -void hi_deinit(); +void hi_deinit(void); /** * Print a random assortment of information from current state */ -void hi_print_module_infos(); +void hi_print_module_infos(void); /** * Reload shared library if it has changed since last reload or init was called diff --git a/src/files.c b/src/files.c index f067e10..a55aafe 100644 --- a/src/files.c +++ b/src/files.c @@ -1,11 +1,12 @@ #include "files.h" -#include "string/string.h" #include "logger/logger.h" +#include "string/string.h" -const char *hi_file_to_str_dyn(const char *filename) { - const char *s = hi_string_from_file_dyn(filename, 0, 0); +char *hi_file_to_str_dyn(const char *filename) { + + char *s = hi_string_from_file_dyn(filename, 0, 0); if (!s) { sc_log_error("Failed to read file: %s\n", filename); return 0; diff --git a/src/files.h b/src/files.h index 4faa613..cffb46e 100644 --- a/src/files.h +++ b/src/files.h @@ -1,8 +1,6 @@ #ifndef FILES_H_ #define FILES_H_ -#include "types.h" - /** * Read file dynamically to a string * @@ -10,7 +8,7 @@ * with no size. The realistic optimum case contains two allocations, one for * the initial memory and a reallocation to match the string size. */ -const char *hi_file_to_str_dyn(const char *filename); +char *hi_file_to_str_dyn(const char *filename); #endif // FILES_H_ diff --git a/src/filewatcher/filewatcher.h b/src/filewatcher/filewatcher.h index 6b6bd83..b45744b 100644 --- a/src/filewatcher/filewatcher.h +++ b/src/filewatcher/filewatcher.h @@ -38,7 +38,7 @@ struct hiFileEvents; * * Will start the watcher thread immediately. */ -struct hiFileWatcherContext *hi_file_watcher_create(); +struct hiFileWatcherContext *hi_file_watcher_create(void); /** * Destroy a previously created file watcher context. * diff --git a/src/hiload.c b/src/hiload.c index 9af2f3d..2100748 100644 --- a/src/hiload.c +++ b/src/hiload.c @@ -1,20 +1,18 @@ #include "hiload/hiload.h" #include "array/sc_array.h" -#include "files.h" #include "filewatcher/filewatcher.h" -#include "logger/logger.h" #include "memory.h" #include "string/string.h" #include "symbols.h" #include "types.h" +#include "logger/logger.h" #include -#include #include -#include #include #include +#include #include #include #include @@ -38,22 +36,6 @@ typedef struct { static HiloadContext context = {0}; static ModuleInfos *module_infos = 0; -// if the pathname contains these, skip them from early gathering -// as they are unlikely to be changed, and would bloat our memory -const char *path_filter_list[] = {"libstdc++.", "libc++.", "libc.", "libm.", - "libgcc", "ld-linux-", NULL}; - -static inline int if_load_symbols_for_module(struct dl_phdr_info *info) { - const char *name = info->dlpi_name; - - for (int i = 0; path_filter_list[i] != NULL; i++) { - if (strstr(name, path_filter_list[i]) != NULL) { - return 0; - } - } - return 1; -} - // Callback function for dl_iterate_phdr static int gather_module_infos_callback(struct dl_phdr_info *info, size_t size, void *data) { @@ -110,8 +92,12 @@ static void module_infos_free(ModuleInfos *modules) { for (size_t i = 0; i < modules->count; i++) { // Free char* before clearing the array const char *n = 0; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-qual" sc_array_foreach(&modules->names, n) { free((void *)n); } sc_array_term(&modules->names); +#pragma GCC diagnostic pop // Use a destructor for the symbolinfos hi_free_symbol_info(&(sc_array_at(&modules->symbols, i))); diff --git a/src/memory.c b/src/memory.c index e38abfd..56566fd 100644 --- a/src/memory.c +++ b/src/memory.c @@ -35,13 +35,14 @@ HiloadResult read_memory_maps_self(struct sc_array_memreg *regions) { sc_array_clear(regions); sc_array_init(regions); - const char* maps_str = hi_file_to_str_dyn("/proc/self/maps"); + char* maps_str = hi_file_to_str_dyn("/proc/self/maps"); if (!maps_str) return HILOAD_FAIL; sc_log_debug("/proc/self/maps:\n%s", maps_str); - char *strptr = (char *)maps_str; + char *strptr = maps_str; + char *line = strtok(strptr, "\n"); while (line) { MemoryRegion reg = {0}; @@ -80,7 +81,7 @@ HiloadResult read_memory_maps_self(struct sc_array_memreg *regions) { line = strtok(NULL, "\n"); } - free((void*)maps_str); + free(maps_str); return HILOAD_OK; } diff --git a/src/memory.h b/src/memory.h index 027a214..4406430 100644 --- a/src/memory.h +++ b/src/memory.h @@ -19,8 +19,8 @@ typedef struct { uptr region_end; ptrdiff offset; u64 inode; - u32 permission; // enum MemoryPermissions const char *pathname; + u32 permission; // enum MemoryPermissions } MemoryRegion; sc_array_def(MemoryRegion, memreg); diff --git a/src/string/string.c b/src/string/string.c index 79b8051..b5b82c1 100644 --- a/src/string/string.c +++ b/src/string/string.c @@ -19,7 +19,7 @@ int hi_path_has_filename(const char *path, const char *filename) { return 0; } -const char *hi_string_from_file_dyn(const char *filename, size_t *nread, +char *hi_string_from_file_dyn(const char *filename, size_t *nread, size_t nmax) { FILE *f = fopen(filename, "r"); @@ -30,7 +30,7 @@ const char *hi_string_from_file_dyn(const char *filename, size_t *nread, // if nmax is set, use that as the chunk size and don't reallocate after bool reallocate = true; - off_t chunk_size = 4096; + size_t chunk_size = 4096; if (nmax > 0) { chunk_size = nmax; @@ -43,7 +43,7 @@ const char *hi_string_from_file_dyn(const char *filename, size_t *nread, char *p = buf; size_t n = 0; do { - n = fread(p, 1, end - p, f); + n = fread(p, 1, (size_t)(end - p), f); total_read += n; p += n; diff --git a/src/string/string.h b/src/string/string.h index 95a434b..f15a6b3 100644 --- a/src/string/string.h +++ b/src/string/string.h @@ -13,12 +13,12 @@ * * In either case, the string is reallocated to match the length before * returning. - * @param filename + * @param filename a complete pathname to the file * @param nread if not null, this will have the total amount bytes read * @param nmax if not 0, this amount of memory in bytes is read and used as * initial allocation */ -const char *hi_string_from_file_dyn(const char *filename, size_t *nread, +char *hi_string_from_file_dyn(const char *filename, size_t *nread, size_t nmax); /** diff --git a/src/symbols.c b/src/symbols.c index a55a2b6..70d6780 100644 --- a/src/symbols.c +++ b/src/symbols.c @@ -1,22 +1,14 @@ #include "symbols.h" -#include "logger/logger.h" -#include "memory.h" -#include "types.h" - #include -static inline uptr -add_ptr_offset_if_invalid(uptr p, uptr offset, - struct sc_array_memreg *const regions) { - if (memory_find_pointer(p, regions, NULL) != HILOAD_OK) - return p + offset; - return p; -} - void hi_free_symbol_info(SymbolInfos *symbols) { for (size_t i = 0; i < symbols->names.size; i++) { + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-qual" free((void *)symbols->names.elems[i]); +#pragma GCC diagnostic pop } sc_array_term(&symbols->names); diff --git a/src/types.h b/src/types.h index 329dca7..de86128 100644 --- a/src/types.h +++ b/src/types.h @@ -18,6 +18,9 @@ typedef double f64; typedef uintptr_t uptr; typedef ptrdiff_t ptrdiff; +typedef uint8_t bool8; +typedef uint32_t bool32; + typedef enum { HILOAD_FAIL = 0, HILOAD_OK } HiloadResult; #define HILOADRES(res) ((res) == HILOAD_OK)