diff options
author | Aleksander Morgado <aleksandermj@chromium.org> | 2023-07-03 11:19:42 +0000 |
---|---|---|
committer | Aleksander Morgado <aleksandermj@chromium.org> | 2023-07-03 12:44:51 +0000 |
commit | d963a2ffff21050e3300dc6bd8b435120c880bed (patch) | |
tree | 260edb26d1972694a97e0b7ad4e3758b48d1540d | |
parent | fbad2f1df4996d997e452dbed614de4f3b7d55ca (diff) |
libmbim-glib,message: avoid memory alignment issues reading UTF-16 strings
Doing an explicit cast to a `gunichar` array is not alignment
safe. Instead, unconditionally copy the byte array into a new one that
is ensured to be correctly aligned.
-rw-r--r-- | src/libmbim-glib/mbim-message.c | 13 |
1 files changed, 4 insertions, 9 deletions
diff --git a/src/libmbim-glib/mbim-message.c b/src/libmbim-glib/mbim-message.c index 035395e..fc73f39 100644 --- a/src/libmbim-glib/mbim-message.c +++ b/src/libmbim-glib/mbim-message.c @@ -569,24 +569,19 @@ _mbim_message_read_string (const MbimMessage *self, if (encoding == MBIM_STRING_ENCODING_UTF16) { g_autofree gunichar2 *utf16d = NULL; - const gunichar2 *utf16 = NULL; - utf16 = (const gunichar2 *) (self->data + information_buffer_offset + struct_start_offset + offset); + /* Always duplicate to avoid memory alignment issues */ + utf16d = g_memdup (self->data + information_buffer_offset + struct_start_offset + offset, size); /* For BE systems, convert from LE to BE */ if (G_BYTE_ORDER == G_BIG_ENDIAN) { guint i; - utf16d = (gunichar2 *) g_malloc (size); for (i = 0; i < (size / 2); i++) - utf16d[i] = GUINT16_FROM_LE (utf16[i]); + utf16d[i] = GUINT16_FROM_LE (utf16d[i]); } - *str = g_utf16_to_utf8 (utf16d ? utf16d : utf16, - size / 2, - NULL, - NULL, - error); + *str = g_utf16_to_utf8 (utf16d, size / 2, NULL, NULL, error); if (!(*str)) { g_prefix_error (error, "Error converting string to UTF-8: "); |