From 35de7aa57d6f02d53a3cb6b8583a20d31a1ea6da Mon Sep 17 00:00:00 2001 From: masterexplorer Date: Mon, 7 Oct 2024 10:36:46 +0200 Subject: [PATCH] tricky union bug (nullptr when accessing an element) + default retval on GET --- amap.h | 4 ++-- simap.h | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/amap.h b/amap.h index 80bb011..48c63f3 100644 --- a/amap.h +++ b/amap.h @@ -11,13 +11,13 @@ typedef enum AMAP_OP AMAP_OP; * Operations: * * AMAP_SET Saves a mapping from key->ptr in map. ptr==NULL "tombstones" the mapping to return NULL. Returns null on error! - * AMAP_GET Gets the symbol at key (the ptr parameter is unused). Returns NULL if there is no ptr for the key. + * AMAP_GET Gets the symbol at key (the ptr parameter is unused). Returns "ptr" if there is no data for the key. * AMAP_ERASE Erases the symbol table so it becomes empty again. Can never fail, returns NULL. * * @param amap_instance The instance we operate upon. * @param op Defines which operation the caller wants. * @param key The key (both for SET and GET). This pointer can get easily invalidated so you might need a copy or you do Trie, etc. - * @param ptr When adding a ptr (data) to the map / table, the key will point to this ptr. + * @param ptr When adding a ptr (data) to the map / table, the key will point to this ptr. Also the nt-found ptr to return in case of GET. * @returns The ptr / data stored for the key, or NULL on tombstone or when not stored yet or op is SET and there was an error. */ typedef void* (*amap)(void *amap_instance, AMAP_OP op, const char *key, void *ptr); diff --git a/simap.h b/simap.h index ae23391..ea46660 100644 --- a/simap.h +++ b/simap.h @@ -219,13 +219,13 @@ static inline void *simap_force_add_internal(simap_instance *map, const char *ke * Operations: * * AMAP_SET Saves a mapping from key->ptr in map. ptr==NULL "tombstones" the mapping to return NULL. - * AMAP_GET Gets the symbol at key (the ptr parameter is unused). Returns NULL if there is no ptr for the key. + * AMAP_GET Gets the symbol at key (the ptr parameter is unused). Returns "ptr" if there is no data for the key. * AMAP_ERASE Erases the symbol table so it becomes empty again. Can never fail, returns NULL. * * @param amap_instance The instance we operate upon. * @param op Defines which operation the caller wants. * @param key The key (both for SET and GET). This pointer can get easily invalidated so you might need a copy or you do Trie, etc. - * @param ptr When adding a ptr (data) to the map / table, the key will point to this ptr. + * @param ptr When adding a ptr (data) to the map / table, the key will point to this ptr and the "nt found" ptr to return in get... * @returns The ptr / data stored for the key, or NULL on tombstone or when not stored yet or op is SET and there was an error. */ static inline void* simap(void *amap_instance, AMAP_OP op, const char *key, void *ptr) { @@ -240,7 +240,7 @@ static inline void* simap(void *amap_instance, AMAP_OP op, const char *key, void simap_ptr64 *found = simap_search_internal(map, key); if(op == AMAP_GET) { - return found->ptr; + return found ? found->ptr : ptr; } else { assert(op == AMAP_SET); @@ -254,7 +254,7 @@ static inline void* simap(void *amap_instance, AMAP_OP op, const char *key, void } assert(false); /* should be unreachable */ - return NULL; + return ptr; } #endif /* SIMAP_H */