clear clang errors

This commit is contained in:
Kasper Sauramo
2025-03-27 12:15:26 +02:00
parent a3d4d46226
commit c3f240624c
12 changed files with 34 additions and 50 deletions

View File

@@ -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
# #############################

View File

@@ -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

View File

@@ -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;

View File

@@ -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_

View File

@@ -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.
*

View File

@@ -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 <assert.h>
#include <stddef.h>
#include <dlfcn.h>
#include <libdwarf/libdwarf.h>
#include <libelf.h>
#include <link.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -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)));

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);
/**

View File

@@ -1,22 +1,14 @@
#include "symbols.h"
#include "logger/logger.h"
#include "memory.h"
#include "types.h"
#include <stdlib.h>
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);

View File

@@ -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)