svn commit: r345130 - head/usr.sbin/trim
Eugene Grosbein
eugen at FreeBSD.org
Thu Mar 14 12:25:17 UTC 2019
Author: eugen
Date: Thu Mar 14 12:25:16 2019
New Revision: 345130
URL: https://svnweb.freebsd.org/changeset/base/345130
Log:
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
Modified:
head/usr.sbin/trim/trim.c
Modified: head/usr.sbin/trim/trim.c
==============================================================================
--- head/usr.sbin/trim/trim.c Thu Mar 14 10:06:46 2019 (r345129)
+++ head/usr.sbin/trim/trim.c Thu Mar 14 12:25:16 2019 (r345130)
@@ -40,6 +40,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sysexits.h>
#include <unistd.h>
@@ -103,6 +104,23 @@ main(int argc, char **argv)
usage(name);
/* 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;
More information about the svn-src-head
mailing list