Browse code

core: rvalue - function to destroy content of rval without freeing it

- needed in case of rval reuse in rve_replace_with_val()

Daniel-Constantin Mierla authored on 06/04/2021 13:23:21
Showing 1 changed files
... ...
@@ -112,10 +112,7 @@ inline static void rval_force_clean(struct rvalue* rv)
112 112
 
113 113
 
114 114
 
115
-/** frees a rval returned by rval_new(), rval_convert() or rval_expr_eval().
116
- *   Note: it will be freed only when refcnt reaches 0
117
- */
118
-void rval_destroy(struct rvalue* rv)
115
+static inline void rval_destroy_helper(struct rvalue* rv, int allocated)
119 116
 {
120 117
 	if (rv && rv_unref(rv)){
121 118
 		rval_force_clean(rv);
... ...
@@ -126,13 +123,38 @@ void rval_destroy(struct rvalue* rv)
126 123
 			regfree(rv->v.re.regex);
127 124
 		}
128 125
 		if (rv->flags & RV_RV_ALLOCED_F){
129
-			pkg_free(rv);
126
+			if(likely(allocated)) {
127
+				pkg_free(rv);
128
+			} else {
129
+				/* not expected to be allocated */
130
+				abort(); /* abort, otherwise is lost - find bugs quicker */
131
+			}
130 132
 		}
131 133
 	}
132 134
 }
133 135
 
134 136
 
135 137
 
138
+/** frees a rval returned by rval_new(), rval_convert() or rval_expr_eval().
139
+ *   Note: it will be freed only when refcnt reaches 0
140
+ */
141
+void rval_destroy(struct rvalue* rv)
142
+{
143
+	rval_destroy_helper(rv, 1);
144
+}
145
+
146
+
147
+
148
+/** frees content of rval which is not allocated, otherwise aborts.
149
+ *   Note: it will be freed only when refcnt reaches 0
150
+ */
151
+void rval_destroy_content(struct rvalue* rv)
152
+{
153
+	rval_destroy_helper(rv, 0);
154
+}
155
+
156
+
157
+
136 158
 void rval_clean(struct rvalue* rv)
137 159
 {
138 160
 	if (rv_unref(rv))
... ...
@@ -1523,7 +1545,7 @@ inline static int int_strop1(int* res, enum rval_expr_op op, str* s1)
1523 1545
 }
1524 1546
 
1525 1547
 
1526
-
1548
+#if 0
1527 1549
 /** integer operation: ret= op v (returns a rvalue).
1528 1550
  * @return rvalue on success, 0 on error
1529 1551
  */
... ...
@@ -1619,7 +1641,7 @@ error:
1619 1641
 	rval_destroy(rv2);
1620 1642
 	return 0;
1621 1643
 }
1622
-
1644
+#endif /* #if 0 */
1623 1645
 
1624 1646
 
1625 1647
 /** string add operation: ret= l . r (returns a rvalue).
... ...
@@ -3024,7 +3046,7 @@ static int rve_replace_with_val(struct rval_expr* rve, enum rval_type type,
3024 3046
 			refcnt=rve->left.rval.refcnt;
3025 3047
 			abort(); /* find bugs quicker -- andrei */
3026 3048
 		}
3027
-		rval_destroy(&rve->left.rval);
3049
+		rval_destroy_content(&rve->left.rval);
3028 3050
 	}
3029 3051
 	rval_init(&rve->left.rval, type, v, flags);
3030 3052
 	rve->left.rval.refcnt=refcnt;
... ...
@@ -3096,7 +3118,7 @@ static int fix_match_rve(struct rval_expr* rve)
3096 3118
 	/* fixup the right side (RE) */
3097 3119
 	if (rve_is_constant(rve->right.rve)){
3098 3120
 		if ((rve_guess_type(rve->right.rve)!=RV_STR)){
3099
-			LM_ERR("fixup failure(%d,%d-%d,%d): left side of  =~ is not string"
3121
+			LM_ERR("fixup failure(%d,%d-%d,%d): right side of  =~ is not string"
3100 3122
 					" (%d,%d)\n",   rve->fpos.s_line, rve->fpos.s_col,
3101 3123
 									rve->fpos.e_line, rve->fpos.e_col,
3102 3124
 									rve->right.rve->fpos.s_line,
... ...
@@ -3104,7 +3126,7 @@ static int fix_match_rve(struct rval_expr* rve)
3104 3126
 			goto error;
3105 3127
 		}
3106 3128
 		if ((rv=rval_expr_eval(0, 0, rve->right.rve))==0){
3107
-			LM_ERR("fixup failure(%d,%d-%d,%d):  bad RE expression\n",
3129
+			LM_ERR("fixup failure(%d,%d-%d,%d): bad RE expression\n",
3108 3130
 					rve->right.rve->fpos.s_line, rve->right.rve->fpos.s_col,
3109 3131
 					rve->right.rve->fpos.e_line, rve->right.rve->fpos.e_col);
3110 3132
 			goto error;