[Asterisk-code-review] json: Check party id name, number, subaddresses for UTF-8. (asterisk[master])

Richard Mudgett asteriskteam at digium.com
Thu Oct 13 16:03:41 CDT 2016


Richard Mudgett has uploaded a new change for review.

  https://gerrit.asterisk.org/4102

Change subject: json: Check party id name, number, subaddresses for UTF-8.
......................................................................

json: Check party id name, number, subaddresses for UTF-8.

* Updated unit test as ast_json_name_number() is now NULL tolerant.

ASTERISK-26466 #close
Reported by: Richard Mudgett

Change-Id: I7d4e14194f8f81f24a1dc34d1b8602c0950265a6
---
M main/json.c
M tests/test_json.c
2 files changed, 36 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/02/4102/1

diff --git a/main/json.c b/main/json.c
index 96d5df5..7eb6cc2 100644
--- a/main/json.c
+++ b/main/json.c
@@ -842,16 +842,16 @@
 struct ast_json *ast_json_name_number(const char *name, const char *number)
 {
 	return ast_json_pack("{s: s, s: s}",
-			     "name", name,
-			     "number", number);
+		"name", ast_json_utf8_check(name) ? name : "",
+		"number", ast_json_utf8_check(number) ? number : "");
 }
 
 struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority)
 {
 	return ast_json_pack("{s: o, s: o, s: o}",
-			     "context", context ? ast_json_string_create(context) : ast_json_null(),
-			     "exten", exten ? ast_json_string_create(exten) : ast_json_null(),
-			     "priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null());
+		"context", context ? ast_json_string_create(context) : ast_json_null(),
+		"exten", exten ? ast_json_string_create(exten) : ast_json_null(),
+		"priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null());
 }
 
 struct ast_json *ast_json_timeval(const struct timeval tv, const char *zone)
@@ -942,7 +942,7 @@
 		return NULL;
 	}
 	return ast_json_pack("{s: s, s: i, s: i, s: s}",
-		"number", number->str,
+		"number", ast_json_utf8_check(number->str) ? number->str : "",
 		"plan", number->plan,
 		"presentation", number->presentation,
 		"presentation_txt", ast_describe_caller_presentation(number->presentation));
@@ -954,7 +954,7 @@
 		return NULL;
 	}
 	return ast_json_pack("{s: s, s: s, s: i, s: s}",
-		"name", name->str,
+		"name", ast_json_utf8_check(name->str) ? name->str : "",
 		"character_set", ast_party_name_charset_describe(name->char_set),
 		"presentation", name->presentation,
 		"presentation_txt", ast_describe_caller_presentation(name->presentation));
@@ -966,7 +966,7 @@
 		return NULL;
 	}
 	return ast_json_pack("{s: s, s: i, s: b}",
-		"subaddress", subaddress->str,
+		"subaddress", ast_json_utf8_check(subaddress->str) ? subaddress->str : "",
 		"type", subaddress->type,
 		"odd", subaddress->odd_even_indicator);
 }
@@ -986,17 +986,20 @@
 	}
 
 	/* Party number */
-	if (party->number.valid && ast_json_object_set(json_party_id, "number", json_party_number(&party->number))) {
+	if (party->number.valid
+		&& ast_json_object_set(json_party_id, "number", json_party_number(&party->number))) {
 		return NULL;
 	}
 
 	/* Party name */
-	if (party->name.valid && ast_json_object_set(json_party_id, "name", json_party_name(&party->name))) {
+	if (party->name.valid
+		&& ast_json_object_set(json_party_id, "name", json_party_name(&party->name))) {
 		return NULL;
 	}
 
 	/* Party subaddress */
-	if (party->subaddress.valid && ast_json_object_set(json_party_id, "subaddress", json_party_subaddress(&party->subaddress))) {
+	if (party->subaddress.valid
+		&& ast_json_object_set(json_party_id, "subaddress", json_party_subaddress(&party->subaddress))) {
 		return NULL;
 	}
 
diff --git a/tests/test_json.c b/tests/test_json.c
index 9155781..7362a61 100644
--- a/tests/test_json.c
+++ b/tests/test_json.c
@@ -1598,11 +1598,26 @@
 	return AST_TEST_PASS;
 }
 
+static int test_name_number(const char *name, const char *number)
+{
+	int res;
+	struct ast_json *uut;
+	struct ast_json *expected;
+
+	expected = ast_json_pack("{s: s, s: s}",
+		"name", name ?: "",
+		"number", number ?: "");
+	uut = ast_json_name_number(name, number);
+
+	res = ast_json_equal(expected, uut);
+
+	ast_json_unref(expected);
+	ast_json_unref(uut);
+	return res;
+}
+
 AST_TEST_DEFINE(json_test_name_number)
 {
-	RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
-	RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
-
 	switch (cmd) {
 	case TEST_INIT:
 		info->name = "name_number";
@@ -1614,15 +1629,10 @@
 		break;
 	}
 
-	ast_test_validate(test, NULL == ast_json_name_number("name", NULL));
-	ast_test_validate(test, NULL == ast_json_name_number(NULL, "1234"));
-	ast_test_validate(test, NULL == ast_json_name_number(NULL, NULL));
-
-	expected = ast_json_pack("{s: s, s: s}",
-				 "name", "Jenny",
-				 "number", "867-5309");
-	uut = ast_json_name_number("Jenny", "867-5309");
-	ast_test_validate(test, ast_json_equal(expected, uut));
+	ast_test_validate(test, test_name_number("name", NULL));
+	ast_test_validate(test, test_name_number(NULL, "1234"));
+	ast_test_validate(test, test_name_number(NULL, NULL));
+	ast_test_validate(test, test_name_number("Jenny", "867-5309"));
 
 	return AST_TEST_PASS;
 }

-- 
To view, visit https://gerrit.asterisk.org/4102
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7d4e14194f8f81f24a1dc34d1b8602c0950265a6
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>



More information about the asterisk-code-review mailing list