aboutsummaryrefslogtreecommitdiff
path: root/libmm-glib/mm-common-helpers.c
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2020-11-22 17:01:19 +0100
committerAleksander Morgado <aleksander@aleksander.es>2021-02-23 11:35:11 +0000
commit657cabcfce6794d2a2f629d63dbd56fc149dab2e (patch)
tree402bd1b6787ee65b93aad0d1b94ef9e9162e2178 /libmm-glib/mm-common-helpers.c
parentdbdf67e9f7c55d7f70ed94449160a7ff254359a2 (diff)
libmm-glib,common-helpers: make hexstr2bin() return a GError
This util method checks whether the input string is a valid hex string, so make sure we return a GError on failure.
Diffstat (limited to 'libmm-glib/mm-common-helpers.c')
-rw-r--r--libmm-glib/mm-common-helpers.c18
1 files changed, 13 insertions, 5 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 */