From 46cc93c84cd25ab6889a8fe3412e3fa9db751e12 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Tue, 17 Sep 2024 16:24:14 +0200 Subject: [PATCH] Initialize all function-level variables --- src/elf_ops.c | 8 +++---- src/gotcha.c | 22 +++++++++--------- src/gotcha_auxv.c | 28 +++++++++++------------ src/gotcha_dl.c | 13 ++++++----- src/gotcha_utils.c | 10 ++++----- src/hash.c | 36 +++++++++++++++--------------- src/hash.h | 1 + src/libc_wrappers.c | 54 ++++++++++++++++++++++----------------------- src/tool.c | 8 +++---- src/tool.h | 1 + src/translations.c | 6 ++--- 11 files changed, 95 insertions(+), 92 deletions(-) diff --git a/src/elf_ops.c b/src/elf_ops.c index 0b8afe0..82b30d3 100644 --- a/src/elf_ops.c +++ b/src/elf_ops.c @@ -48,9 +48,9 @@ static uint32_t gnu_hash_func(const char *str) { signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms, ElfW(Half) * versym, char *symnames, void *sheader) { - uint32_t *buckets, *vals; - uint32_t hash_val; - uint32_t cur_sym, cur_sym_hashval; + uint32_t *buckets = NULL, *vals = NULL; + uint32_t hash_val = 0; + uint32_t cur_sym = 0, cur_sym_hashval = 0; signed long latest_sym = -1; ElfW(Half) latest_sym_ver = 0; struct gnu_hash_header *header = (struct gnu_hash_header *)(sheader); @@ -89,7 +89,7 @@ signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms, } static unsigned long elf_hash(const unsigned char *name) { - unsigned int h = 0, g; + unsigned int h = 0, g = 0; while (*name != '\0') { h = (h << 4) + *name++; if ((g = h & 0xf0000000)) { diff --git a/src/gotcha.c b/src/gotcha.c index 3d9db45..66c4ea7 100644 --- a/src/gotcha.c +++ b/src/gotcha.c @@ -54,7 +54,7 @@ static void setInternalBindingAddressPointer(void **in, void *value) { long lookup_exported_symbol(const char *name, const struct link_map *lib, void **symbol) { - long result; + long result = 0; if (is_vdso(lib)) { debug_printf(2, "Skipping VDSO library at 0x%lx with name %s\n", lib->l_addr, LIB_NAME(lib)); @@ -96,8 +96,8 @@ long lookup_exported_symbol(const char *name, const struct link_map *lib, } int prepare_symbol(struct internal_binding_t *binding) { - int result; - struct link_map *lib; + int result = 0; + struct link_map *lib = NULL; struct gotcha_binding_t *user_binding = binding->user_binding; debug_printf(2, "Looking up exported symbols for %s\n", user_binding->name); @@ -162,7 +162,7 @@ static int rewrite_wrapper_orders(struct internal_binding_t *binding) { insert_priority); struct internal_binding_t *head; - int hash_result; + int hash_result = 0; hash_result = lookup_hashtable(&function_hash_table, (void *)name, (void **)&head); if (hash_result != 0) { @@ -209,9 +209,9 @@ static int rewrite_wrapper_orders(struct internal_binding_t *binding) { static int update_lib_bindings(ElfW(Sym) * symbol KNOWN_UNUSED, char *name, ElfW(Addr) offset, struct link_map *lmap, hash_table_t *lookuptable) { - int result; - struct internal_binding_t *internal_binding; - void **got_address; + int result = 0; + struct internal_binding_t *internal_binding = NULL; + void **got_address = NULL; result = lookup_hashtable(lookuptable, name, (void **)&internal_binding); if (result != 0) return -1; @@ -391,7 +391,7 @@ static int update_library_got(struct link_map *map, } void update_all_library_gots(hash_table_t *bindings) { - struct link_map *lib_iter; + struct link_map *lib_iter = NULL; debug_printf(2, "Searching all callsites for %lu bindings\n", (unsigned long)bindings->entry_count); for (lib_iter = _r_debug.r_map; lib_iter != 0; lib_iter = lib_iter->l_next) { @@ -402,9 +402,9 @@ void update_all_library_gots(hash_table_t *bindings) { GOTCHA_EXPORT enum gotcha_error_t gotcha_wrap( struct gotcha_binding_t *user_bindings, int num_actions, const char *tool_name) { - int i, not_found = 0, new_bindings_count = 0; - tool_t *tool; - hash_table_t new_bindings; + int i = 0, not_found = 0, new_bindings_count = 0; + tool_t *tool = NULL; + hash_table_t new_bindings = EMPTY_HASH_TABLE; gotcha_init(); diff --git a/src/gotcha_auxv.c b/src/gotcha_auxv.c index 9fe0984..ab78220 100644 --- a/src/gotcha_auxv.c +++ b/src/gotcha_auxv.c @@ -35,11 +35,11 @@ static unsigned int auxv_pagesz = 0; int parse_auxv_contents() { char name[] = "/proc/self/auxv"; - int fd, done = 0; - char buffer[BUFFER_LEN]; + int fd = -1, done = 0; + char buffer[BUFFER_LEN] = {'\0'}; const ssize_t buffer_size = BUFFER_LEN; - ssize_t offset = 0, result; - ElfW(auxv_t) * auxv, *a; + ssize_t offset = 0, result = 0; + ElfW(auxv_t) * auxv = NULL, *a = NULL; static int parsed_auxv = 0; if (parsed_auxv) return parsed_auxv == -1 ? parsed_auxv : 0; @@ -86,7 +86,7 @@ int parse_auxv_contents() { } struct link_map *get_vdso_from_auxv() { - struct link_map *m; + struct link_map *m = NULL; ElfW(Phdr) *vdso_phdrs = NULL; ElfW(Half) vdso_phdr_num, p; @@ -114,7 +114,7 @@ struct link_map *get_vdso_from_auxv() { } unsigned int get_auxv_pagesize() { - int result; + int result = 0; result = parse_auxv_contents(); return result == -1 ? 0 : auxv_pagesz; } @@ -122,8 +122,8 @@ unsigned int get_auxv_pagesize() { static char *vdso_aliases[] = {"linux-vdso.so", "linux-gate.so", NULL}; struct link_map *get_vdso_from_aliases() { - struct link_map *m; - char **aliases; + struct link_map *m = NULL; + char **aliases = NULL; for (m = _r_debug.r_map; m; m = m->l_next) { for (aliases = vdso_aliases; *aliases; aliases++) { @@ -136,7 +136,7 @@ struct link_map *get_vdso_from_aliases() { } static int read_line(char *line, int size, int fd) { - int i; + int i = 0; for (i = 0; i < size - 1; i++) { int result = gotcha_read(fd, line + i, 1); if (result == -1 && errno == EINTR) continue; // GCOVR_EXCL_LINE @@ -201,10 +201,10 @@ static int read_word(char *str, char *word, int word_size) { } struct link_map *get_vdso_from_maps() { - int maps, hit_eof; - ElfW(Addr) addr_begin, addr_end, dynamic; - char name[BUFFER_LEN], line[BUFFER_LEN], *line_pos; - struct link_map *m; + int maps = 0, hit_eof = 0; + ElfW(Addr) addr_begin = 0, addr_end = 0, dynamic = 0; + char name[BUFFER_LEN] = {'\0'}, line[BUFFER_LEN] = {'\0'}, *line_pos = NULL; + struct link_map *m = NULL; maps = gotcha_open("/proc/self/maps", O_RDONLY); for (;;) { hit_eof = read_line(line, BUFFER_LEN, maps); @@ -239,7 +239,7 @@ struct link_map *get_vdso_from_maps() { int is_vdso(const struct link_map *map) { static int vdso_checked = 0; static struct link_map *vdso = NULL; - struct link_map *result; + struct link_map *result = NULL; if (!map) return 0; if (vdso_checked) return (map == vdso); diff --git a/src/gotcha_dl.c b/src/gotcha_dl.c index fe9308d..4ac0125 100644 --- a/src/gotcha_dl.c +++ b/src/gotcha_dl.c @@ -15,6 +15,7 @@ struct Addrs { struct link_map *lmap; // output int found; }; +#define EMPTY_ADDRS {0, NULL, 0} /** * This is a callback to get headers for each library. * We check if the caller's virtual address is between base address and the @@ -29,7 +30,7 @@ struct Addrs { int lib_header_callback(struct dl_phdr_info *info, size_t size, void *data) { struct Addrs *addrs = data; const char *name = NULL; - ElfW(Addr) load_address; + ElfW(Addr) load_address = 0; for (int i = 0; i < info->dlpi_phnum; ++i) { if (info->dlpi_phdr[i].p_type == PT_LOAD) { ElfW(Addr) base_addr = info->dlpi_addr; @@ -73,11 +74,11 @@ static struct link_map *gotchas_dlsym_rtld_next_lookup(const char *name, /* Iterative over the library headers and find the caller * the address of the caller is set in addrs->library_laddr **/ - struct Addrs addrs; + struct Addrs addrs = EMPTY_ADDRS; addrs.lookup_addr = caller; addrs.lmap = _r_debug.r_map; addrs.found = 0; - void *symbol; + void *symbol = NULL; dl_iterate_phdr(lib_header_callback, &addrs); if (!addrs.found) { // GCOVR_EXCL_START error_printf("RTLD_NEXT used in code not dynamically loaded"); @@ -106,7 +107,7 @@ gotcha_wrappee_handle_t orig_dlsym_handle; static int per_binding(hash_key_t key, hash_data_t data, void *opaque KNOWN_UNUSED) { - int result; + int result = 0; struct internal_binding_t *binding = (struct internal_binding_t *)data; debug_printf(3, "Trying to re-bind %s from tool %s after dlopen\n", @@ -136,7 +137,7 @@ static int per_binding(hash_key_t key, hash_data_t data, static void *dlopen_wrapper(const char *filename, int flags) { typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle); - void *handle; + void *handle = NULL; debug_printf(1, "User called dlopen(%s, 0x%x)\n", filename, (unsigned int)flags); handle = orig_dlopen(filename, flags); @@ -154,7 +155,7 @@ static void *dlopen_wrapper(const char *filename, int flags) { static void *dlsym_wrapper(void *handle, const char *symbol_name) { typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle); typeof(&dlsym_wrapper) orig_dlsym = gotcha_get_wrappee(orig_dlsym_handle); - struct internal_binding_t *binding; + struct internal_binding_t *binding = NULL; debug_printf(1, "User called dlsym(%p, %s)\n", handle, symbol_name); int result = lookup_hashtable(&function_hash_table, (hash_key_t)symbol_name, (hash_data_t *)&binding); diff --git a/src/gotcha_utils.c b/src/gotcha_utils.c index 92f7109..02bfb1a 100644 --- a/src/gotcha_utils.c +++ b/src/gotcha_utils.c @@ -28,7 +28,7 @@ int debug_level; static void debug_init() { static int debug_initialized = 0; - char *debug_str; + char *debug_str = NULL; if (debug_initialized) { return; // GCOVR_EXCL_LINE } @@ -48,9 +48,9 @@ static void debug_init() { hash_table_t function_hash_table; hash_table_t notfound_binding_table; -static hash_table_t library_table; +static hash_table_t library_table = EMPTY_HASH_TABLE; static library_t *library_list = NULL; -unsigned int current_generation; +unsigned int current_generation = 0; static hash_hashvalue_t link_map_hash(struct link_map *map) { hash_hashvalue_t hashval = (hash_hashvalue_t)((unsigned long)map); @@ -72,8 +72,8 @@ static void setup_hash_tables() { } struct library_t *get_library(struct link_map *map) { - library_t *lib; - int result; + library_t *lib = NULL; + int result = 0; result = lookup_hashtable(&library_table, (hash_key_t)map, (hash_data_t *)&lib); if (result == -1) return NULL; diff --git a/src/hash.c b/src/hash.c index 29e27b0..a2d7390 100644 --- a/src/hash.c +++ b/src/hash.c @@ -34,8 +34,8 @@ typedef struct hash_entry_t hash_entry_t; int create_hashtable(hash_table_t *table, size_t initial_size, hash_func_t hashfunc, hash_cmp_t keycmp) { - hash_entry_t *newtable; - int entries_per_page; + hash_entry_t *newtable = NULL; + int entries_per_page = 0; entries_per_page = gotcha_getpagesize() / sizeof(hash_entry_t); if (initial_size % entries_per_page) @@ -86,9 +86,9 @@ static hash_entry_t *insert(hash_table_t *table, hash_key_t key, } int grow_hashtable(hash_table_t *table, size_t new_size) { - hash_table_t newtable; - hash_entry_t *result; - size_t i; + hash_table_t newtable = EMPTY_HASH_TABLE; + hash_entry_t *result = NULL; + size_t i = 0; newtable.table_size = new_size; newtable.entry_count = 0; @@ -126,8 +126,8 @@ int destroy_hashtable(hash_table_t *table) { } static int lookup(hash_table_t *table, hash_key_t key, hash_entry_t **entry) { - size_t index, startindex; - hash_hashvalue_t hashval; + size_t index = 0, startindex = 0; + hash_hashvalue_t hashval = 0; hashval = table->hashfunc(key); index = hashval % table->table_size; @@ -149,8 +149,8 @@ static int lookup(hash_table_t *table, hash_key_t key, hash_entry_t **entry) { } int lookup_hashtable(hash_table_t *table, hash_key_t key, hash_data_t *data) { - hash_entry_t *entry; - int result; + hash_entry_t *entry = NULL; + int result = 0; result = lookup(table, key, &entry); if (result == -1) return -1; @@ -159,10 +159,10 @@ int lookup_hashtable(hash_table_t *table, hash_key_t key, hash_data_t *data) { } int addto_hashtable(hash_table_t *table, hash_key_t key, hash_data_t data) { - size_t newsize; - int result; - hash_hashvalue_t val; - hash_entry_t *entry; + size_t newsize = 0; + int result = 0; + hash_hashvalue_t val = 0; + hash_entry_t *entry = NULL; newsize = table->table_size; while (table->entry_count > newsize / 2) newsize *= 2; @@ -179,8 +179,8 @@ int addto_hashtable(hash_table_t *table, hash_key_t key, hash_data_t data) { } int removefrom_hashtable(hash_table_t *table, hash_key_t key) { - hash_entry_t *entry; - int result; + hash_entry_t *entry = NULL; + int result = 0; result = lookup(table, key, &entry); if (result == -1) return -1; @@ -202,8 +202,8 @@ int removefrom_hashtable(hash_table_t *table, hash_key_t key) { int foreach_hash_entry(hash_table_t *table, void *opaque, int (*cb)(hash_key_t key, hash_data_t data, void *opaque)) { - int result; - struct hash_entry_t *i; + int result = 0; + struct hash_entry_t *i = NULL; for (i = table->head; i != NULL; i = i->next) { result = cb(i->key, i->data, opaque); if (result != 0) return result; // GCOVR_EXCL_LINE @@ -213,7 +213,7 @@ int foreach_hash_entry(hash_table_t *table, void *opaque, hash_hashvalue_t strhash(const char *str) { unsigned long hash = 5381; - int c; + int c = 0; while ((c = *str++)) hash = hash * 33 + c; diff --git a/src/hash.h b/src/hash.h index 4f846d4..e7b4a8a 100644 --- a/src/hash.h +++ b/src/hash.h @@ -35,6 +35,7 @@ typedef struct { struct hash_entry_t *table; struct hash_entry_t *head; } hash_table_t; +#define EMPTY_HASH_TABLE {0, 0, NULL, NULL, NULL, NULL} int create_hashtable(hash_table_t *table, size_t initial_size, hash_func_t func, hash_cmp_t keycmp); diff --git a/src/libc_wrappers.c b/src/libc_wrappers.c index 51e5900..2fc4b30 100644 --- a/src/libc_wrappers.c +++ b/src/libc_wrappers.c @@ -46,7 +46,7 @@ static malloc_link_t *free_list = NULL; static void split_allocation(malloc_link_t *allocation, size_t new_size) { size_t orig_size = allocation->header.size; - malloc_link_t *newalloc; + malloc_link_t *newalloc = NULL; if (orig_size - new_size <= sizeof(malloc_link_t)) return; @@ -58,10 +58,10 @@ static void split_allocation(malloc_link_t *allocation, size_t new_size) { } void *gotcha_malloc(size_t size) { - malloc_link_t *cur, *prev, *newalloc; - malloc_link_t *best_fit = NULL, *best_fit_prev; - ssize_t best_fit_diff, diff, block_size; - void *result; + malloc_link_t *cur = NULL, *prev = NULL, *newalloc = NULL; + malloc_link_t *best_fit = NULL, *best_fit_prev = NULL; + ssize_t best_fit_diff = SIZE_MAX, diff = 0, block_size = 0; + void *result = NULL; if (size < MIN_SIZE) size = MIN_SIZE; if (size % 8) size += 8 - (size % 8); @@ -107,8 +107,8 @@ void *gotcha_malloc(size_t size) { } void *gotcha_realloc(void *buffer, size_t size) { - void *newbuffer; - malloc_link_t *alloc; + void *newbuffer = NULL; + malloc_link_t *alloc = NULL; alloc = (malloc_link_t *)(((malloc_header_t *)buffer) - 1); @@ -124,7 +124,7 @@ void *gotcha_realloc(void *buffer, size_t size) { } void gotcha_free(void *buffer) { - malloc_link_t *alloc; + malloc_link_t *alloc = NULL; alloc = (malloc_link_t *)(((malloc_header_t *)buffer) - 1); alloc->next = free_list; @@ -132,7 +132,7 @@ void gotcha_free(void *buffer) { } void gotcha_memcpy(void *dest, void *src, size_t size) { - size_t i; + size_t i = 0; for (i = 0; i < size; i++) { ((unsigned char *)dest)[i] = ((unsigned char *)src)[i]; } @@ -164,7 +164,7 @@ int gotcha_strcmp(const char *in_one, const char *in_two) { } char *gotcha_strstr(const char *searchIn, const char *searchFor) { - int i, j; + int i = 0, j = 0; if (!searchFor[0]) return NULL; // GCOVR_EXCL_LINE for (i = 0; searchIn[i]; i++) { @@ -183,14 +183,14 @@ ssize_t gotcha_write(int fd, const void *buf, size_t count) { } size_t gotcha_strlen(const char *s) { - size_t i; + size_t i = 0; for (i = 0; s[i]; i++) ; return i; } size_t gotcha_strnlen(const char *s, size_t max_length) { - size_t i; + size_t i = 0; for (i = 0; s[i] && i < max_length; i++) ; return i; @@ -198,8 +198,8 @@ size_t gotcha_strnlen(const char *s, size_t max_length) { static int ulong_to_hexstr(unsigned long num, char *str, int strlen, int uppercase) { - int len, i; - unsigned long val; + int len = 0, i = 0; + unsigned long val = 0UL; char base_char = uppercase ? 'A' : 'a'; if (num == 0) { @@ -224,8 +224,8 @@ static int ulong_to_hexstr(unsigned long num, char *str, int strlen, } static int ulong_to_str(unsigned long num, char *str, int strlen) { - int len, i; - unsigned long val; + int len = 0, i = 0; + unsigned long val = 0UL; if (num == 0) { if (strlen < 2) return -1; // GCOVR_EXCL_LINE @@ -248,7 +248,7 @@ static int ulong_to_str(unsigned long num, char *str, int strlen) { } static int slong_to_str(signed long num, char *str, int strlen) { - int result; + int result = 0; if (num >= 0) return ulong_to_str((unsigned long)num, str, strlen); result = ulong_to_str((unsigned long)(num * -1), str + 1, strlen - 1); @@ -259,8 +259,8 @@ static int slong_to_str(signed long num, char *str, int strlen) { // GCOVR_EXCL_START void gotcha_assert_fail(const char *s, const char *file, unsigned int line, const char *function) { - char linestr[64]; - int result; + char linestr[64] = {'\0'}; + int result = 0; result = ulong_to_str(line, linestr, sizeof(linestr) - 1); if (result == -1) linestr[0] = '\0'; @@ -279,8 +279,8 @@ void gotcha_assert_fail(const char *s, const char *file, unsigned int line, extern char **__environ; char *gotcha_getenv(const char *name) { - char **s; - int name_len; + char **s = NULL; + int name_len = 0; name_len = gotcha_strlen(name); for (s = __environ; *s; s++) { @@ -307,7 +307,7 @@ unsigned int gotcha_getpagesize() { int gotcha_open(const char *pathname, int flags, ...) { mode_t mode; va_list args; - long result; + long result = 0; va_start(args, flags); if (flags & O_CREAT) { @@ -330,7 +330,7 @@ int gotcha_open(const char *pathname, int flags, ...) { void *gotcha_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) { - long result; + long result = 0; result = syscall(SYS_mmap, addr, length, prot, flags, fd, offset); return (void *)result; @@ -338,7 +338,7 @@ void *gotcha_mmap(void *addr, size_t length, int prot, int flags, int fd, int gotcha_atoi(const char *nptr) { int neg = 1, len, val = 0, mult = 1; - const char *cur; + const char *cur = NULL; while (*nptr == '-') { neg = neg * -1; @@ -392,9 +392,9 @@ int gotcha_int_printf(int fd, const char *format, ...) { va_list args; const char *str = format; int buffer_pos = 0; - int char_width, short_width, long_width, long_long_width, size_width; + int char_width = 0, short_width = 0, long_width = 0, long_long_width = 0, size_width = 0; int num_printed = 0; - char buffer[4096]; + char buffer[4096] = {'\0'}; va_start(args, format); while (*str) { @@ -517,7 +517,7 @@ int gotcha_int_printf(int fd, const char *format, ...) { } void *gotcha_memset(void *s, int c, size_t n) { - size_t i; + size_t i = 0; unsigned char byte = (unsigned char)c; for (i = 0; i < n; i++) { ((unsigned char *)s)[i] = byte; diff --git a/src/tool.c b/src/tool.c index 8202a70..0e3dc46 100644 --- a/src/tool.c +++ b/src/tool.c @@ -83,7 +83,7 @@ tool_t *create_tool(const char *tool_name) { } tool_t *get_tool(const char *tool_name) { - tool_t *t; + tool_t *t = NULL; for (t = tools; t; t = t->next_tool) { if (gotcha_strcmp(tool_name, t->tool_name) == 0) { return t; @@ -95,8 +95,8 @@ tool_t *get_tool(const char *tool_name) { binding_t *add_binding_to_tool(tool_t *tool, struct gotcha_binding_t *user_binding, int user_binding_size) { - binding_t *newbinding; - int result, i; + binding_t *newbinding = NULL; + int result = 0, i = 0; newbinding = (binding_t *)gotcha_malloc(sizeof(binding_t)); newbinding->tool = tool; struct internal_binding_t *internal_bindings = @@ -149,7 +149,7 @@ binding_t *get_bindings() { return all_bindings; } binding_t *get_tool_bindings(tool_t *tool) { return tool->binding; } struct gotcha_configuration_t get_default_configuration() { - struct gotcha_configuration_t result; + struct gotcha_configuration_t result = EMPTY_CONFIGURATION; result.priority = UNSET_PRIORITY; return result; } diff --git a/src/tool.h b/src/tool.h index 8c7e9b3..5d6e149 100644 --- a/src/tool.h +++ b/src/tool.h @@ -32,6 +32,7 @@ enum gotcha_config_key_t { GOTCHA_PRIORITY }; struct gotcha_configuration_t { int priority; }; +#define EMPTY_CONFIGURATION {0} /** * A per-library structure diff --git a/src/translations.c b/src/translations.c index 6325f8a..244b249 100644 --- a/src/translations.c +++ b/src/translations.c @@ -18,9 +18,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include "gotcha_utils.h" -int main_wrapped; -gotcha_wrappee_handle_t gotcha_internal_libc_main_wrappee_handle; -gotcha_wrappee_handle_t gotcha_internal_main_wrappee_handle; +int main_wrapped = 0; +gotcha_wrappee_handle_t gotcha_internal_libc_main_wrappee_handle = NULL; +gotcha_wrappee_handle_t gotcha_internal_main_wrappee_handle = NULL; /** * This function is excluded from coverage as this is only required for our * wrapper to call main.