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: parse option '-cli' strictly



details:   https://anonhg.NetBSD.org/src/rev/68afca4f7af7
branches:  trunk
changeset: 990483:68afca4f7af7
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Oct 28 21:02:04 2021 +0000

description:
indent: parse option '-cli' strictly

diffstat:

 tests/usr.bin/indent/t_errors.sh |  11 ++++++++++-
 tests/usr.bin/indent/t_misc.sh   |  12 ++++--------
 usr.bin/indent/args.c            |  17 ++++++++---------
 3 files changed, 22 insertions(+), 18 deletions(-)

diffs (107 lines):

diff -r 50ea8be9cc60 -r 68afca4f7af7 tests/usr.bin/indent/t_errors.sh
--- a/tests/usr.bin/indent/t_errors.sh  Thu Oct 28 20:56:32 2021 +0000
+++ b/tests/usr.bin/indent/t_errors.sh  Thu Oct 28 21:02:04 2021 +0000
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_errors.sh,v 1.5 2021/10/24 17:19:49 rillig Exp $
+# $NetBSD: t_errors.sh,v 1.6 2021/10/28 21:02:05 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -135,6 +135,14 @@
            -i3garbage
 }
 
+atf_test_case 'option_cli_trailing_garbage'
+option_cli_trailing_garbage_body()
+{
+       expect_error \
+           'indent: Command line: argument "3garbage" to option "-cli" must be numeric' \
+           -cli3garbage
+}
+
 atf_test_case 'option_buffer_overflow'
 option_buffer_overflow_body()
 {
@@ -354,6 +362,7 @@
        atf_add_test_case 'option_tabsize_large'
        atf_add_test_case 'option_tabsize_very_large'
        atf_add_test_case 'option_int_trailing_garbage'
+       atf_add_test_case 'option_cli_trailing_garbage'
        atf_add_test_case 'option_indent_size_zero'
        atf_add_test_case 'unterminated_comment'
        atf_add_test_case 'in_place_wrong_backup'
diff -r 50ea8be9cc60 -r 68afca4f7af7 tests/usr.bin/indent/t_misc.sh
--- a/tests/usr.bin/indent/t_misc.sh    Thu Oct 28 20:56:32 2021 +0000
+++ b/tests/usr.bin/indent/t_misc.sh    Thu Oct 28 21:02:04 2021 +0000
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_misc.sh,v 1.8 2021/10/24 17:19:49 rillig Exp $
+# $NetBSD: t_misc.sh,v 1.9 2021/10/28 21:02:05 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -216,15 +216,11 @@
 -T/* define
 a type */custom_type
 
-/* For int options, trailing garbage would lead to an error message. */
+/* For int options, trailing garbage would be an error. */
 -i3
 
-/*
- * For float options, trailing garbage is ignored.
- *
- * See atof.
- */
--cli3.5garbage
+/*For float options, trailing garbage would be an error. */
+-cli3.5
 
 -b/*/acc       /* The comment is '/' '*' '/', making the option '-bacc'. */
 EOF
diff -r 50ea8be9cc60 -r 68afca4f7af7 usr.bin/indent/args.c
--- a/usr.bin/indent/args.c     Thu Oct 28 20:56:32 2021 +0000
+++ b/usr.bin/indent/args.c     Thu Oct 28 21:02:04 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: args.c,v 1.62 2021/10/28 20:49:36 rillig Exp $ */
+/*     $NetBSD: args.c,v 1.63 2021/10/28 21:02:04 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.62 2021/10/28 20:49:36 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.63 2021/10/28 21:02:04 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
 #endif
@@ -73,12 +73,7 @@
 #define int_option(name, var, min, max) \
        {name, false, false, false, min, max, assert_type(&(opt.var), int *)}
 
-/*
- * N.B.: an option whose name is a prefix of another option must come earlier;
- * for example, "l" must come before "lp".
- *
- * See set_special_option for special options.
- */
+/* See set_special_option for special options. */
 static const struct pro {
     const char p_name[5];      /* e.g. "bl", "cli" */
     bool p_is_bool;
@@ -170,7 +165,11 @@
        arg_end = arg + 3;
        if (arg_end[0] == '\0')
            goto need_param;
-       opt.case_indent = (float)atof(arg_end);
+       char *end;
+       opt.case_indent = (float)strtod(arg_end, &end);
+       if (*end != '\0')
+           errx(1, "%s: argument \"%s\" to option \"-%.*s\" must be numeric",
+                option_source, arg_end, (int)(arg_end - arg), arg);
        return true;
     }
 



Home | Main Index | Thread Index | Old Index