From: swc Date: Thu, 19 Nov 2020 04:10:34 +0000 (+0900) Subject: Fixing minor exception checks X-Git-Tag: 4.4.6~1 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=f8623e7dc17184b3359a5de8a81a54bb963469c7;p=ric-plt%2Flib%2Frmr.git Fixing minor exception checks - uta_ring_free() updated to free resources - Exception Handling to avoid reference null_ptr - free_ctx() updated to free resources - err: added to safely handle resource free Issue-ID: RIC-673 Signed-off-by: swc Change-Id: I2645d6eddb230ca9cf2822c778c1b533e5da1f9b --- diff --git a/src/rmr/common/src/ring_static.c b/src/rmr/common/src/ring_static.c index da2a9bd..8e565fa 100644 --- a/src/rmr/common/src/ring_static.c +++ b/src/rmr/common/src/ring_static.c @@ -148,7 +148,15 @@ static void uta_ring_free( void* vr ) { if( (r = (ring_t*) vr) == NULL ) { return; } - + if( r->data ){ + free( r->data ); + } + if( r->rgate ){ + free( r->rgate ); + } + if( r->wgate ){ + free( r->wgate ); + } free( r ); } @@ -183,6 +191,7 @@ static inline void* uta_ring_extract( void* vr ) { if( r->rgate != NULL ) { // if lock exists we must honour it pthread_mutex_lock( r->rgate ); if( r->tail == r->head ) { // ensure ring didn't go empty while waiting + pthread_mutex_unlock( r->rgate ); return NULL; } } diff --git a/src/rmr/common/src/rt_generic_static.c b/src/rmr/common/src/rt_generic_static.c index 1d8d9ca..eb887a9 100644 --- a/src/rmr/common/src/rt_generic_static.c +++ b/src/rmr/common/src/rt_generic_static.c @@ -1218,10 +1218,11 @@ static route_table_t* rt_clone_space( uta_ctx_t* ctx, route_table_t* srt, route_ things.nalloc = 2048; things.nused = 0; things.things = (void **) malloc( sizeof( void * ) * things.nalloc ); - memset( things.things, 0, sizeof( sizeof( void * ) * things.nalloc ) ); things.names = (const char **) malloc( sizeof( char * ) * things.nalloc ); - memset( things.names, 0, sizeof( char * ) * things.nalloc ); - if( things.things == NULL ) { + if( things.things == NULL || things.names == NULL ){ + if( things.things != NULL) { free( things.things ); } + if( things.names != NULL) { free( things.names ); } + if( free_on_err ) { rmr_sym_free( nrt->hash ); free( nrt ); @@ -1230,6 +1231,8 @@ static route_table_t* rt_clone_space( uta_ctx_t* ctx, route_table_t* srt, route_ return nrt; } + memset( things.things, 0, sizeof( sizeof( void * ) * things.nalloc ) ); + memset( things.names, 0, sizeof( char * ) * things.nalloc ); sst = srt->hash; // convenience pointers (src symtab) nst = nrt->hash; diff --git a/src/rmr/si/src/rmr_si.c b/src/rmr/si/src/rmr_si.c index bbc47a5..5aaa778 100644 --- a/src/rmr/si/src/rmr_si.c +++ b/src/rmr/si/src/rmr_si.c @@ -82,8 +82,32 @@ Clean up a context. */ static void free_ctx( uta_ctx_t* ctx ) { - if( ctx && ctx->rtg_addr ) { - free( ctx->rtg_addr ); + if( ctx ) { + if( ctx->rtg_addr ){ + free( ctx->rtg_addr ); + } + uta_ring_free( ctx->mring ); + uta_ring_free( ctx->zcb_mring ); + if( ctx->chutes ){ + free( ctx->chutes ); + } + if( ctx->fd2ep ){ + rmr_sym_free( ctx->fd2ep ); + } + if( ctx->my_name ){ + free( ctx->my_name ); + } + if( ctx->my_ip ){ + free( ctx->my_ip ); + } + if( ctx->rtable ){ + rmr_sym_free( ctx->rtable->hash ); + free( ctx->rtable ); + } + if ( ctx->ephash ){ + free( ctx->ephash ); + } + free( ctx ); } } @@ -574,10 +598,15 @@ static void* init( char* uproto_port, int def_msg_size, int flags ) { proto_port = strdup( uproto_port ); // so we can modify it } - if( (ctx = (uta_ctx_t *) malloc( sizeof( uta_ctx_t ) )) == NULL ) { + if ( proto_port == NULL ){ errno = ENOMEM; return NULL; } + + if( (ctx = (uta_ctx_t *) malloc( sizeof( uta_ctx_t ) )) == NULL ) { + errno = ENOMEM; + goto err; + } memset( ctx, 0, sizeof( uta_ctx_t ) ); if( DEBUG ) rmr_vlog( RMR_VL_DEBUG, " rmr_init: allocating 266 rivers\n" ); @@ -614,8 +643,7 @@ static void* init( char* uproto_port, int def_msg_size, int flags ) { ctx->si_ctx = SIinitialise( SI_OPT_FG ); // FIX ME: si needs to streamline and drop fork/bg stuff if( ctx->si_ctx == NULL ) { rmr_vlog( RMR_VL_CRIT, "unable to initialise SI95 interface\n" ); - free_ctx( ctx ); - return NULL; + goto err; } if( (port = strchr( proto_port, ':' )) != NULL ) { @@ -649,8 +677,7 @@ static void* init( char* uproto_port, int def_msg_size, int flags ) { } else { if( (gethostname( wbuf, sizeof( wbuf ) )) != 0 ) { rmr_vlog( RMR_VL_CRIT, "rmr_init: cannot determine localhost name: %s\n", strerror( errno ) ); - free_ctx( ctx ); - return NULL; + goto err; } if( (tok = strchr( wbuf, '.' )) != NULL ) { *tok = 0; // we don't keep domain portion @@ -660,8 +687,8 @@ static void* init( char* uproto_port, int def_msg_size, int flags ) { ctx->my_name = (char *) malloc( sizeof( char ) * RMR_MAX_SRC ); if( snprintf( ctx->my_name, RMR_MAX_SRC, "%s:%s", wbuf, port ) >= RMR_MAX_SRC ) { // our registered name is host:port rmr_vlog( RMR_VL_CRIT, "rmr_init: hostname + port must be less than %d characters; %s:%s is not\n", RMR_MAX_SRC, wbuf, port ); - free( proto_port ); // some scanners complain that port is not freed; it CANNOT be - return NULL; + errno = EINVAL; + goto err; } if( (tok = getenv( ENV_NAME_ONLY )) != NULL ) { @@ -696,9 +723,7 @@ static void* init( char* uproto_port, int def_msg_size, int flags ) { snprintf( bind_info, sizeof( bind_info ), "%s:%s", interface, port ); // FIXME -- si only supports 0.0.0.0 by default if( (state = SIlistener( ctx->si_ctx, TCP_DEVICE, bind_info )) < 0 ) { rmr_vlog( RMR_VL_CRIT, "rmr_init: unable to start si listener for %s: %s\n", bind_info, strerror( errno ) ); - free( proto_port ); // some scanners might complain that port is not freed; it CANNOT be - free_ctx( ctx ); - return NULL; + goto err; } // finish all flag setting before threads to keep helgrind quiet @@ -714,9 +739,8 @@ static void* init( char* uproto_port, int def_msg_size, int flags ) { ctx->ephash = rmr_sym_alloc( 129 ); // host:port to ep symtab exists outside of any route table if( ctx->ephash == NULL ) { rmr_vlog( RMR_VL_CRIT, "rmr_init: unable to allocate ep hash\n" ); - free( proto_port ); // some scanners might complain that port is not freed; it CANNOT be - free_ctx( ctx ); - return NULL; + errno = ENOMEM; + goto err; } ctx->rtable = rt_clone_space( ctx, NULL, NULL, 0 ); // create an empty route table so that wormhole/rts calls can be used @@ -744,6 +768,11 @@ static void* init( char* uproto_port, int def_msg_size, int flags ) { free( proto_port ); return (void *) ctx; + +err: + free( proto_port ); + free_ctx( ctx ); + return NULL; } /* diff --git a/src/rmr/si/src/si95/siwait.c b/src/rmr/si/src/si95/siwait.c index c14c5a9..291b8aa 100644 --- a/src/rmr/si/src/si95/siwait.c +++ b/src/rmr/si/src/si95/siwait.c @@ -69,7 +69,10 @@ extern int SIwait( struct ginfo_blk *gptr ) { char *buf; char *ibuf; - ibuf = (char *) malloc( 2048 ); + if( ( ibuf = (char *) malloc( 2048 ) ) == NULL ) { + rmr_vlog( RMR_VL_WARN, "ibuf malloc fail\n" ); + return SI_ERROR; + } if( gptr->flags & GIF_SHUTDOWN ) { // cannot do if we should shutdown free( ibuf );