diff --git a/src/scrut.c b/src/scrut.c index 7eccaee..cfdd923 100644 --- a/src/scrut.c +++ b/src/scrut.c @@ -17,6 +17,7 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ +#include /* assert(3) */ #include /* fprintf(3), stderr, NULL */ #include /* EXIT_FAILURE, EXIT_SUCCESS */ #include /* memset(3), strchr(3) */ @@ -29,6 +30,7 @@ char *program_name = "scrut"; #define OPTS "bcdefgkprsuwxLS" +/* this is an array so main:sel's size can be known at compile time */ static char opts[] = OPTS; static int @@ -58,7 +60,10 @@ int main(int argc, char *argv[]) { memset(sel, '\0', sizeof sel); for (int c; (c = getopt(argc, argv, opts)) != -1;) { if ((p = strchr(opts, c)) == NULL) { return usage(argv[0]); } - else { sel[p - opts] = c; } + else { + assert(p - opts < sizeof sel / sizeof *sel); /* bounds check */ + sel[p - opts] = c; + } } /* straighten out selections; permute out nulls */ @@ -73,7 +78,7 @@ int main(int argc, char *argv[]) { if (optind == argc) { return usage(argv[0]); } - for (argv += optind ; *argv != NULL; ++argv) { + for (argv += optind ; *argv != NULL; argv = &argv[1]) { struct stat buf; #ifdef __OpenBSD__