<<< Date Index >>>     <<< Thread Index >>>

[PATCH] Fix -d command line option



Hi,

some background: lately I tried to help someone on #mutt and asked for debug output. After compiling a debug version, the user insisted there were no ~/.muttdebugX files.

As it turned out, he ran 'mutt -d -1' instead of '-d 1' (and that actually works, mutt doesn't produce an error and says it's debugging at level -1).

The attached patch fixes that and does some more: in generall I think we should be parsing input more strictly, esp. numbers. Simply atoi() is bad as it doesn't enable us to check for conversion errors.

The attached patch thus adds mutt_atoi() as a wrapper around strtol() which can be used to check for conversion errors. I used it for the -d switch to enforce a value of >0 for it (I'm not sure if exiting right atfer an error is good or not).

But there are much more places were we may want to warn the user that his input was invalid...

  bye, Rocco
--
:wq!
diff --git a/lib.c b/lib.c
index aac0742..b8f36b5 100644
--- a/lib.c
+++ b/lib.c
@@ -794,3 +794,22 @@ mutt_strsysexit(int e)
   
   return sysexits_h[i].str;
 }
+
+int mutt_atoi (const char *str, int *dst)
+{
+  int r;
+  int *res = dst ? dst : &r;
+  char *e = NULL;
+
+  /* no input: 0 */
+  if (!str || !*str)
+  {
+    *res = 0;
+    return 0;
+  }
+
+  *res = (int) strtol (str, &e, 10);
+  if (e && *e != '\0')
+    return -1;
+  return 0;
+}
diff --git a/lib.h b/lib.h
index eb3ba33..381e6d9 100644
--- a/lib.h
+++ b/lib.h
@@ -121,6 +121,8 @@ char *safe_strcat (char *, size_t, const
 char *safe_strncat (char *, size_t, const char *, size_t);
 char *safe_strdup (const char *);
 
+int mutt_atoi (const char *, int *);
+
 const char *mutt_stristr (const char *, const char *);
 const char *mutt_basename (const char *);
 
diff --git a/main.c b/main.c
index b11df91..8590125 100644
--- a/main.c
+++ b/main.c
@@ -613,7 +613,11 @@ #endif
 
       case 'd':
 #ifdef DEBUG
-       debuglevel = atoi (optarg);
+       if (mutt_atoi (optarg, &debuglevel) < 0 || debuglevel <= 0)
+       {
+         fprintf (stderr, _("Error: value '%s' is invalid for -d.\n"), optarg);
+         return 1;
+       }
        printf (_("Debugging at level %d.\n"), debuglevel);
 #else
        printf _("DEBUG was not defined during compilation.  Ignored.\n");