From 7f654915764168b33235ccec82f76c06ed1e466c Mon Sep 17 00:00:00 2001 From: Eugene Grosbein Date: Thu, 14 Mar 2019 12:25:16 +0000 Subject: [PATCH] trim(8): add another safety net It is quite easy make a mistake and run something like this: trim -f /dev/da0 -r rfile This would trim the whole device then emit an error on non-existing file -r. Add another check to prevent this while allowing this form still for real object names beginning from dash: trim -f -- /dev/da0 -r rfile MFC after: 1 week --- usr.sbin/trim/trim.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/usr.sbin/trim/trim.c b/usr.sbin/trim/trim.c index 929827a98777..37c90d87779c 100644 --- a/usr.sbin/trim/trim.c +++ b/usr.sbin/trim/trim.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -104,6 +105,23 @@ main(int argc, char **argv) /* NOTREACHED */ } + /* + * Safety net: do not allow mistakes like + * + * trim -f /dev/da0 -r rfile + * + * This would trim whole device then error on non-existing file -r. + * Following check prevents this while allowing this form still: + * + * trim -f -- /dev/da0 -r rfile + */ + + if (strcmp(argv[optind-1], "--") != 0) { + for (ch = optind; ch < argc; ch++) + if (argv[ch][0] == '-') + usage(name); + } + argv += optind; argc -= optind;