Răsfoiți Sursa

plugins-root/check_dhcp.c: fix a potential segfault

- remove call_getopt(), which iteratively calls process_arguments()
  iteratively with successive elements of argv.

Since getopt_long is internally iterating over argv anyway, this extra
iteration is unnecessary and can potentially cause a segfault in glibc:

    if (d->optind != argc && !strcmp (argv[d->optind], "--"))

Since the argv passed to getopt_long becomes smaller while d->optind
continues increasing, eventually extending beyond the end of argv. Since
memory after the end of argv _may_ be valid, this is not always
reproducible (e.g. I cannot reproduce directly from a shell, but it
fails consistently from Shinken.)

Conflicts:
	plugins-root/check_dhcp.c
Greg Bowser 10 ani în urmă
părinte
comite
8dc6e3b4a0
1 a modificat fișierele cu 7 adăugiri și 28 ștergeri
  1. 7 28
      plugins-root/check_dhcp.c

+ 7 - 28
plugins-root/check_dhcp.c

@@ -228,7 +228,6 @@ struct in_addr requested_address;
 
 
 
 
 int process_arguments(int, char **);
 int process_arguments(int, char **);
-int call_getopt(int, char **);
 int validate_arguments(void);
 int validate_arguments(void);
 void print_usage(void);
 void print_usage(void);
 void print_help(void);
 void print_help(void);
@@ -1072,30 +1071,9 @@ int get_results(void){
 
 
 /* process command-line arguments */
 /* process command-line arguments */
 int process_arguments(int argc, char **argv){
 int process_arguments(int argc, char **argv){
-	int c;
-
-	if(argc<1)
-		return ERROR;
-
-	c=0;
-	while((c+=(call_getopt(argc-c,&argv[c])))<argc){
-
-		/*
-		if(is_option(argv[c]))
-			continue;
-		*/
-		}
-
-	return validate_arguments();
-        }
-
-
-
-int call_getopt(int argc, char **argv){
-	int c=0;
-	int i=0;
-
+	int c = 0;
 	int option_index = 0;
 	int option_index = 0;
+
 	static struct option long_options[] =
 	static struct option long_options[] =
 	{
 	{
 		{"serverip",       required_argument,0,'s'},
 		{"serverip",       required_argument,0,'s'},
@@ -1110,11 +1088,12 @@ int call_getopt(int argc, char **argv){
 		{0,0,0,0}
 		{0,0,0,0}
 	};
 	};
 
 
+	if(argc<1)
+		return ERROR;
+
 	while(1){
 	while(1){
 		c=getopt_long(argc,argv,"+hVvt:s:r:t:i:m:u",long_options,&option_index);
 		c=getopt_long(argc,argv,"+hVvt:s:r:t:i:m:u",long_options,&option_index);
 
 
-		i++;
-
 		if(c==-1||c==EOF||c==1)
 		if(c==-1||c==EOF||c==1)
 			break;
 			break;
 
 
@@ -1123,7 +1102,6 @@ int call_getopt(int argc, char **argv){
 		case 'r':
 		case 'r':
 		case 't':
 		case 't':
 		case 'i':
 		case 'i':
-			i++;
 			break;
 			break;
 		default:
 		default:
 			break;
 			break;
@@ -1195,7 +1173,8 @@ int call_getopt(int argc, char **argv){
 		        }
 		        }
 	        }
 	        }
 
 
-	return i+1;
+
+	return validate_arguments();
         }
         }