]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
squidpurge: display friendly errors on missing command line options
authorAmos Jeffries <squid3@treenet.co.nz>
Wed, 16 Jan 2013 11:22:25 +0000 (00:22 +1300)
committerAmos Jeffries <squid3@treenet.co.nz>
Wed, 16 Jan 2013 11:22:25 +0000 (00:22 +1300)
Currently the tool will crash with a segmentation fault if any one of
several command switches which are expected to have a mandatory argument
are in fact followed by nothing.
 Detect these cases and display a message about what is missing.

 Detected by Coverity Scan. Issue 740378

tools/purge/purge.cc

index 67baa971d84e9cd0dc9a34cdc2a335f3b616caca..d3aded2e7dcb8be92ca780f64359175a45ab2876 100644 (file)
@@ -652,20 +652,27 @@ parseCommandline( int argc, char* argv[], REList*& head,
             }
             break;
         case 'c':
-            if ( optarg && *optarg ) {
-                if ( *conffile ) xfree((void*) conffile);
-                conffile = xstrdup(optarg);
-                assert(conffile);
+            if ( !optarg || !*optarg ) {
+                fprintf( stderr, "%c requires a regex pattern argument!\n", option );
+                exit(1);
             }
+            if ( *conffile ) xfree((void*) conffile);
+            conffile = xstrdup(optarg);
+            assert(conffile);
             break;
 
         case 'd':
-            ::debugFlag = strtoul( optarg, 0, 0 );
+            ::debugFlag = optarg ? 0 : strtoul( optarg, 0, 0 );
             break;
 
         case 'E':
         case 'e':
-            if ( head == 0 ) tail = head = new REList( optarg, option=='E' );
+            if ( !optarg || !*optarg ) {
+                fprintf( stderr, "%c requires a regex pattern argument!\n", option );
+                exit(1);
+            }
+            if ( head == 0 )
+                tail = head = new REList( optarg, option=='E' );
             else {
                 tail->next = new REList( optarg, option=='E' );
                 tail = tail->next;
@@ -673,6 +680,10 @@ parseCommandline( int argc, char* argv[], REList*& head,
             break;
 
         case 'f':
+            if ( !optarg || !*optarg ) {
+                fprintf( stderr, "%c requires a filename argument!\n", option );
+                exit(1);
+            }
             if ( (rfile = fopen( optarg, "r" )) != NULL ) {
                 unsigned long lineno = 0;
 #define LINESIZE 512
@@ -711,6 +722,10 @@ parseCommandline( int argc, char* argv[], REList*& head,
             ::no_fork = ! ::no_fork;
             break;
         case 'p':
+            if ( !optarg || !*optarg ) {
+                fprintf( stderr, "%c requires a port argument!\n", option );
+                exit(1);
+            }
             colon = strchr( optarg, ':' );
             if ( colon == 0 ) {
                 // no colon, only look at host
@@ -743,6 +758,10 @@ parseCommandline( int argc, char* argv[], REList*& head,
             }
             break;
         case 'P':
+            if ( !optarg || !*optarg ) {
+                fprintf( stderr, "%c requires a mode argument!\n", option );
+                exit(1);
+            }
             ::purgeMode = ( strtol( optarg, 0, 0 ) & 0x07 );
             break;
         case 's':