diff options
author | Aleksander Morgado <aleksandermj@chromium.org> | 2023-07-10 10:08:26 +0000 |
---|---|---|
committer | Aleksander Morgado <aleksandermj@chromium.org> | 2023-07-10 10:10:41 +0000 |
commit | e129ca87a698d81753d1935c3ea3209f0dcf9c78 (patch) | |
tree | 2d5acae766c83ca104c0cdc930431a1e0bca3b74 | |
parent | 1782f3ad9ee47a01e2c23c3950cc5c1b640115a9 (diff) |
libmbim,message: validate UTF-8 explicitly even when converting from UTF-16
The g_utf16_to_utf8() method doesn't do any explicit UTF-8 validation,
it only ensures that the input data is correctly interpreted as
UTF-16.
-rw-r--r-- | src/libmbim-glib/mbim-message.c | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/src/libmbim-glib/mbim-message.c b/src/libmbim-glib/mbim-message.c index 32a706f..b37ec1e 100644 --- a/src/libmbim-glib/mbim-message.c +++ b/src/libmbim-glib/mbim-message.c @@ -532,10 +532,11 @@ _mbim_message_read_string (const MbimMessage *self, guint32 *bytes_read, GError **error) { - guint64 required_size; - guint32 offset; - guint32 size; - guint32 information_buffer_offset; + g_autofree gchar *tmp = NULL; + guint64 required_size; + guint32 offset; + guint32 size; + guint32 information_buffer_offset; information_buffer_offset = _mbim_message_get_information_buffer_offset (self); @@ -581,12 +582,12 @@ _mbim_message_read_string (const MbimMessage *self, utf16d[i] = GUINT16_FROM_LE (utf16d[i]); } - *str = g_utf16_to_utf8 (utf16d, size / 2, NULL, NULL, error); - - if (!(*str)) { + tmp = g_utf16_to_utf8 (utf16d, size / 2, NULL, NULL, error); + if (!tmp) { g_prefix_error (error, "Error converting string to UTF-8: "); return FALSE; } + size = strlen (tmp); } else if (encoding == MBIM_STRING_ENCODING_UTF8) { const gchar *utf8; @@ -596,15 +597,16 @@ _mbim_message_read_string (const MbimMessage *self, while (size > 0 && utf8[size - 1] == '\0') size--; - if (!g_utf8_validate (utf8, size, NULL)) { - g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Error validating UTF-8 string"); - return FALSE; - } - - *str = g_strndup (utf8, size); + tmp = g_strndup (utf8, size); } else g_assert_not_reached (); + if (!g_utf8_validate (tmp, size, NULL)) { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Error validating UTF-8 string"); + return FALSE; + } + + *str = g_steal_pointer (&tmp); return TRUE; } |