summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2016-04-30 14:44:09 +0200
committerThomas Haller <thaller@redhat.com>2016-05-03 11:52:39 +0200
commit05478e4f4cb1bf8600f3211a9b2983b9e4c43152 (patch)
tree2c7f97129b31f1f2c8bbc28f3583fe1ba90f5397
parent904f840c2072062a22f82024563bf0caf2a062cf (diff)
downloadNetworkManager-05478e4f4cb1bf8600f3211a9b2983b9e4c43152.tar.gz
utils: refactor _nm_utils_ascii_str_to_int64()
To allow for trailing whitespace, we don't need to copy and trunacate the input string. g_ascii_strtoll() conveniently returns the location via the endptr argument.
-rw-r--r--shared/nm-shared-utils.c47
1 files changed, 12 insertions, 35 deletions
diff --git a/shared/nm-shared-utils.c b/shared/nm-shared-utils.c
index 932b7432c3..38f6529d2a 100644
--- a/shared/nm-shared-utils.c
+++ b/shared/nm-shared-utils.c
@@ -45,8 +45,7 @@ gint64
_nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback)
{
gint64 v;
- size_t len;
- char buf[64], *s, *str_free = NULL;
+ char *s = NULL;
if (str) {
while (g_ascii_isspace (str[0]))
@@ -57,46 +56,24 @@ _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 ma
return fallback;
}
- len = strlen (str);
- if (g_ascii_isspace (str[--len])) {
- /* backward search the first non-ws character.
- * We already know that str[0] is non-ws. */
- while (g_ascii_isspace (str[--len]))
- ;
-
- /* str[len] is now the last non-ws character... */
- len++;
-
- if (len >= sizeof (buf))
- s = str_free = g_malloc (len + 1);
- else
- s = buf;
-
- memcpy (s, str, len);
- s[len] = 0;
-
- nm_assert (len > 0 && len < strlen (str) && len == strlen (s));
- nm_assert (!g_ascii_isspace (str[len-1]) && g_ascii_isspace (str[len]));
- nm_assert (strncmp (str, s, len) == 0);
-
- str = s;
- }
-
errno = 0;
v = g_ascii_strtoll (str, &s, base);
if (errno != 0)
- v = fallback;
- else if (s[0] != 0) {
- errno = EINVAL;
- v = fallback;
- } else if (v > max || v < min) {
+ return fallback;
+ if (s[0] != '\0') {
+ while (g_ascii_isspace (s[0]))
+ s++;
+ if (s[0] != '\0') {
+ errno = EINVAL;
+ return fallback;
+ }
+ }
+ if (v > max || v < min) {
errno = ERANGE;
- v = fallback;
+ return fallback;
}
- if (G_UNLIKELY (str_free))
- g_free (str_free);
return v;
}