[PATCH v2 libinput 01/10] util: add safe_atoi helper function

Peter Hutterer peter.hutterer at who-t.net
Wed Jun 15 06:37:35 UTC 2016


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
Changes to v1:
- New in this series

 src/libinput-util.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/libinput-util.h b/src/libinput-util.h
index 701fe07..051d08f 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 */
-- 
2.7.4



More information about the wayland-devel mailing list