Module Name: src Committed By: rillig Date: Wed Oct 13 23:33:52 UTC 2021
Modified Files: src/distrib/sets/lists/tests: mi src/tests/usr.bin/indent: Makefile opt.0.pro src/usr.bin/indent: args.c Added Files: src/tests/usr.bin/indent: t_errors.sh Log Message: indent: check command line options stricter Previously, bool options were allowed to have trailing garbage. For example, the option '-bacc' could be spelled '-bacchus' as well. Check that the exact option name is given in the command line, to prevent typos in the configuration files and to reduce surprises just in case a future option is a prefix of an existing option, or vice versa. Add a new test program for error handling. Most of these tests are so simple that it would be overkill to create 3 files for each test. To generate a diff of this commit: cvs rdiff -u -r1.1136 -r1.1137 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.13 -r1.14 src/tests/usr.bin/indent/Makefile cvs rdiff -u -r1.1 -r1.2 src/tests/usr.bin/indent/opt.0.pro cvs rdiff -u -r0 -r1.1 src/tests/usr.bin/indent/t_errors.sh cvs rdiff -u -r1.54 -r1.55 src/usr.bin/indent/args.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.1136 src/distrib/sets/lists/tests/mi:1.1137 --- src/distrib/sets/lists/tests/mi:1.1136 Wed Oct 13 04:57:20 2021 +++ src/distrib/sets/lists/tests/mi Wed Oct 13 23:33:52 2021 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.1136 2021/10/13 04:57:20 thorpej Exp $ +# $NetBSD: mi,v 1.1137 2021/10/13 23:33:52 rillig Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -5005,6 +5005,7 @@ ./usr/tests/usr.bin/indent/surplusbad.0 tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/indent/surplusbad.0.pro tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/indent/surplusbad.0.stdout tests-usr.bin-tests compattestfile,atf +./usr/tests/usr.bin/indent/t_errors tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/indent/t_indent tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/indent/token-binary_op.0 tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/indent/token-binary_op.0.pro tests-usr.bin-tests compattestfile,atf Index: src/tests/usr.bin/indent/Makefile diff -u src/tests/usr.bin/indent/Makefile:1.13 src/tests/usr.bin/indent/Makefile:1.14 --- src/tests/usr.bin/indent/Makefile:1.13 Thu Oct 7 18:07:25 2021 +++ src/tests/usr.bin/indent/Makefile Wed Oct 13 23:33:52 2021 @@ -1,9 +1,10 @@ -# $NetBSD: Makefile,v 1.13 2021/10/07 18:07:25 rillig Exp $ +# $NetBSD: Makefile,v 1.14 2021/10/13 23:33:52 rillig Exp $ .include <bsd.own.mk> TESTSDIR= ${TESTSBASE}/usr.bin/indent -TESTS_SH= t_indent +TESTS_SH= t_errors +TESTS_SH+= t_indent FILESDIR= ${TESTSDIR} FILES= binary.0 Index: src/tests/usr.bin/indent/opt.0.pro diff -u src/tests/usr.bin/indent/opt.0.pro:1.1 src/tests/usr.bin/indent/opt.0.pro:1.2 --- src/tests/usr.bin/indent/opt.0.pro:1.1 Thu Oct 7 18:07:25 2021 +++ src/tests/usr.bin/indent/opt.0.pro Wed Oct 13 23:33:52 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: opt.0.pro,v 1.1 2021/10/07 18:07:25 rillig Exp $ */ +/* $NetBSD: opt.0.pro,v 1.2 2021/10/13 23:33:52 rillig Exp $ */ /* $FreeBSD$ */ /* The latter of the two options wins. */ @@ -18,10 +18,3 @@ -cli3.5garbage -b/*/acc /* The comment is '/' '*' '/', making the option '-bacc'. */ - -/* - * For boolean options, trailing garbage is ignored. - * - * See strncmp - */ --bacchus /* Is interpreted as '-bacc'. */ Index: src/usr.bin/indent/args.c diff -u src/usr.bin/indent/args.c:1.54 src/usr.bin/indent/args.c:1.55 --- src/usr.bin/indent/args.c:1.54 Fri Oct 8 20:07:44 2021 +++ src/usr.bin/indent/args.c Wed Oct 13 23:33:52 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: args.c,v 1.54 2021/10/08 20:07:44 rillig Exp $ */ +/* $NetBSD: args.c,v 1.55 2021/10/13 23:33:52 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)args.c 8.1 ( #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: args.c,v 1.54 2021/10/08 20:07:44 rillig Exp $"); +__RCSID("$NetBSD: args.c,v 1.55 2021/10/13 23:33:52 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $"); #endif @@ -288,15 +288,16 @@ set_option(const char *arg, const char * if (param_start != NULL) goto found; } - errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1); + errx(1, "%s: unknown option \"-%s\"", option_source, arg); found: if (p->p_is_bool) { - /* XXX: Trailing garbage in param_start is silently ignored. */ + if (param_start[0] != '\0') + errx(1, "%s: unknown option \"-%s\"", option_source, arg); *(bool *)p->p_var = p->p_may_negate ? arg[0] != 'n' : p->p_bool_value; } else { if (!isdigit((unsigned char)*param_start)) - errx(1, "%s: ``%s'' requires a parameter", + errx(1, "%s: option \"-%s\" requires an integer parameter", option_source, p->p_name); *(int *)p->p_var = atoi(param_start); } Added files: Index: src/tests/usr.bin/indent/t_errors.sh diff -u /dev/null src/tests/usr.bin/indent/t_errors.sh:1.1 --- /dev/null Wed Oct 13 23:33:52 2021 +++ src/tests/usr.bin/indent/t_errors.sh Wed Oct 13 23:33:52 2021 @@ -0,0 +1,63 @@ +#! /bin/sh +# $NetBSD: t_errors.sh,v 1.1 2021/10/13 23:33:52 rillig Exp $ +# +# Copyright (c) 2021 The NetBSD Foundation, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# $FreeBSD$ + +indent=$(atf_config_get usr.bin.indent.test_indent /usr/bin/indent) +nl=' +' + +atf_test_case 'option_unknown' +option_unknown_body() +{ + atf_check -s 'exit:1' \ + -e 'inline:indent: Command line: unknown option "-Z-unknown"'"$nl" \ + "$indent" -Z-unknown +} + +atf_test_case 'option_bool_trailing_garbage' +option_bool_trailing_garbage_body() +{ + atf_check -s 'exit:1' \ + -e 'inline:indent: Command line: unknown option "-bacchus"'"$nl" \ + "$indent" -bacchus +} + +atf_test_case 'option_int_missing_parameter' +option_int_missing_parameter_body() +{ + atf_check -s 'exit:1' \ + -e 'inline:indent: Command line: option "-ts" requires an integer parameter'"$nl" \ + "$indent" -tsx +} + +atf_init_test_cases() +{ + atf_add_test_case 'option_unknown' + atf_add_test_case 'option_bool_trailing_garbage' + atf_add_test_case 'option_int_missing_parameter' +}