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.
This commit is contained in:
Aleksander Morgado
2021-02-23 11:35:11 +00:00
parent dbdf67e9f7
commit 657cabcfce
11 changed files with 90 additions and 153 deletions
+29 -56
View File
@@ -1291,9 +1291,9 @@ static guint
parse_mnc_length (const gchar *response,
GError **error)
{
guint sw1 = 0;
guint sw2 = 0;
gchar *hex = 0;
guint sw1 = 0;
guint sw2 = 0;
g_autofree gchar *hex = NULL;
if (!mm_3gpp_parse_crsm_response (response,
&sw1,
@@ -1306,47 +1306,34 @@ parse_mnc_length (const gchar *response,
(sw1 == 0x91) ||
(sw1 == 0x92) ||
(sw1 == 0x9f)) {
gsize buflen = 0;
guint32 mnc_len;
gchar *bin;
gsize buflen = 0;
guint32 mnc_len;
g_autofree gchar *bin = NULL;
/* Convert hex string to binary */
bin = mm_utils_hexstr2bin (hex, &buflen);
if (!bin || buflen < 4) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"SIM returned malformed response '%s'",
hex);
g_free (bin);
g_free (hex);
bin = mm_utils_hexstr2bin (hex, &buflen, error);
if (!bin) {
g_prefix_error (error, "SIM returned malformed response '%s': ", hex);
return 0;
}
if (buflen < 4) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"SIM returned malformed response '%s': too short", hex);
return 0;
}
g_free (hex);
/* MNC length is byte 4 of this SIM file */
mnc_len = bin[3] & 0xFF;
if (mnc_len == 2 || mnc_len == 3) {
g_free (bin);
if (mnc_len == 2 || mnc_len == 3)
return mnc_len;
}
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"SIM returned invalid MNC length %d (should be either 2 or 3)",
mnc_len);
g_free (bin);
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"SIM returned invalid MNC length %d (should be either 2 or 3)", mnc_len);
return 0;
}
g_free (hex);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"SIM failed to handle CRSM request (sw1 %d sw2 %d)",
sw1, sw2);
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"SIM failed to handle CRSM request (sw1 %d sw2 %d)", sw1, sw2);
return 0;
}
@@ -1410,9 +1397,9 @@ static gchar *
parse_spn (const gchar *response,
GError **error)
{
guint sw1 = 0;
guint sw2 = 0;
gchar *hex = 0;
guint sw1 = 0;
guint sw2 = 0;
g_autofree gchar *hex = NULL;
if (!mm_3gpp_parse_crsm_response (response,
&sw1,
@@ -1425,40 +1412,26 @@ parse_spn (const gchar *response,
(sw1 == 0x91) ||
(sw1 == 0x92) ||
(sw1 == 0x9f)) {
gsize buflen = 0;
gchar *bin;
gchar *utf8;
gsize buflen = 0;
g_autofree gchar *bin = NULL;
/* Convert hex string to binary */
bin = mm_utils_hexstr2bin (hex, &buflen);
bin = mm_utils_hexstr2bin (hex, &buflen, error);
if (!bin) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"SIM returned malformed response '%s'",
hex);
g_free (hex);
g_prefix_error (error, "SIM returned malformed response '%s': ", hex);
return NULL;
}
g_free (hex);
/* Remove the FF filler at the end */
while (buflen > 1 && bin[buflen - 1] == (char)0xff)
buflen--;
/* First byte is metadata; remainder is GSM-7 unpacked into octets; convert to UTF8 */
utf8 = (gchar *)mm_charset_gsm_unpacked_to_utf8 ((guint8 *)bin + 1, buflen - 1);
g_free (bin);
return utf8;
return (gchar *)mm_charset_gsm_unpacked_to_utf8 ((guint8 *)bin + 1, buflen - 1);
}
g_free (hex);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"SIM failed to handle CRSM request (sw1 %d sw2 %d)",
sw1, sw2);
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"SIM failed to handle CRSM request (sw1 %d sw2 %d)", sw1, sw2);
return NULL;
}
+1 -1
View File
@@ -169,7 +169,7 @@ mm_modem_charset_hex_to_utf8 (const gchar *src,
iconv_from = charset_iconv_from (charset);
g_return_val_if_fail (iconv_from != NULL, FALSE);
unconverted = mm_utils_hexstr2bin (src, &unconverted_len);
unconverted = mm_utils_hexstr2bin (src, &unconverted_len, NULL);
if (!unconverted)
return NULL;
+7 -13
View File
@@ -1811,25 +1811,19 @@ mm_firmware_unique_id_to_qmi_unique_id (const gchar *unique_id,
/* The length will be exactly EXPECTED_QMI_UNIQUE_ID_LENGTH*2 if given in HEX */
if (len == (2 * EXPECTED_QMI_UNIQUE_ID_LENGTH)) {
guint8 *tmp;
gsize tmp_len;
guint i;
for (i = 0; i < len; i++) {
if (!g_ascii_isxdigit (unique_id[i])) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Unexpected character found in unique id (not HEX): %c", unique_id[i]);
return NULL;
}
}
g_autofree guint8 *tmp = NULL;
gsize tmp_len;
tmp_len = 0;
tmp = (guint8 *) mm_utils_hexstr2bin (unique_id, &tmp_len);
tmp = (guint8 *) mm_utils_hexstr2bin (unique_id, &tmp_len, error);
if (!tmp) {
g_prefix_error (error, "Unexpected character found in unique id: ");
return NULL;
}
g_assert (tmp_len == EXPECTED_QMI_UNIQUE_ID_LENGTH);
qmi_unique_id = g_array_sized_new (FALSE, FALSE, sizeof (guint8), tmp_len);
g_array_insert_vals (qmi_unique_id, 0, tmp, tmp_len);
g_free (tmp);
return qmi_unique_id;
}
+2 -3
View File
@@ -4291,10 +4291,9 @@ mm_3gpp_parse_emergency_numbers (const char *raw, GError **error)
return NULL;
}
bin = (guint8 *) mm_utils_hexstr2bin (raw, &binlen);
bin = (guint8 *) mm_utils_hexstr2bin (raw, &binlen, error);
if (!bin) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS,
"invalid raw emergency numbers list contents: %s", raw);
g_prefix_error (error, "invalid raw emergency numbers list contents: ");
return NULL;
}
+5 -12
View File
@@ -339,24 +339,17 @@ mm_sms_part_3gpp_new_from_pdu (guint index,
gpointer log_object,
GError **error)
{
gsize pdu_len;
guint8 *pdu;
MMSmsPart *part;
g_autofree guint8 *pdu = NULL;
gsize pdu_len;
/* Convert PDU from hex to binary */
pdu = (guint8 *) mm_utils_hexstr2bin (hexpdu, &pdu_len);
pdu = (guint8 *) mm_utils_hexstr2bin (hexpdu, &pdu_len, error);
if (!pdu) {
g_set_error_literal (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't convert 3GPP PDU from hex to binary");
g_prefix_error (error, "Couldn't convert 3GPP PDU from hex to binary: ");
return NULL;
}
part = mm_sms_part_3gpp_new_from_binary_pdu (index, pdu, pdu_len, log_object, error);
g_free (pdu);
return part;
return mm_sms_part_3gpp_new_from_binary_pdu (index, pdu, pdu_len, log_object, error);
}
MMSmsPart *
+5 -12
View File
@@ -317,24 +317,17 @@ mm_sms_part_cdma_new_from_pdu (guint index,
gpointer log_object,
GError **error)
{
gsize pdu_len;
guint8 *pdu;
MMSmsPart *part;
g_autofree guint8 *pdu = NULL;
gsize pdu_len;
/* Convert PDU from hex to binary */
pdu = (guint8 *) mm_utils_hexstr2bin (hexpdu, &pdu_len);
pdu = (guint8 *) mm_utils_hexstr2bin (hexpdu, &pdu_len, error);
if (!pdu) {
g_set_error_literal (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't convert CDMA PDU from hex to binary");
g_prefix_error (error, "Couldn't convert CDMA PDU from hex to binary: ");
return NULL;
}
part = mm_sms_part_cdma_new_from_binary_pdu (index, pdu, pdu_len, log_object, error);
g_free (pdu);
return part;
return mm_sms_part_cdma_new_from_binary_pdu (index, pdu, pdu_len, log_object, error);
}
struct Parameter {