[PATCH 2/2] xrandr: check_strtol(): be pedantic about conversion errors

Yann Droneaud yann at droneaud.fr
Tue Dec 29 02:33:29 PST 2009


Check errors for overflow, underflow, incomplete string conversion.

Signed-off-by: Yann Droneaud <yann at droneaud.fr>

---
 xrandr.c |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/xrandr.c b/xrandr.c
index 9b4f402..590da86 100644
--- a/xrandr.c
+++ b/xrandr.c
@@ -36,6 +36,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <math.h>
+#include <limits.h>
 
 #include "config.h"
 
@@ -2003,11 +2004,21 @@ pick_crtcs (void)
 static int
 check_strtol(char *s)
 {
-    char *endptr;
-    int result = strtol(s, &endptr, 10);
-    if (s == endptr)
+    char *endptr = NULL;
+    long result;
+
+    if (s == NULL || *s == '\0')
 	usage();
-    return result;
+
+    errno = 0;
+    result = strtol(s, &endptr, 10);
+    if (s == endptr ||
+	(endptr != NULL && endptr != '\0') ||
+	((result == LONG_MIN || result == LONG_MAX) && errno != 0) ||
+	(result < INT_MIN || result > INT_MAX))
+	usage();
+
+    return (int)result;
 }
 
 static double
-- 
1.6.2.5



More information about the xorg-devel mailing list