From 00670456ffb4b08af3620a95c73601bc15d84eb9 Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Fri, 23 Sep 2011 17:21:15 -0400 Subject: sms: sanitize 8-bit data so that it is UTF8-clean When receiving a SMS message with raw 8-bit data, sanitize it by replacing non-ASCII characters with \xNN escape sequences. This prevents a problem further down the line where the body of the message is passed into DBus as a string, and DBus requires strings to be UTF-8. BUG=chrome-os-partner:5953 TEST=Run network_ModemManagerSMS.py with the PDU from this bug. Change-Id: Ic33a365f9a065c49a325e047e4c3f5e81450fa1f Reviewed-on: http://gerrit.chromium.org/gerrit/8232 Reviewed-by: Eric Shienbrood Tested-by: Nathan J. Williams Commit-Ready: Nathan J. Williams --- src/mm-sms-utils.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/mm-sms-utils.c b/src/mm-sms-utils.c index 3f56a644..1b32a796 100644 --- a/src/mm-sms-utils.c +++ b/src/mm-sms-utils.c @@ -13,6 +13,9 @@ * Copyright (C) 2011 Red Hat, Inc. */ +#include +#include + #include #include "mm-charsets.h" @@ -200,8 +203,22 @@ sms_decode_text (const guint8 *text, int len, SmsEncoding encoding, int bit_offs g_free (unpacked); } else if (encoding == MM_SMS_ENCODING_UCS2) utf8 = g_convert ((char *) text, len, "UTF8", "UCS-2BE", NULL, NULL, NULL); - else if (encoding == MM_SMS_ENCODING_8BIT) - utf8 = g_strndup ((const char *)text, len); + else if (encoding == MM_SMS_ENCODING_8BIT) { + /* DBus requires UTF-8 strings, so we have some sanitizing to do */ + char *p; + int i; + utf8 = g_malloc0 (4*len+1); /* Worst case: Every byte becomes "\xFF" */ + p = utf8; + for (i = 0 ; i < len ; i++) { + if (isascii (text[i]) && text[i] != '\0') + *p++ = text[i]; + else { + sprintf(p, "\\x%02x", text[i]); + p += 4; + } + } + *p = '\0'; + } else utf8 = g_strdup (""); -- cgit v1.2.3-70-g09d2