[PATCH 1/5] Convert atoi -> estrtol

From: FRIGN <dev_AT_frign.de>
Date: Mon, 22 Dec 2014 14:17:21 +0100

Basically we can't assume user-input is well-formed.
---
 dmenu.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
diff --git a/dmenu.c b/dmenu.c
index 94c70de..2d0a513 100644
--- a/dmenu.c
+++ b/dmenu.c
_AT_@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -25,6 +26,7 @@ struct Item {
 	Bool out;
 };
 
+static long estrtol(const char *s, int base);
 static void appenditem(Item *item, Item **list, Item **last);
 static void calcoffsets(void);
 static char *cistrstr(const char *s, const char *sub);
_AT_@ -61,6 +63,29 @@ static int mon = -1;
 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
 static char *(*fstrstr)(const char *, const char *) = strstr;
 
+static long
+estrtol(const char *s, int base)
+{
+        char *end;
+        long n;
+        errno = 0;
+        n = strtol(s, &end, base);
+        if (*end != '\0') {
+                if (base == 0) {
+                        printf("%s: not an integer\n", s);
+                        exit(1);
+                } else {
+                        printf("%s: not a base %d integer\n", s, base);
+                        exit(1);
+                }
+        }
+        if (errno != 0) {
+                printf("%s:", s);
+                exit(1);
+        }
+        return n;
+}
+
 int
 main(int argc, char *argv[]) {
 	Bool fast = False;
_AT_@ -84,9 +109,9 @@ main(int argc, char *argv[]) {
 			usage();
 		/* these options take one argument */
 		else if(!strcmp(argv[i], "-l"))   /* number of lines in vertical list */
-			lines = atoi(argv[++i]);
+			lines = estrtol(argv[++i], 0);
 		else if(!strcmp(argv[i], "-m"))
-			mon = atoi(argv[++i]);
+			mon = estrtol(argv[++i], 0);
 		else if(!strcmp(argv[i], "-p"))   /* adds prompt to left of input field */
 			prompt = argv[++i];
 		else if(!strcmp(argv[i], "-fn"))  /* font or font set */
-- 
1.8.5.5
--Multipart=_Mon__22_Dec_2014_18_40_59_+0100_ph7wbDS_mXmI_UXb
Content-Type: text/x-diff;
 name="0002-Un-boolify-codebase-and-refactor-config.def.h.patch"
Content-Disposition: attachment;
 filename="0002-Un-boolify-codebase-and-refactor-config.def.h.patch"
Content-Transfer-Encoding: 7bit
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon Dec 22 2014 - 19:00:04 CET