diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2012-03-28 11:25:22 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-03-29 09:18:04 +0200 |
commit | e00705c20349cf936d2a732942f0edd9103a4b51 (patch) | |
tree | 35f4d8b06b5e068feb79e22779cd7bdcacd4021d | |
parent | 71e108fd64174e76502408d59b124387616a5270 (diff) | |
download | ModemManager-e00705c20349cf936d2a732942f0edd9103a4b51.tar.gz |
libmm-common,helpers: additional checks in the string to number converters
We won't allow NULL, empty, or strings with non-digits.
-rw-r--r-- | libmm-common/mm-common-helpers.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/libmm-common/mm-common-helpers.c b/libmm-common/mm-common-helpers.c index 1ca4f4e2f..ca96d7526 100644 --- a/libmm-common/mm-common-helpers.c +++ b/libmm-common/mm-common-helpers.c @@ -511,6 +511,14 @@ mm_get_int_from_str (const gchar *str, { glong num; + if (!str || !str[0]) + return FALSE; + + for (num = 0; str[num]; num++) { + if (str[num] != '-' && !g_ascii_isdigit (str[num])) + return FALSE; + } + errno = 0; num = strtol (str, NULL, 10); if (!errno && num >= G_MININT && num <= G_MAXINT) { @@ -543,6 +551,14 @@ mm_get_uint_from_str (const gchar *str, { gulong num; + if (!str || !str[0]) + return FALSE; + + for (num = 0; str[num]; num++) { + if (!g_ascii_isdigit (str[num])) + return FALSE; + } + errno = 0; num = strtoul (str, NULL, 10); if (!errno && num <= G_MAXUINT) { @@ -574,6 +590,19 @@ mm_get_double_from_str (const gchar *str, gdouble *out) { gdouble num; + guint i; + + if (!str || !str[0]) + return FALSE; + + for (i = 0; str[i]; i++) { + /* we don't really expect numbers in scientific notation, so + * don't bother looking for exponents and such */ + if (str[i] != '-' && + str[i] != '.' && + !g_ascii_isdigit (str[i])) + return FALSE; + } errno = 0; num = strtod (str, NULL); |