diff mbox

[06/23] ndef: Validate text data in WKT Text records

Message ID 20170615182516.4508-7-mgreer@animalcreek.com (mailing list archive)
State Accepted
Delegated to: Samuel Ortiz
Headers show

Commit Message

Mark Greer June 15, 2017, 6:24 p.m. UTC
Ensure that the text encodings in WKT Text records are valid.

Signed-off-by: Mark Greer <mgreer@animalcreek.com>
---
 src/ndef.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
diff mbox

Patch

diff --git a/src/ndef.c b/src/ndef.c
index 38dcf72..fb17be9 100644
--- a/src/ndef.c
+++ b/src/ndef.c
@@ -1149,8 +1149,9 @@  static struct near_ndef_text_payload *
 parse_text_payload(uint8_t *payload, uint32_t length)
 {
 	struct near_ndef_text_payload *text_payload = NULL;
-	uint8_t status, lang_length;
+	uint8_t status, lang_length, len, *txt, *g_str;
 	uint32_t offset;
+	gboolean valid;
 
 	DBG("");
 
@@ -1185,9 +1186,26 @@  parse_text_payload(uint8_t *payload, uint32_t length)
 
 	offset += lang_length;
 
-	if ((length - lang_length - 1) > 0) {
-		text_payload->data = g_strndup((char *)(payload + offset),
-					length - lang_length - 1);
+	len = length - lang_length - 1;
+
+	if (len > 0) {
+		txt = payload + offset;
+
+		if (status)
+			g_str = g_utf16_to_utf8((gunichar2 *)txt, len, NULL,
+						NULL, NULL);
+		else
+			g_str = txt;
+
+		valid = g_utf8_validate(g_str, len, NULL);
+
+		if (status)
+			g_free(g_str);
+
+		if (!valid)
+			goto fail;
+
+		text_payload->data = g_strndup(txt, len);
 	} else {
 		text_payload->data = NULL;
 	}