aboutsummaryrefslogtreecommitdiff
path: root/libmm-glib
diff options
context:
space:
mode:
Diffstat (limited to 'libmm-glib')
-rw-r--r--libmm-glib/mm-common-helpers.c18
-rw-r--r--libmm-glib/mm-common-helpers.h2
2 files changed, 14 insertions, 6 deletions
diff --git a/libmm-glib/mm-common-helpers.c b/libmm-glib/mm-common-helpers.c
index da1cc1cc..b443f1ab 100644
--- a/libmm-glib/mm-common-helpers.c
+++ b/libmm-glib/mm-common-helpers.c
@@ -1687,10 +1687,12 @@ mm_utils_hex2byte (const gchar *hex)
}
gchar *
-mm_utils_hexstr2bin (const gchar *hex, gsize *out_len)
+mm_utils_hexstr2bin (const gchar *hex,
+ gsize *out_len,
+ GError **error)
{
const gchar *ipos = hex;
- gchar *buf = NULL;
+ g_autofree gchar *buf = NULL;
gsize i;
gint a;
gchar *opos;
@@ -1699,20 +1701,26 @@ mm_utils_hexstr2bin (const gchar *hex, gsize *out_len)
len = strlen (hex);
/* Length must be a multiple of 2 */
- g_return_val_if_fail ((len % 2) == 0, NULL);
+ if ((len % 2) != 0) {
+ g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS,
+ "Hex conversion failed: invalid input length");
+ return NULL;
+ }
opos = buf = g_malloc0 ((len / 2) + 1);
for (i = 0; i < len; i += 2) {
a = mm_utils_hex2byte (ipos);
if (a < 0) {
- g_free (buf);
+ g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS,
+ "Hex byte conversion from '%c%c' failed",
+ ipos[0], ipos[1]);
return NULL;
}
*opos++ = a;
ipos += 2;
}
*out_len = len / 2;
- return buf;
+ return g_steal_pointer (&buf);
}
/* End from hostap */
diff --git a/libmm-glib/mm-common-helpers.h b/libmm-glib/mm-common-helpers.h
index 65d0e70a..d3cf62d7 100644
--- a/libmm-glib/mm-common-helpers.h
+++ b/libmm-glib/mm-common-helpers.h
@@ -181,7 +181,7 @@ gchar *mm_get_string_unquoted_from_match_info (GMatchInfo *match_info,
const gchar *mm_sms_delivery_state_get_string_extended (guint delivery_state);
gint mm_utils_hex2byte (const gchar *hex);
-gchar *mm_utils_hexstr2bin (const gchar *hex, gsize *out_len);
+gchar *mm_utils_hexstr2bin (const gchar *hex, gsize *out_len, GError **error);
gchar *mm_utils_bin2hexstr (const guint8 *bin, gsize len);
gboolean mm_utils_ishexstr (const gchar *hex);