[sr-dev] git:4.1: json: unreference the json object in json_get_field()

Daniel-Constantin Mierla miconda at gmail.com
Thu Nov 6 12:15:41 CET 2014


Module: sip-router
Branch: 4.1
Commit: dd3a4e49c8f7b9be590dca5db7ffde4ab26d98c4
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=dd3a4e49c8f7b9be590dca5db7ffde4ab26d98c4

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Wed Nov  5 14:29:34 2014 +0100

json: unreference the json object in json_get_field()

- the parsed json object has to be freed
- reported by dongwflj at 163.com

(cherry picked from commit def8d2600422a442b971c204df98594b0dfdaacd)
(cherry picked from commit 8eea9aa8faafd5a248a00caaba43379fc7b52423)

---

 modules/json/json_funcs.c |   21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/modules/json/json_funcs.c b/modules/json/json_funcs.c
index a75704b..7661e1e 100644
--- a/modules/json/json_funcs.c
+++ b/modules/json/json_funcs.c
@@ -33,10 +33,12 @@
 
 int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst)
 {
-  str json_s;
-  str field_s;
-  pv_spec_t *dst_pv;
-  pv_value_t dst_val;
+	str json_s;
+	str field_s;
+	pv_spec_t *dst_pv;
+	pv_value_t dst_val;
+	char *value;
+	struct json_object *j = NULL;
 
 	if (fixup_get_svalue(msg, (gparam_p)json, &json_s) != 0) {
 		LM_ERR("cannot get json string value\n");
@@ -47,22 +49,25 @@ int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst)
 		LM_ERR("cannot get field string value\n");
 		return -1;
 	}
-	
+
 	dst_pv = (pv_spec_t *)dst;
-	
-	struct json_object *j = json_tokener_parse(json_s.s);
+
+
+	j = json_tokener_parse(json_s.s);
 
 	if (is_error(j)) {
 		LM_ERR("empty or invalid JSON\n");
+		if(j!=NULL) json_object_put(j);
 		return -1;
 	}
 
-	char *value = (char*)json_object_to_json_string(json_object_object_get(j, field_s.s));
+	value = (char*)json_object_to_json_string(json_object_object_get(j, field_s.s));
 
 	dst_val.rs.s = value;
 	dst_val.rs.len = strlen(value);
 	dst_val.flags = PV_VAL_STR;
 	dst_pv->setf(msg, &dst_pv->pvp, (int)EQ_T, &dst_val);
 
+	if(j!=NULL) json_object_put(j);
 	return 1;
 }




More information about the sr-dev mailing list