From 27f6679aa87b7b4a7b214d3f08f83bec16434580 Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Thu, 17 Feb 2022 09:59:24 +0100 Subject: [PATCH] charsets: set error if UTF-8 validation fails Otherwise, mm_modem_charset_bytearray_to_utf8() may return NULL without error set, and that will trigger a crash in the caller. Fixes https://gitlab.freedesktop.org/mobile-broadband/ModemManager/-/issues/511 --- src/mm-charsets.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/mm-charsets.c b/src/mm-charsets.c index a48da368..9a7bbb95 100644 --- a/src/mm-charsets.c +++ b/src/mm-charsets.c @@ -865,11 +865,18 @@ mm_modem_charset_bytearray_to_utf8 (GByteArray *bytearray, g_assert_not_reached (); } - if (utf8 && g_utf8_validate (utf8, -1, NULL)) - return g_steal_pointer (&utf8); + if (!utf8) { + g_prefix_error (error, "Invalid conversion from %s to UTF-8: ", settings->gsm_name); + return NULL; + } - g_prefix_error (error, "Invalid conversion from %s to UTF-8: ", settings->gsm_name); - return NULL; + if (!g_utf8_validate (utf8, -1, NULL)) { + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, + "Invalid conversion from %s: invalid UTF-8", settings->gsm_name); + return NULL; + } + + return g_steal_pointer (&utf8); } gchar *