From 78aeaffda9996d2db98130d2bffdc3ae17539e83 Mon Sep 17 00:00:00 2001 From: Dave Chinner Date: Wed, 8 Apr 2020 09:48:04 -0400 Subject: [PATCH] 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 Reviewed-by: Christoph Hellwig Reviewed-by: Darrick J. Wong Signed-off-by: Eric Sandeen --- copy/xfs_copy.c | 2 +- db/freesp.c | 2 +- db/init.c | 7 ++----- growfs/xfs_growfs.c | 1 - io/copy_file_range.c | 2 ++ logprint/logprint.c | 2 +- mkfs/xfs_mkfs.c | 2 +- repair/xfs_repair.c | 2 +- scrub/xfs_scrub.c | 2 -- spaceman/freesp.c | 1 - spaceman/prealloc.c | 1 - 11 files changed, 9 insertions(+), 15 deletions(-) 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(©_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); } -- 2.49.0