tricky union bug (nullptr when accessing an element) + default retval on GET

This commit is contained in:
masterexplorer 2024-10-07 10:36:46 +02:00
parent eb40561074
commit 35de7aa57d
2 changed files with 6 additions and 6 deletions

4
amap.h
View File

@ -11,13 +11,13 @@ typedef enum AMAP_OP AMAP_OP;
* Operations: * Operations:
* *
* AMAP_SET Saves a mapping from key->ptr in map. ptr==NULL "tombstones" the mapping to return NULL. Returns null on error! * 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. * 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 amap_instance The instance we operate upon.
* @param op Defines which operation the caller wants. * @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 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. * @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); typedef void* (*amap)(void *amap_instance, AMAP_OP op, const char *key, void *ptr);

View File

@ -219,13 +219,13 @@ static inline void *simap_force_add_internal(simap_instance *map, const char *ke
* Operations: * Operations:
* *
* AMAP_SET Saves a mapping from key->ptr in map. ptr==NULL "tombstones" the mapping to return NULL. * 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. * 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 amap_instance The instance we operate upon.
* @param op Defines which operation the caller wants. * @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 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. * @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) { 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); simap_ptr64 *found = simap_search_internal(map, key);
if(op == AMAP_GET) { if(op == AMAP_GET) {
return found->ptr; return found ? found->ptr : ptr;
} else { } else {
assert(op == AMAP_SET); 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 */ assert(false); /* should be unreachable */
return NULL; return ptr;
} }
#endif /* SIMAP_H */ #endif /* SIMAP_H */