]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[ifmgmt] Avoid relying on global variable within ifcommon_exec()
authorMichael Brown <mcb30@ipxe.org>
Mon, 15 Jul 2013 15:30:39 +0000 (17:30 +0200)
committerMichael Brown <mcb30@ipxe.org>
Mon, 15 Jul 2013 15:35:48 +0000 (17:35 +0200)
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>
src/hci/commands/ifmgmt_cmd.c

index 3f3f6b51cd220e94ea4cfe7ad5b9dca5419ce912..350f14d07a8085e8f7066fd5d03fe12868147f06 100644 (file)
@@ -53,6 +53,7 @@ int ifcommon_exec ( int argc, char **argv,
                    int stop_on_first_success ) {
        struct ifcommon_options opts;
        struct net_device *netdev;
+       int i;
        int rc;
 
        /* Parse options */
@@ -61,11 +62,9 @@ int ifcommon_exec ( int argc, char **argv,
 
        if ( optind != argc ) {
                /* Treat arguments as a list of interfaces to try */
-               while ( optind != argc ) {
-                       if ( ( rc = parse_netdev ( argv[optind++],
-                                                  &netdev ) ) != 0 ) {
+               for ( i = optind ; i < argc ; i++ ) {
+                       if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
                                continue;
-                       }
                        if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
                             stop_on_first_success ) {
                                return 0;