diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2016-11-28 15:12:46 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2016-11-28 15:12:46 +0100 |
commit | 99c36dc00518c5e5bd62633bb4097d6ef88c15bd (patch) | |
tree | 4e8e19346d32b1d504b7cace58d23bdd6d6dd28d /src/qmicli | |
parent | 56d8b97684ca40a1d44d97a1be0a067adaca8480 (diff) | |
download | external_libqmi-99c36dc00518c5e5bd62633bb4097d6ef88c15bd.zip external_libqmi-99c36dc00518c5e5bd62633bb4097d6ef88c15bd.tar.gz external_libqmi-99c36dc00518c5e5bd62633bb4097d6ef88c15bd.tar.bz2 |
qmicli,helpers: more strict approach validating the unique id string
We don't want e.g. an array that has some printable ASCII bytes, then
NUL bytes and then some more printable ASCII bytes. We require a totally
valid ASCII string, suffixed with NUL bytes if the string is shorter
than 16 bytes.
Plus, we avoid g_str_is_ascii(), which is only available in GLib 2.40.0.
Diffstat (limited to 'src/qmicli')
-rw-r--r-- | src/qmicli/qmicli-helpers.c | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/src/qmicli/qmicli-helpers.c b/src/qmicli/qmicli-helpers.c index bc04afb..938516c 100644 --- a/src/qmicli/qmicli-helpers.c +++ b/src/qmicli/qmicli-helpers.c @@ -107,7 +107,10 @@ qmicli_get_raw_data_printable (const GArray *data, gchar * qmicli_get_firmware_image_unique_id_printable (const GArray *unique_id) { - gchar *unique_id_str; + gchar *unique_id_str; + guint i; + guint n_ascii; + gboolean end; #define UNIQUE_ID_LEN 16 @@ -115,12 +118,32 @@ qmicli_get_firmware_image_unique_id_printable (const GArray *unique_id) unique_id_str = g_malloc0 (UNIQUE_ID_LEN + 1); memcpy (unique_id_str, unique_id->data, UNIQUE_ID_LEN); -#undef UNIQUE_ID_LEN + /* We want an ASCII string that, if finished before the 16 bytes, + * is suffixed with NUL bytes. */ + for (i = 0; i < UNIQUE_ID_LEN; i++) { + /* If a byte isn't ASCII, stop */ + if (unique_id_str[i] & 0x80) + break; + /* If string isn't finished yet... */ + if (!end) { + /* String finished now */ + if (unique_id_str[i] == '\0') + end = TRUE; + else + n_ascii++; + } else { + /* String finished but we then got + * another ASCII byte? not possible */ + if (unique_id_str[i] != '\0') + break; + } + } - /* If this is ASCII (more than likely), return it */ - if (g_str_is_ascii (unique_id_str)) + if (i == UNIQUE_ID_LEN && n_ascii > 0) return unique_id_str; +#undef UNIQUE_ID_LEN + g_free (unique_id_str); /* Get a raw hex string otherwise */ |