Browse Source

ctl: free rpc struct in case of error

(cherry picked from commit 4a71d3c9e6a4f92ef33ce8e62372b2c4dba62a56)
(cherry picked from commit 5024235f876df4b3239ef746de700ade362cc7b2)
(cherry picked from commit 5e836af2ff198481bde0485a315bbfdf39043f9d)
Daniel-Constantin Mierla 7 months ago
parent
commit
dffd625bf7
1 changed files with 14 additions and 9 deletions
  1. 14 9
      src/modules/ctl/binrpc_run.c

+ 14 - 9
src/modules/ctl/binrpc_run.c

@@ -1025,7 +1025,7 @@ static int rpc_add(struct binrpc_ctx *ctx, char *fmt, ...)
 	int err;
 	str st;
 	str *sp;
-	struct rpc_struct_l *rs;
+	struct rpc_struct_l *rs = NULL;
 	str null_value = str_init("<null string>");
 	double d;
 
@@ -1069,12 +1069,14 @@ static int rpc_add(struct binrpc_ctx *ctx, char *fmt, ...)
 				if(err < 0)
 					goto error_add;
 				rs = new_rpc_struct();
-				if(rs == 0)
+				if(rs == NULL)
 					goto error_mem;
 				rs->offset = binrpc_pkt_len(&ctx->out.pkt);
 				err = binrpc_end_struct(&ctx->out.pkt);
-				if(err < 0)
+				if(err < 0) {
+					ctl_free(rs);
 					goto error_add;
+				}
 				clist_append(&ctx->out.structs, rs, next, prev);
 				*(va_arg(ap, void **)) = rs;
 				break;
@@ -1180,7 +1182,7 @@ static int rpc_struct_add(struct rpc_struct_l *s, char *fmt, ...)
 	va_list ap;
 	int err;
 	struct binrpc_val avp;
-	struct rpc_struct_l *rs;
+	struct rpc_struct_l *rs = NULL;
 	str *sp;
 	str null_value = str_init("<null string>");
 
@@ -1226,7 +1228,7 @@ static int rpc_struct_add(struct rpc_struct_l *s, char *fmt, ...)
 					goto error_add;
 				}
 				rs = new_rpc_struct();
-				if(rs == 0) {
+				if(rs == NULL) {
 					LM_ERR("not enough memory (%c)\n", *fmt);
 					goto error_mem;
 				}
@@ -1234,6 +1236,7 @@ static int rpc_struct_add(struct rpc_struct_l *s, char *fmt, ...)
 				err = binrpc_end_struct(&s->pkt);
 				if(err < 0) {
 					LM_ERR("failed to end struct (%c)\n", *fmt);
+					ctl_free(rs);
 					goto error_add;
 				}
 				clist_append(&s->substructs, rs, next, prev);
@@ -1289,8 +1292,8 @@ static int rpc_array_add(struct rpc_struct_l *s, char *fmt, ...)
 	va_list ap;
 	int err;
 	str st;
-	str *sp;
-	struct rpc_struct_l *rs;
+	str *sp = NULL;
+	struct rpc_struct_l *rs = NULL;
 	str null_value = str_init("<null string>");
 	double d;
 
@@ -1334,12 +1337,14 @@ static int rpc_array_add(struct rpc_struct_l *s, char *fmt, ...)
 				if(err < 0)
 					goto error_add;
 				rs = new_rpc_struct();
-				if(rs == 0)
+				if(rs == NULL)
 					goto error_mem;
 				rs->offset = binrpc_pkt_len(&s->pkt);
 				err = binrpc_end_struct(&s->pkt);
-				if(err < 0)
+				if(err < 0) {
+					ctl_free(rs);
 					goto error_add;
+				}
 				clist_append(&s->substructs, rs, next, prev);
 				*(va_arg(ap, void **)) = rs;
 				break;