Do additional checking to ensure the user provides valid values
for boolean parameters. Also, provide a helpful error if the user
goes astray.

Signed-off-by: Jason Gerecke <killert...@gmail.com>
---

 Changes to previous version:
 - convert_value_from_user now returns conversion success and requires a return 
pointer
 - atoi() swapped to strtol() for better error checking
 - error message more generic
 - extra crazy to safely handle memory de/allocation

 tools/xsetwacom.c |   51 ++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 40 insertions(+), 11 deletions(-)

diff --git a/tools/xsetwacom.c b/tools/xsetwacom.c
index fa98e3e..04feed5 100644
--- a/tools/xsetwacom.c
+++ b/tools/xsetwacom.c
@@ -24,6 +24,8 @@
 #include <wacom-properties.h>
 #include "Xwacom.h"
 
+#include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <ctype.h>
@@ -1420,18 +1422,30 @@ error:
        return;
 }
 
-static int convert_value_from_user(param_t *param, char *value)
+static Bool convert_value_from_user(param_t *param, char *value, int 
*return_value)
 {
-       int val;
+       if (!return_value)
+               return False;
 
-       if ((param->prop_flags & PROP_FLAG_BOOLEAN) && strcmp(value, "off") == 
0)
-                       val = 0;
-       else if ((param->prop_flags & PROP_FLAG_BOOLEAN) && strcmp(value, "on") 
== 0)
-                       val = 1;
-       else
-               val = atoi(value);
+       if (param->prop_flags & PROP_FLAG_BOOLEAN)
+       {
+               if (strcmp(value, "off") == 0)
+                       *return_value = 0;
+               else if (strcmp(value, "on") == 0)
+                       *return_value = 1;
+               else
+                       return False;
+       } else {
+               char *end;
+               long conversion = strtol(value, &end, 10);
+               if (end == value || *end != '\0' || errno == ERANGE ||
+                   conversion < INT_MIN || conversion > INT_MAX)
+                       return False;
+
+               *return_value = (int)conversion;
+       }
 
-       return val;
+       return True;
 }
 
 static void set(Display *dpy, int argc, char **argv)
@@ -1507,7 +1521,22 @@ static void set(Display *dpy, int argc, char **argv)
 
        for (i = 0; i < nvals; i++)
        {
-               val = convert_value_from_user(param, values[i]);
+               int *retval = calloc(1,sizeof(int));
+               if (!retval)
+               {
+                       fprintf(stderr, "calloc failled\n");
+                       exit(EXIT_FAILURE);
+               }
+               Bool success = convert_value_from_user(param, values[i], 
retval);
+               int val = *retval;
+               free(retval);
+
+               if (!success)
+               {
+                       fprintf(stderr, "'%s' is not a valid value for the '%s' 
property.\n",
+                               values[i], param->name);
+                       goto out;
+               }
 
                switch(param->prop_format)
                {
-- 
1.7.1


------------------------------------------------------------------------------
What You Don't Know About Data Connectivity CAN Hurt You
This paper provides an overview of data connectivity, details
its effect on application quality, and explores various alternative
solutions. http://p.sf.net/sfu/progress-d2d
_______________________________________________
Linuxwacom-devel mailing list
Linuxwacom-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel

Reply via email to