Fixes after coverity static analysis

This commit is contained in:
Olof hagsand 2024-03-03 17:05:36 +01:00
parent d94b4f5b7c
commit a1badc312e
24 changed files with 148 additions and 70 deletions

View file

@ -299,6 +299,8 @@ clixon_client_connect_netconf(clixon_handle h,
}
retval = 0;
done:
if (argv)
free(argv);
return retval;
}
@ -340,6 +342,8 @@ clixon_client_connect_ssh(clixon_handle h,
}
retval = 0;
done:
if (argv)
free(argv);
return retval;
}

View file

@ -743,6 +743,8 @@ xmldb_print(clixon_handle h,
}
retval = 0;
done:
if (keys)
free(keys);
return retval;
}

View file

@ -142,6 +142,7 @@ xml_copy_bottom_recurse(cxobj *x0t,
cxobj *x0p = NULL;
cxobj *x1p = NULL;
cxobj *x1 = NULL;
int x1malloc = 0;
cxobj *x1a = NULL;
cxobj *x0a = NULL;
cxobj *x0k;
@ -168,6 +169,7 @@ xml_copy_bottom_recurse(cxobj *x0t,
if (x1 == NULL){ /* If not, create it and copy it one level only */
if ((x1 = xml_new(xml_name(x0), x1p, CX_ELMNT)) == NULL)
goto done;
x1malloc++;
if (xml_copy_one(x0, x1) < 0)
goto done;
/* Copy all attributes */
@ -200,9 +202,12 @@ xml_copy_bottom_recurse(cxobj *x0t,
}
}
*x1pp = x1;
x1 = NULL;
ok:
retval = 0;
done:
if (x1 && x1malloc)
xml_free(x1);
return retval;
}

View file

@ -118,8 +118,8 @@ split_path(char *path,
{
int retval = -1;
size_t allocated = PATH_CHUNKS;
char *work; /* don't modify the original copy */
char **list;
char *work = NULL; /* don't modify the original copy */
char **list = NULL;
size_t len = 0;
char *ptr;
char *new_element;
@ -136,9 +136,7 @@ split_path(char *path,
list[len++] = new_element;
ptr++;
}
ptr = strtok(ptr, "/");
while (ptr != NULL) {
if (len > allocated) {
/* we've run out of space, allocate a bigger list */
@ -146,20 +144,20 @@ split_path(char *path,
if ((list = realloc(list, allocated * sizeof(char *))) == NULL)
goto done;
}
if ((new_element = strdup(ptr)) == NULL)
goto done;
list[len++] = new_element;
ptr = strtok(NULL, "/");
}
*plist = list;
list = NULL;
*plist_len = len;
free(work);
retval = 0;
done:
if (list)
free(list);
if (work)
free(work);
return retval;
}
@ -236,13 +234,15 @@ add_peer_node(dispatcher_entry_t *node,
new_node->peer_head = new_node;
return new_node;
} else {
}
else {
/* possibly adding to the list */
/* search for existing, or get tail end of list */
eptr = node->peer_head;
while (eptr->peer != NULL) {
if (strcmp(eptr->node_name, name) == 0) {
free(new_node);
return eptr;
}
eptr = eptr->peer;
@ -250,6 +250,7 @@ add_peer_node(dispatcher_entry_t *node,
// if eptr->node_name == name, we done
if (strcmp(eptr->node_name, name) == 0) {
free(new_node);
return eptr;
}
@ -275,7 +276,6 @@ add_peer_node(dispatcher_entry_t *node,
* @retval pointer Pointer to head of children list
* @retval NULL Error
*/
static dispatcher_entry_t *
add_child_node(dispatcher_entry_t *node,
char *name)
@ -382,13 +382,14 @@ int
dispatcher_register_handler(dispatcher_entry_t **root,
dispatcher_definition *x)
{
int retval = -1;
char **split_path_list = NULL;
size_t split_path_len = 0;
dispatcher_entry_t *ptr;
if (*x->dd_path != '/') {
errno = EINVAL;
return -1;
goto done;
}
/*
@ -396,7 +397,7 @@ dispatcher_register_handler(dispatcher_entry_t **root,
* up to create the elements of the dispatcher table
*/
if (split_path(x->dd_path, &split_path_list, &split_path_len) < 0)
return -1;
goto done;
/*
* the first element is always a peer to the top level
@ -411,7 +412,7 @@ dispatcher_register_handler(dispatcher_entry_t **root,
for (size_t i = 1; i < split_path_len; i++) {
if ((ptr = add_child_node(ptr, split_path_list[i])) == NULL)
return -1;
goto done;
}
/* when we get here, ptr points at last entry added */
@ -420,8 +421,9 @@ dispatcher_register_handler(dispatcher_entry_t **root,
/* clean up */
split_path_free(split_path_list, split_path_len);
return 0;
retval = 0;
done:
return retval;
}
/*! Call the handler and all its descendant handlers

View file

@ -501,7 +501,7 @@ clixon_err_restore(void* handle)
if ((es = (struct err_state *)handle) != NULL){
_err_category = es->es_category;
_err_subnr = es->es_subnr;
strncpy(_err_reason, es->es_reason, ERR_STRLEN);
strncpy(_err_reason, es->es_reason, ERR_STRLEN-1);
free(es);
}
return 0;

View file

@ -609,7 +609,7 @@ clixon_msg_rcv11(int s,
int eom = 0;
cxobj *xtop = NULL;
cxobj *xerr = NULL;
sigset_t oldsigset;
sigset_t oldsigset = {0,};
struct sigaction oldsigaction[32] = {{{0,},},};
if ((cbmsg = cbuf_new()) == NULL){

View file

@ -601,6 +601,7 @@ clicon_rpc_get_config(clixon_handle h,
goto done;
xml_sort(xd); /* Ensure attr is first */
*xt = xd;
xd = NULL;
}
retval = 0;
done:
@ -614,6 +615,8 @@ clicon_rpc_get_config(clixon_handle h,
xml_free(xret);
if (msg)
free(msg);
if (xd)
xml_free(xd);
return retval;
}
@ -1271,6 +1274,7 @@ clicon_rpc_get_pageable_list(clixon_handle h,
goto done;
xml_sort(xd); /* Ensure attr is first */
*xt = xd;
xd = NULL;
}
retval = 0;
done:
@ -1284,6 +1288,8 @@ clicon_rpc_get_pageable_list(clixon_handle h,
xml_free(xret);
if (msg)
free(msg);
if (xd)
xml_free(xd);
return retval;
}

View file

@ -115,6 +115,21 @@ stream_find(clixon_handle h,
return NULL;
}
/*! Delete event stream core components
*
* @param[in] es Event notification stream structure
*/
static int
stream_delete(event_stream_t *es)
{
if (es->es_name)
free(es->es_name);
if (es->es_description)
free(es->es_description);
free(es);
return 0;
}
/*! Add notification event stream
*
* @param[in] h Clixon handle
@ -133,7 +148,7 @@ stream_add(clixon_handle h,
struct timeval *retention)
{
int retval = -1;
event_stream_t *es;
event_stream_t *es = NULL;
if ((es = stream_find(h, name)) != NULL)
goto ok;
@ -154,9 +169,12 @@ stream_add(clixon_handle h,
if (retention)
es->es_retention = *retention;
clicon_stream_append(h, es);
es = NULL;
ok:
retval = 0;
done:
if (es)
stream_delete(es);
return retval;
}
@ -179,10 +197,6 @@ stream_delete_all(clixon_handle h,
while ((es = clicon_stream(h)) != NULL){
DELQ(es, head, event_stream_t *);
clicon_stream_set(h, head);
if (es->es_name)
free(es->es_name);
if (es->es_description)
free(es->es_description);
while ((ss = es->es_subscription) != NULL){
if (stream_ss_rm(h, es, ss, force) < 0)
goto done;
@ -193,7 +207,8 @@ stream_delete_all(clixon_handle h,
xml_free(r->r_xml);
free(r);
}
free(es);
if (stream_delete(es) < 0)
goto done;
}
retval = 0;
done:
@ -857,7 +872,7 @@ stream_replay_trigger(clixon_handle h,
{
int retval = -1;
struct timeval now;
struct replay_arg *ra;
struct replay_arg *ra = NULL;
if ((ra = malloc(sizeof(*ra))) == NULL){
clixon_err(OE_UNIX, errno, "malloc");
@ -875,8 +890,11 @@ stream_replay_trigger(clixon_handle h,
if (clixon_event_reg_timeout(now, stream_replay_cb, ra,
"create-subscribtion stream replay") < 0)
goto done;
ra = NULL;
retval = 0;
done:
if (ra)
free(ra);
return retval;
}

View file

@ -147,7 +147,7 @@ clixon_strsep2(char *str,
char *s1;
char *s2;
int nr = 0;
char *ptr;
char *ptr = NULL;
int i;
s1 = str;
@ -840,7 +840,7 @@ uri_str2cvec(char *string,
char *s;
char *s0 = NULL;;
char *val; /* value */
char *valu; /* unescaped value */
char *valu = NULL; /* unescaped value */
char *snext; /* next element in string */
cvec *cvv = NULL;
cg_var *cv;
@ -883,7 +883,10 @@ uri_str2cvec(char *string,
s++;
cv_name_set(cv, s);
cv_string_set(cv, valu);
free(valu); valu = NULL;
if (valu) {
free(valu);
valu = NULL;
}
}
else{
if (strlen(s)){
@ -901,6 +904,8 @@ uri_str2cvec(char *string,
*cvp = cvv;
if (s0)
free(s0);
if (valu)
free(valu);
return retval;
err:
if (cvv){

View file

@ -1736,9 +1736,9 @@ xml_enumerate_get(cxobj *x)
/*! Get the first sub-node which is an XML body.
*
* @param[in] xn xml tree node
* @retval The returned body as a pointer to the name string
* @retval NULL if no such node or no body in found node
* @param[in] xn XML tree node
* @retval body The returned body as a pointer to the name string
* @retval NULL If no such node or no body in found node
* Note, make a copy of the return value to use it properly
* @see xml_find_body
* Explaining picture:

View file

@ -86,11 +86,11 @@ xml_default_create1(yang_stmt *y,
cxobj *xt,
cxobj **xcp)
{
int retval = -1;
char *namespace;
char *prefix;
int ret;
cxobj *xc = NULL;
int retval = -1;
char *namespace;
char *prefix;
int ret;
cxobj *xc = NULL;
if ((xc = xml_new(yang_argument_get(y), NULL, CX_ELMNT)) == NULL)
goto done;
@ -116,8 +116,11 @@ xml_default_create1(yang_stmt *y,
if (xml_addsub(xt, xc) < 0)
goto done;
*xcp = xc;
xc = NULL;
retval = 0;
done:
if (xc)
xml_free(xc);
return retval;
}

View file

@ -1045,8 +1045,10 @@ xpath_traverse_canonical(xpath_tree *xs,
if (xml_nsctx_add(nsc1, prefix1, namespace) < 0)
goto done;
if (prefix0==NULL || strcmp(prefix0, prefix1) != 0){
if (xs->xs_s0)
if (xs->xs_s0){
free(xs->xs_s0);
xs->xs_s0 = NULL;
}
if ((xs->xs_s0 = strdup(prefix1)) == NULL){
clixon_err(OE_UNIX, errno, "strdup");
goto done;

View file

@ -621,8 +621,11 @@ xp_logop(xp_ctx *xc1,
goto done;
}
*xrp = xr;
xr = NULL;
retval = 0;
done:
if (xr)
ctx_free(xr);
return retval;
}
@ -684,8 +687,11 @@ xp_numop(xp_ctx *xc1,
goto done;
}
*xrp = xr;
xr = NULL;
retval = 0;
done:
if (xr)
ctx_free(xr);
return retval;
}
@ -1111,8 +1117,11 @@ xp_union(xp_ctx *xc1,
goto done;
}
*xrp = xr;
xr = NULL;
retval = 0;
done:
if (xr)
ctx_free(xr);
return retval;
}

View file

@ -375,6 +375,8 @@ xp_function_derived_from(xp_ctx *xc,
ctx_free(xr0);
if (xr1)
ctx_free(xr1);
if (xr)
ctx_free(xr);
if (identity)
free(identity);
return retval;
@ -568,12 +570,15 @@ xp_function_name(xp_ctx *xc,
break;
}
*xrp = xr;
xr = NULL;
retval = 0;
done:
if (xr0)
ctx_free(xr0);
if (s0)
free(s0);
if (xr)
ctx_free(xr);
return retval;
}

View file

@ -1014,18 +1014,10 @@ ys_replace(yang_stmt *yorig,
{
int retval = -1;
yang_stmt *yp; /* parent */
yang_stmt *yc; /* child */
yp = yang_parent_get(yorig);
/* Remove old yangs all children */
yc = NULL;
while ((yc = yn_each(yorig, yc)) != NULL)
ys_free(yc);
if (yorig->ys_stmt){
free(yorig->ys_stmt);
yorig->ys_stmt = NULL;
yorig->ys_len = 0;
}
ys_freechildren(yorig);
ys_free1(yorig, 0); /* Remove all in yold except the actual object */
if (ys_cp(yorig, yfrom) < 0)
goto done;