summaryrefslogtreecommitdiff
path: root/tests/usr.bin
diff options
context:
space:
mode:
authorrillig <rillig@NetBSD.org>2023-06-10 17:35:40 +0000
committerrillig <rillig@NetBSD.org>2023-06-10 17:35:40 +0000
commitbd90a1ef0372f29e1cfc0d241d896ca8ab6300a1 (patch)
tree86287bc7b7fac63054a12248178bd4c7d146d6cb /tests/usr.bin
parente64099d25bfe5670c12db55575530480a27f3209 (diff)
indent: fix stack overflow, add more tests
For several parser symbols, 2 symbols are pushed in a row, which led to an out-of-bounds write.
Diffstat (limited to 'tests/usr.bin')
-rw-r--r--tests/usr.bin/indent/opt_cli.c53
-rw-r--r--tests/usr.bin/indent/psym_else.c17
-rw-r--r--tests/usr.bin/indent/t_errors.sh29
3 files changed, 96 insertions, 3 deletions
diff --git a/tests/usr.bin/indent/opt_cli.c b/tests/usr.bin/indent/opt_cli.c
index c141006a8cf..d4b657e6ac1 100644
--- a/tests/usr.bin/indent/opt_cli.c
+++ b/tests/usr.bin/indent/opt_cli.c
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_cli.c,v 1.6 2023/06/06 04:37:27 rillig Exp $ */
+/* $NetBSD: opt_cli.c,v 1.7 2023/06/10 17:35:41 rillig Exp $ */
/*
* Tests for the option '-cli' ("case label indentation"), which sets the
@@ -97,3 +97,54 @@ classify(int n)
}
}
//indent end
+
+
+/*
+ * Test the combination of left-aligned braces and a deep case indentation.
+ *
+ * When the 'case' labels are that deeply indented, the distance between the
+ * braces and the 'case' is between 1 and 2 indentation levels.
+ */
+//indent input
+{
+switch (expr)
+{
+case 1:
+}
+}
+//indent end
+
+//indent run -br -cli3.25
+{
+ switch (expr) {
+ case 1:
+ }
+}
+//indent end
+
+//indent run -bl -cli3.25
+{
+ switch (expr)
+ {
+ case 1:
+ }
+}
+//indent end
+
+//indent run -bl -cli2.75
+{
+ switch (expr)
+ {
+ case 1:
+ }
+}
+//indent end
+
+//indent run -bl -cli1.25
+{
+ switch (expr)
+ {
+ case 1:
+ }
+}
+//indent end
diff --git a/tests/usr.bin/indent/psym_else.c b/tests/usr.bin/indent/psym_else.c
index e5348fff8df..8458afe6764 100644
--- a/tests/usr.bin/indent/psym_else.c
+++ b/tests/usr.bin/indent/psym_else.c
@@ -1,4 +1,4 @@
-/* $NetBSD: psym_else.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */
+/* $NetBSD: psym_else.c,v 1.5 2023/06/10 17:35:41 rillig Exp $ */
/*
* Tests for the parser symbol psym_else, which represents the keyword 'else'
@@ -71,3 +71,18 @@ function(void)
(var = 3);
}
//indent end
+
+
+//indent input
+{
+ else
+}
+//indent end
+
+//indent run
+error: Standard Input:2: Unmatched 'else'
+{
+ else
+}
+exit 1
+//indent end
diff --git a/tests/usr.bin/indent/t_errors.sh b/tests/usr.bin/indent/t_errors.sh
index a9498e9d008..d0fc52ac67b 100644
--- a/tests/usr.bin/indent/t_errors.sh
+++ b/tests/usr.bin/indent/t_errors.sh
@@ -1,5 +1,5 @@
#! /bin/sh
-# $NetBSD: t_errors.sh,v 1.34 2023/06/09 11:22:31 rillig Exp $
+# $NetBSD: t_errors.sh,v 1.35 2023/06/10 17:35:41 rillig Exp $
#
# Copyright (c) 2021 The NetBSD Foundation, Inc.
# All rights reserved.
@@ -485,6 +485,32 @@ EOF
"$indent" -l34 code.c -st
}
+atf_test_case 'stack_overflow'
+stack_overflow_body()
+{
+ cat <<-EOF > code.c
+ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{
+ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{
+ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{
+ EOF
+
+ atf_check \
+ -s 'exit:1' \
+ -e 'inline:error: code.c:3: Stuff missing from end of file\n' \
+ "$indent" code.c
+
+ cat <<-EOF > code.c
+ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{
+ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{{{{
+ {{{{{{{{{{ {{{{{{{{{{ {{{{{{{ {
+ EOF
+
+ atf_check \
+ -s 'exit:1' \
+ -e 'inline:indent: Parser stack overflow\n' \
+ "$indent" code.c
+}
+
atf_init_test_cases()
{
@@ -524,4 +550,5 @@ atf_init_test_cases()
atf_add_test_case 'gcc_statement_expression'
atf_add_test_case 'crash_comment_after_controlling_expression'
atf_add_test_case 'comment_fits_in_one_line'
+ atf_add_test_case 'stack_overflow'
}