summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2016-05-25 13:29:32 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2016-06-22 08:48:01 +1000
commiteb9d47a8aea4f0f202359d8e68907556b92b5997 (patch)
tree5d015c910ee45c3745bc76d189adae984cef4ca8 /src
parent28f75d9f9debf4fcc78029e2ddca5c7feac06571 (diff)
util: add safe_atoi helper function
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net> Reviewed-by: Jason Gerecke <jason.gerecke@wacom.com> Reviewed-by: Carlos Garnacho <carlosg@gnome.org>
Diffstat (limited to 'src')
-rw-r--r--src/libinput-util.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libinput-util.h b/src/libinput-util.h
index 701fe078..051d08f2 100644
--- a/src/libinput-util.h
+++ b/src/libinput-util.h
@@ -27,6 +27,7 @@
#include <assert.h>
#include <unistd.h>
+#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
@@ -399,4 +400,23 @@ us2ms(uint64_t us)
return (uint32_t)(us / 1000);
}
+static inline bool
+safe_atoi(const char *str, int *val)
+{
+ char *endptr;
+ long v;
+
+ v = strtol(str, &endptr, 10);
+ if (str == endptr)
+ return false;
+ if (*str != '\0' && *endptr != '\0')
+ return false;
+
+ if (v > INT_MAX || v < INT_MIN)
+ return false;
+
+ *val = v;
+ return true;
+}
+
#endif /* LIBINPUT_UTIL_H */