Browse Source

[ifmgmt] Avoid relying on global variable within ifcommon_exec()

The getopt API defines optind as a global variable.  When used by the
"autoboot" command, the payload function passed to ifcommon_exec() may
result in a new iPXE script being executed; the commands therein would
then overwrite the value of optind.  On returning, ifcommon_exec()
would continue processing the list of interfaces from an undefined
point.

Fix by using a local variable to hold the index within the list of
interfaces.

Reported-by: Robin Smidsrød <robin@smidsrod.no>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 years ago
parent
commit
ed28c8304c
1 changed files with 3 additions and 4 deletions
  1. 3
    4
      src/hci/commands/ifmgmt_cmd.c

+ 3
- 4
src/hci/commands/ifmgmt_cmd.c View File

@@ -53,6 +53,7 @@ int ifcommon_exec ( int argc, char **argv,
53 53
 		    int stop_on_first_success ) {
54 54
 	struct ifcommon_options opts;
55 55
 	struct net_device *netdev;
56
+	int i;
56 57
 	int rc;
57 58
 
58 59
 	/* Parse options */
@@ -61,11 +62,9 @@ int ifcommon_exec ( int argc, char **argv,
61 62
 
62 63
 	if ( optind != argc ) {
63 64
 		/* Treat arguments as a list of interfaces to try */
64
-		while ( optind != argc ) {
65
-			if ( ( rc = parse_netdev ( argv[optind++],
66
-						   &netdev ) ) != 0 ) {
65
+		for ( i = optind ; i < argc ; i++ ) {
66
+			if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
67 67
 				continue;
68
-			}
69 68
 			if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
70 69
 			     stop_on_first_success ) {
71 70
 				return 0;

Loading…
Cancel
Save