Browse code

json: unreference the json object in json_get_field()

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

Daniel-Constantin Mierla authored on 05/11/2014 13:29:34
Showing 1 changed files
... ...
@@ -33,10 +33,12 @@
33 33
 
34 34
 int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst)
35 35
 {
36
-  str json_s;
37
-  str field_s;
38
-  pv_spec_t *dst_pv;
39
-  pv_value_t dst_val;
36
+	str json_s;
37
+	str field_s;
38
+	pv_spec_t *dst_pv;
39
+	pv_value_t dst_val;
40
+	char *value;
41
+	struct json_object *j = NULL;
40 42
 
41 43
 	if (fixup_get_svalue(msg, (gparam_p)json, &json_s) != 0) {
42 44
 		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)
47 49
 		LM_ERR("cannot get field string value\n");
48 50
 		return -1;
49 51
 	}
50
-	
52
+
51 53
 	dst_pv = (pv_spec_t *)dst;
52
-	
53
-	struct json_object *j = json_tokener_parse(json_s.s);
54
+
55
+
56
+	j = json_tokener_parse(json_s.s);
54 57
 
55 58
 	if (is_error(j)) {
56 59
 		LM_ERR("empty or invalid JSON\n");
60
+		if(j!=NULL) json_object_put(j);
57 61
 		return -1;
58 62
 	}
59 63
 
60
-	char *value = (char*)json_object_to_json_string(json_object_object_get(j, field_s.s));
64
+	value = (char*)json_object_to_json_string(json_object_object_get(j, field_s.s));
61 65
 
62 66
 	dst_val.rs.s = value;
63 67
 	dst_val.rs.len = strlen(value);
64 68
 	dst_val.flags = PV_VAL_STR;
65 69
 	dst_pv->setf(msg, &dst_pv->pvp, (int)EQ_T, &dst_val);
66 70
 
71
+	if(j!=NULL) json_object_put(j);
67 72
 	return 1;
68 73
 }