Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: check command line options stricter



details:   https://anonhg.NetBSD.org/src/rev/b6823a9001d6
branches:  trunk
changeset: 1024246:b6823a9001d6
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Oct 13 23:33:52 2021 +0000

description:
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.

diffstat:

 distrib/sets/lists/tests/mi      |   3 +-
 tests/usr.bin/indent/Makefile    |   5 +-
 tests/usr.bin/indent/opt.0.pro   |   9 +-----
 tests/usr.bin/indent/t_errors.sh |  63 ++++++++++++++++++++++++++++++++++++++++
 usr.bin/indent/args.c            |  11 +++---
 5 files changed, 75 insertions(+), 16 deletions(-)

diffs (158 lines):

diff -r e0572afb1fd7 -r b6823a9001d6 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Wed Oct 13 23:03:10 2021 +0000
+++ b/distrib/sets/lists/tests/mi       Wed Oct 13 23:33:52 2021 +0000
@@ -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
diff -r e0572afb1fd7 -r b6823a9001d6 tests/usr.bin/indent/Makefile
--- a/tests/usr.bin/indent/Makefile     Wed Oct 13 23:03:10 2021 +0000
+++ b/tests/usr.bin/indent/Makefile     Wed Oct 13 23:33:52 2021 +0000
@@ -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
diff -r e0572afb1fd7 -r b6823a9001d6 tests/usr.bin/indent/opt.0.pro
--- a/tests/usr.bin/indent/opt.0.pro    Wed Oct 13 23:03:10 2021 +0000
+++ b/tests/usr.bin/indent/opt.0.pro    Wed Oct 13 23:33:52 2021 +0000
@@ -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'. */
diff -r e0572afb1fd7 -r b6823a9001d6 tests/usr.bin/indent/t_errors.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/indent/t_errors.sh  Wed Oct 13 23:33:52 2021 +0000
@@ -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'
+}
diff -r e0572afb1fd7 -r b6823a9001d6 usr.bin/indent/args.c
--- a/usr.bin/indent/args.c     Wed Oct 13 23:03:10 2021 +0000
+++ b/usr.bin/indent/args.c     Wed Oct 13 23:33:52 2021 +0000
@@ -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 @@
 
 #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 @@
        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);
     }



Home | Main Index | Thread Index | Old Index