Browse code

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

- needed in case of rval reuse in rve_replace_with_val()

(cherry picked from commit e770e0aa6889f001a3f7f2644202bf106f6559da)

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))
... ...
@@ -1511,7 +1533,7 @@ inline static int int_strop1(int* res, enum rval_expr_op op, str* s1)
1511 1533
 }
1512 1534
 
1513 1535
 
1514
-
1536
+#if 0
1515 1537
 /** integer operation: ret= op v (returns a rvalue).
1516 1538
  * @return rvalue on success, 0 on error
1517 1539
  */
... ...
@@ -1607,7 +1629,7 @@ error:
1607 1629
 	rval_destroy(rv2);
1608 1630
 	return 0;
1609 1631
 }
1610
-
1632
+#endif /* #if 0 */
1611 1633
 
1612 1634
 
1613 1635
 /** string add operation: ret= l . r (returns a rvalue).
... ...
@@ -2940,7 +2962,7 @@ static int rve_replace_with_val(struct rval_expr* rve, enum rval_type type,
2940 2962
 			refcnt=rve->left.rval.refcnt;
2941 2963
 			abort(); /* find bugs quicker -- andrei */
2942 2964
 		}
2943
-		rval_destroy(&rve->left.rval);
2965
+		rval_destroy_content(&rve->left.rval);
2944 2966
 	}
2945 2967
 	rval_init(&rve->left.rval, type, v, flags);
2946 2968
 	rve->left.rval.refcnt=refcnt;
... ...
@@ -3012,7 +3034,7 @@ static int fix_match_rve(struct rval_expr* rve)
3012 3034
 	/* fixup the right side (RE) */
3013 3035
 	if (rve_is_constant(rve->right.rve)){
3014 3036
 		if ((rve_guess_type(rve->right.rve)!=RV_STR)){
3015
-			LM_ERR("fixup failure(%d,%d-%d,%d): left side of  =~ is not string"
3037
+			LM_ERR("fixup failure(%d,%d-%d,%d): right side of  =~ is not string"
3016 3038
 					" (%d,%d)\n",   rve->fpos.s_line, rve->fpos.s_col,
3017 3039
 									rve->fpos.e_line, rve->fpos.e_col,
3018 3040
 									rve->right.rve->fpos.s_line,
... ...
@@ -3020,7 +3042,7 @@ static int fix_match_rve(struct rval_expr* rve)
3020 3042
 			goto error;
3021 3043
 		}
3022 3044
 		if ((rv=rval_expr_eval(0, 0, rve->right.rve))==0){
3023
-			LM_ERR("fixup failure(%d,%d-%d,%d):  bad RE expression\n",
3045
+			LM_ERR("fixup failure(%d,%d-%d,%d): bad RE expression\n",
3024 3046
 					rve->right.rve->fpos.s_line, rve->right.rve->fpos.s_col,
3025 3047
 					rve->right.rve->fpos.e_line, rve->right.rve->fpos.e_col);
3026 3048
 			goto error;