diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2020-05-27 06:24:20 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2020-05-27 07:29:25 +0200 |
commit | 245692353729d1c1eb3490f01937e7d861c9d2bf (patch) | |
tree | b0de7dda8e4ffc9b3f20fc75a822e4728101fe88 | |
parent | c30e6e17469019c58bde8497989f9d51630082eb (diff) |
libqmi-glib,utils: skip trailing NUL bytes from UTF-8 validation
Otherwise, --dms-swi-get-current-firmware will fail to report the
correct strings:
# qmicli -d /dev/cdc-wdm0 -p --dms-swi-get-current-firmware
[/dev/cdc-wdm0] Successfully retrieved current firmware:
Model: M??!S&
Boot version: S.%J?k??C>A_ceL?.?Lq$??
AMSS version: S.%J?k??C>A_ceL?.?Lq$??
SKU ID: ???6
Package ID: unknown
Carrier ID: 1
?????? Config version: 0?Hq$f
(cherry picked from commit e4052d52db2aead068da95a1e5906ff6155e2265)
-rw-r--r-- | src/libqmi-glib/qmi-utils.c | 8 | ||||
-rw-r--r-- | src/libqmi-glib/test/test-message.c | 42 |
2 files changed, 46 insertions, 4 deletions
diff --git a/src/libqmi-glib/qmi-utils.c b/src/libqmi-glib/qmi-utils.c index fbe64d0..8654649 100644 --- a/src/libqmi-glib/qmi-utils.c +++ b/src/libqmi-glib/qmi-utils.c @@ -131,6 +131,14 @@ __qmi_string_utf8_validate_printable (const guint8 *utf8, g_assert (utf8); g_assert (utf8_len); + /* Ignore all trailing NUL bytes, if any */ + while ((utf8_len > 0) && (utf8[utf8_len - 1] == '\0')) + utf8_len--; + + /* An empty string */ + if (!utf8_len) + return TRUE; + /* First check if valid UTF-8 */ init = (const gchar *)utf8; if (!g_utf8_validate (init, utf8_len, NULL)) diff --git a/src/libqmi-glib/test/test-message.c b/src/libqmi-glib/test/test-message.c index b0a843b..978ed34 100644 --- a/src/libqmi-glib/test/test-message.c +++ b/src/libqmi-glib/test/test-message.c @@ -177,20 +177,27 @@ test_message_parse_complete_and_complete (void) static void test_message_printable_common (const guint8 *buffer, guint buffer_len, + guint16 vendor_id, const gchar *expected_in_printable) { + QmiMessageContext *context = NULL; QmiMessage *message; GByteArray *array; GError *error = NULL; gchar *printable; + if (vendor_id != QMI_MESSAGE_VENDOR_GENERIC) { + context = qmi_message_context_new (); + qmi_message_context_set_vendor_id (context, vendor_id); + } + array = g_byte_array_sized_new (buffer_len); g_byte_array_append (array, buffer, buffer_len); message = qmi_message_new_from_raw (array, &error); g_assert_no_error (error); g_assert (message); - printable = qmi_message_get_printable_full (message, NULL, ""); + printable = qmi_message_get_printable_full (message, context, ""); #ifdef TEST_PRINT_MESSAGE g_print ("\n%s\n", printable); #endif @@ -199,6 +206,9 @@ test_message_printable_common (const guint8 *buffer, g_byte_array_unref (array); qmi_message_unref (message); + + if (context) + qmi_message_context_unref (context); } static void @@ -214,7 +224,7 @@ test_message_parse_wrong_tlv (void) 0x06, 0x00, 0x01, 0x01, 0x01, 0x02, 0x01, 0x05 }; - test_message_printable_common (buffer, G_N_ELEMENTS (buffer), "ERROR: Reading TLV would overflow"); + test_message_printable_common (buffer, G_N_ELEMENTS (buffer), QMI_MESSAGE_VENDOR_GENERIC, "ERROR: Reading TLV would overflow"); } static void @@ -237,7 +247,7 @@ test_message_parse_missing_size (void) 0x01 }; - test_message_printable_common (buffer, G_N_ELEMENTS (buffer), "ERROR: Reading TLV would overflow"); + test_message_printable_common (buffer, G_N_ELEMENTS (buffer), QMI_MESSAGE_VENDOR_GENERIC, "ERROR: Reading TLV would overflow"); } static void @@ -254,7 +264,30 @@ test_message_parse_string_with_crlf (void) 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2A, 0x31, 0x45, 0x0D, 0x0A }; - test_message_printable_common (buffer, sizeof (buffer), "$GPGSA,A,1,,,,,,,,,,,,,,,*1E"); + test_message_printable_common (buffer, sizeof (buffer), QMI_MESSAGE_VENDOR_GENERIC, "$GPGSA,A,1,,,,,,,,,,,,,,,*1E"); +} + +static void +test_message_parse_string_with_trailing_nul (void) +{ + /* The Sierra Wireless (vid 0x1199) specific method to get current firmware + * info uses NUL-terminated strings. + */ + const guint8 buffer[] = { + 0x01, 0x82, 0x00, 0x80, 0x02, 0x03, 0x02, 0x01, 0x00, 0x56, 0x55, 0x76, + 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x0C, 0x00, 0x30, + 0x30, 0x32, 0x2E, 0x30, 0x37, 0x32, 0x5F, 0x30, 0x30, 0x30, 0x00, 0x17, + 0x08, 0x00, 0x47, 0x45, 0x4E, 0x45, 0x52, 0x49, 0x43, 0x00, 0x16, 0x08, + 0x00, 0x30, 0x30, 0x30, 0x2E, 0x30, 0x30, 0x35, 0x00, 0x15, 0x02, 0x00, + 0x31, 0x00, 0x13, 0x08, 0x00, 0x31, 0x31, 0x30, 0x32, 0x34, 0x37, 0x36, + 0x00, 0x12, 0x15, 0x00, 0x53, 0x57, 0x49, 0x39, 0x58, 0x33, 0x30, 0x43, + 0x5F, 0x30, 0x32, 0x2E, 0x33, 0x33, 0x2E, 0x30, 0x33, 0x2E, 0x30, 0x30, + 0x00, 0x11, 0x15, 0x00, 0x53, 0x57, 0x49, 0x39, 0x58, 0x33, 0x30, 0x43, + 0x5F, 0x30, 0x32, 0x2E, 0x33, 0x33, 0x2E, 0x30, 0x33, 0x2E, 0x30, 0x30, + 0x00, 0x10, 0x07, 0x00, 0x4D, 0x43, 0x37, 0x34, 0x35, 0x35, 0x00 + }; + + test_message_printable_common (buffer, sizeof (buffer), 0x1199, "SWI9X30C_02.33.03.00"); } /*****************************************************************************/ @@ -1587,6 +1620,7 @@ int main (int argc, char **argv) g_test_add_func ("/libqmi-glib/message/parse/wrong-tlv", test_message_parse_wrong_tlv); g_test_add_func ("/libqmi-glib/message/parse/missing-size", test_message_parse_missing_size); g_test_add_func ("/libqmi-glib/message/parse/string-with-crlf", test_message_parse_string_with_crlf); + g_test_add_func ("/libqmi-glib/message/parse/string-with-trailing-nul", test_message_parse_string_with_trailing_nul); g_test_add_func ("/libqmi-glib/message/new/request", test_message_new_request); g_test_add_func ("/libqmi-glib/message/new/request-from-data", test_message_new_request_from_data); |