X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Frmr%2Fcommon%2Fsrc%2Fsymtab.c;h=c88c82cca60a61ccc538b84d2fe2c10493ec7baa;hb=280477fab59b789d924830e1a50dc9d2656915af;hp=8b94a41e81cac9ed11816381639ad27425efb9f5;hpb=2dfb310585e1ea20d222aa1ed129c89e8c240935;p=ric-plt%2Flib%2Frmr.git diff --git a/src/rmr/common/src/symtab.c b/src/rmr/common/src/symtab.c index 8b94a41..c88c82c 100644 --- a/src/rmr/common/src/symtab.c +++ b/src/rmr/common/src/symtab.c @@ -53,6 +53,7 @@ Mod: 2016 23 Feb - converted Symtab refs so that caller need only a #include #include #include +#include #include "rmr_symtab.h" @@ -96,7 +97,10 @@ static int sym_hash( const char *n, long size ) return (int ) (t % size); } -/* delete element pointed to by eptr at hash loc hv */ +/* + Delete element pointed to by eptr which is assumed to be + a member of the list at symtab[i]. +*/ static void del_ele( Sym_tab *table, int hv, Sym_ele *eptr ) { Sym_ele **sym_tab; @@ -124,6 +128,39 @@ static void del_ele( Sym_tab *table, int hv, Sym_ele *eptr ) } } +/* + Delete the head element from table[i]. This isn't really + needed, but keeps code analysers from claiming that memory + is being used after it is freed. +*/ +static void del_head_ele( Sym_tab *table, int hv ) { + Sym_ele **sym_tab; + Sym_ele *eptr; // first in list + + + if( hv < 0 || hv >= table->size ) { + return; + } + + sym_tab = table->symlist; + if( (eptr = sym_tab[hv]) != NULL ) // not an empty element; yank it off + { + if( (sym_tab[hv] = eptr->next) != NULL ) { // bump list to next if not the only thing here + sym_tab[hv]->prev = NULL; // new head + } + eptr->next = NULL; // take no chances + + if( eptr->class && eptr->name ) { // class 0 entries are numeric, so name is NOT a pointer + free( (void *) eptr->name ); + } + + free( eptr ); + + table->deaths++; + table->inhabitants--; + } +} + /* Determine if these are the same. */ @@ -143,8 +180,8 @@ static inline int same( unsigned int c1, unsigned int c2, const char *s1, const much the same. */ static int putin( Sym_tab *table, const char *name, unsigned int class, void *val ) { - Sym_ele *eptr; /* pointer into hash table */ - Sym_ele **sym_tab; /* pointer into hash table */ + Sym_ele *eptr; /* pointer into hash table */ + Sym_ele **sym_tab; /* pointer into hash table */ int hv; /* hash value */ int rc = 0; /* assume it existed */ uint64_t nkey = 0; // numeric key if class == 0 @@ -160,7 +197,7 @@ static int putin( Sym_tab *table, const char *name, unsigned int class, void *va for( eptr=sym_tab[hv]; eptr && eptr->nkey != nkey; eptr=eptr->next ); } - if( ! eptr ) { // not found above, so add + if( ! eptr ) { // not found above, so add rc++; table->inhabitants++; @@ -205,9 +242,11 @@ extern void rmr_sym_clear( void *vtable ) table = (Sym_tab *) vtable; sym_tab = table->symlist; - for( i = 0; i < table->size; i++ ) - while( sym_tab[i] ) - del_ele( table, i, sym_tab[i] ); + for( i = 0; i < table->size; i++ ) { + while( sym_tab[i] ) { + del_head_ele( table, i ); // delete the head element (and keep bloody sonar from claiming use after free) + } + } } /* @@ -464,17 +503,20 @@ extern void rmr_sym_foreach_class( void *vst, unsigned int class, void (* user_f Sym_ele **list; Sym_ele *se; Sym_ele *next; /* allows user to delete the node(s) we return */ - int i; + int i; st = (Sym_tab *) vst; - if( st && (list = st->symlist) != NULL && user_fun != NULL ) - for( i = 0; i < st->size; i++ ) - for( se = list[i]; se; se = next ) /* using next allows user to delet via this */ - { - next = se->next; + if( st && (list = st->symlist) != NULL && user_fun != NULL ) { + for( i = 0; i < st->size; i++ ) { + se = list[i]; + while( se ) { + next = se->next; // allow callback to delete from the list w/o borking us if( class == se->class ) { user_fun( st, se, se->name, se->val, user_data ); } + se = next; } + } + } }