From: Dave Chinner <dchinner@redhat.com>
Date: Wed, 8 Apr 2020 13:48:04 +0000 (-0400)
Subject: xfsprogs: fix silently broken option parsing
X-Git-Tag: v5.6.0-rc1~10
X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=78aeaffda9996d2db98130d2bffdc3ae17539e83;p=users%2Fhch%2Fxfsprogs.git

xfsprogs: fix silently broken option parsing

When getopt() is passed an option string like "-m -n" and the
parameter m is defined as "m:", getopt returns a special error
to indication that the optstring started with a "-". Any getopt()
caller that is just catching the "?" error character will not
not catch this special error, so it silently eats the parameter
following -m.

Lots of getopt loops in xfsprogs have this issue. Convert them all
to just use a "default:" to catch anything unexpected.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
---

diff --git a/copy/xfs_copy.c b/copy/xfs_copy.c
index 91c2ae016..c4f9f3498 100644
--- a/copy/xfs_copy.c
+++ b/copy/xfs_copy.c
@@ -584,7 +584,7 @@ main(int argc, char **argv)
 		case 'V':
 			printf(_("%s version %s\n"), progname, VERSION);
 			exit(0);
-		case '?':
+		default:
 			usage();
 		}
 	}
diff --git a/db/freesp.c b/db/freesp.c
index 903c60d73..6f2346665 100644
--- a/db/freesp.c
+++ b/db/freesp.c
@@ -177,7 +177,7 @@ init(
 		case 's':
 			summaryflag = 1;
 			break;
-		case '?':
+		default:
 			return usage();
 		}
 	}
diff --git a/db/init.c b/db/init.c
index 61eea111f..ac649fbdd 100644
--- a/db/init.c
+++ b/db/init.c
@@ -84,15 +84,12 @@ init(
 		case 'V':
 			printf(_("%s version %s\n"), progname, VERSION);
 			exit(0);
-		case '?':
+		default:
 			usage();
-			/*NOTREACHED*/
 		}
 	}
-	if (optind + 1 != argc) {
+	if (optind + 1 != argc)
 		usage();
-		/*NOTREACHED*/
-	}
 
 	fsdevice = argv[optind];
 	if (!x.disfile)
diff --git a/growfs/xfs_growfs.c b/growfs/xfs_growfs.c
index d27e3b94e..a68b515de 100644
--- a/growfs/xfs_growfs.c
+++ b/growfs/xfs_growfs.c
@@ -120,7 +120,6 @@ main(int argc, char **argv)
 		case 'V':
 			printf(_("%s version %s\n"), progname, VERSION);
 			exit(0);
-		case '?':
 		default:
 			usage();
 		}
diff --git a/io/copy_file_range.c b/io/copy_file_range.c
index fb5702e1f..4c4332c6e 100644
--- a/io/copy_file_range.c
+++ b/io/copy_file_range.c
@@ -127,6 +127,8 @@ copy_range_f(int argc, char **argv)
 			/* Expect no src_path arg */
 			src_path_arg = 0;
 			break;
+		default:
+			return command_usage(&copy_range_cmd);
 		}
 	}
 
diff --git a/logprint/logprint.c b/logprint/logprint.c
index 511a32aca..e882c5d44 100644
--- a/logprint/logprint.c
+++ b/logprint/logprint.c
@@ -193,7 +193,7 @@ main(int argc, char **argv)
 			case 'V':
 				printf(_("%s version %s\n"), progname, VERSION);
 				exit(0);
-			case '?':
+			default:
 				usage();
 		}
 	}
diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c
index f14ce8db5..039b1dcc5 100644
--- a/mkfs/xfs_mkfs.c
+++ b/mkfs/xfs_mkfs.c
@@ -3679,7 +3679,7 @@ main(
 		case 'V':
 			printf(_("%s version %s\n"), progname, VERSION);
 			exit(0);
-		case '?':
+		default:
 			unknown(optopt, "");
 		}
 	}
diff --git a/repair/xfs_repair.c b/repair/xfs_repair.c
index 4d37ddc64..e509fdeb6 100644
--- a/repair/xfs_repair.c
+++ b/repair/xfs_repair.c
@@ -326,7 +326,7 @@ process_args(int argc, char **argv)
 		case 'e':
 			report_corrected = true;
 			break;
-		case '?':
+		default:
 			usage();
 		}
 	}
diff --git a/scrub/xfs_scrub.c b/scrub/xfs_scrub.c
index 014c54dd7..33b876f21 100644
--- a/scrub/xfs_scrub.c
+++ b/scrub/xfs_scrub.c
@@ -671,8 +671,6 @@ main(
 		case 'x':
 			scrub_data = true;
 			break;
-		case '?':
-			/* fall through */
 		default:
 			usage();
 		}
diff --git a/spaceman/freesp.c b/spaceman/freesp.c
index 92cdb7439..de301c195 100644
--- a/spaceman/freesp.c
+++ b/spaceman/freesp.c
@@ -310,7 +310,6 @@ init(
 		case 's':
 			summaryflag = 1;
 			break;
-		case '?':
 		default:
 			return command_usage(&freesp_cmd);
 		}
diff --git a/spaceman/prealloc.c b/spaceman/prealloc.c
index e5d857bdd..6fcbb4611 100644
--- a/spaceman/prealloc.c
+++ b/spaceman/prealloc.c
@@ -56,7 +56,6 @@ prealloc_f(
 			eofb.eof_min_file_size = cvtnum(fsgeom->blocksize,
 					fsgeom->sectsize, optarg);
 			break;
-		case '?':
 		default:
 			return command_usage(&prealloc_cmd);
 		}