From 8c30a6b6f813114d0cdc6b8a04ae4336c8393d48 Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Sun, 14 Feb 2021 10:43:35 +0100 Subject: [PATCH] libmm-glib,common-helpers: hexstr2bin fails on empty input string Also, remove the trailing NUL byte that was appended to the output binary stream, as it's not needed in any case. --- libmm-glib/mm-common-helpers.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libmm-glib/mm-common-helpers.c b/libmm-glib/mm-common-helpers.c index 3a97e3be..30f9f94f 100644 --- a/libmm-glib/mm-common-helpers.c +++ b/libmm-glib/mm-common-helpers.c @@ -1701,6 +1701,12 @@ mm_utils_hexstr2bin (const gchar *hex, if (len < 0) len = strlen (hex); + if (len == 0) { + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS, + "Hex conversion failed: empty string"); + return NULL; + } + /* Length must be a multiple of 2 */ if ((len % 2) != 0) { g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS, @@ -1708,7 +1714,7 @@ mm_utils_hexstr2bin (const gchar *hex, return NULL; } - opos = buf = g_malloc0 ((len / 2) + 1); + opos = buf = g_malloc0 (len / 2); for (i = 0; i < len; i += 2) { a = mm_utils_hex2byte (ipos); if (a < 0) {