X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Frmr%2Fcommon%2Fsrc%2Fsymtab.c;h=18cf60adf1aab9067d8c564b8a6c81d225413851;hb=9c923bcc9322c22220b574671c7b46f10008c614;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..18cf60a 100644 --- a/src/rmr/common/src/symtab.c +++ b/src/rmr/common/src/symtab.c @@ -96,7 +96,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 +127,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. */ @@ -205,9 +241,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) + } + } } /* @@ -468,13 +506,16 @@ extern void rmr_sym_foreach_class( void *vst, unsigned int class, void (* user_f 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; } + } + } }