Module Name:    src
Committed By:   rillig
Date:           Sat Oct 30 15:26:58 UTC 2021

Modified Files:
        src/tests/usr.bin/indent: t_errors.sh token_comment.c
        src/usr.bin/indent: indent.c

Log Message:
indent: revert previous fix of assertion failure

The strange code with the out of bounds memory access is needed to
transform 'if (expr) /* comment */ {' to 'if (expr) { /* comment */',
that is, to move the comment to the right.

Add a test that prevents "repairing" this code again.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/tests/usr.bin/indent/t_errors.sh
cvs rdiff -u -r1.12 -r1.13 src/tests/usr.bin/indent/token_comment.c
cvs rdiff -u -r1.187 -r1.188 src/usr.bin/indent/indent.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/indent/t_errors.sh
diff -u src/tests/usr.bin/indent/t_errors.sh:1.14 src/tests/usr.bin/indent/t_errors.sh:1.15
--- src/tests/usr.bin/indent/t_errors.sh:1.14	Sat Oct 30 13:30:26 2021
+++ src/tests/usr.bin/indent/t_errors.sh	Sat Oct 30 15:26:58 2021
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_errors.sh,v 1.14 2021/10/30 13:30:26 rillig Exp $
+# $NetBSD: t_errors.sh,v 1.15 2021/10/30 15:26:58 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -411,7 +411,7 @@ unbalanced_parentheses_3_body()
 atf_test_case 'search_stmt_comment_segv'
 search_stmt_comment_segv_body()
 {
-	# Before NetBSD indent.c 1.187 from 2021-10-30, indent crashed while
+	# As of NetBSD indent.c 1.188 from 2021-10-30, indent crashes while
 	# trying to format the following artificial code.
 
 	printf '{if(expr\n)/*c*/;}\n' > code.c
@@ -423,7 +423,9 @@ search_stmt_comment_segv_body()
 		;
 }
 EOF
-	atf_check -o 'file:code.exp' \
+
+	# TODO: actually produce code.exp instead of an assertion failure.
+	atf_check -s 'signal' -o 'ignore' -e 'match:assert' \
 	    "$indent" code.c -st
 }
 

Index: src/tests/usr.bin/indent/token_comment.c
diff -u src/tests/usr.bin/indent/token_comment.c:1.12 src/tests/usr.bin/indent/token_comment.c:1.13
--- src/tests/usr.bin/indent/token_comment.c:1.12	Sat Oct 30 13:06:43 2021
+++ src/tests/usr.bin/indent/token_comment.c	Sat Oct 30 15:26:58 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: token_comment.c,v 1.12 2021/10/30 13:06:43 rillig Exp $ */
+/* $NetBSD: token_comment.c,v 1.13 2021/10/30 15:26:58 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -325,6 +325,25 @@ tab1+++	tab2---	tab3+++	tab4---	tab5+++	
 
 
 /*
+ * Ensure that '{' after a search_stmt_comment is preserved.
+ */
+#indent input
+{
+	if(0)/*comment*/{
+	}
+}
+#indent end
+
+/* The comment in the output has moved to the right of the '{'. */
+#indent run
+{
+	if (0) {		/* comment */
+	}
+}
+#indent end
+
+
+/*
  * The following comments test line breaking when the comment ends with a
  * space.
  */

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.187 src/usr.bin/indent/indent.c:1.188
--- src/usr.bin/indent/indent.c:1.187	Sat Oct 30 13:30:26 2021
+++ src/usr.bin/indent/indent.c	Sat Oct 30 15:26:58 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.187 2021/10/30 13:30:26 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.188 2021/10/30 15:26:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c	5.1
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.187 2021/10/30 13:30:26 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.188 2021/10/30 15:26:58 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -189,10 +189,17 @@ search_stmt_comment(bool *comment_buffer
 	 * process_comment() will use that to calculate original indentation
 	 * of a boxed comment.
 	 */
-	size_t line_len = (size_t)(inp.s - inp.buf) - strlen("/*");
-	memcpy(sc_buf, inp.buf, line_len);
-	save_com = sc_buf + line_len;
-	sc_end = save_com;
+	/*
+	 * FIXME: This '4' needs an explanation. For example, in the snippet
+	 * 'if(expr)/''*comment', the 'r)' of the code is not copied. If there
+	 * is an additional line break before the ')', memcpy tries to copy
+	 * (size_t)-1 bytes.
+	 */
+	assert((size_t)(inp.s - inp.buf) >= 4);
+	memcpy(sc_buf, inp.buf, (size_t)(inp.s - inp.buf) - 4);
+	save_com = sc_buf + (inp.s - inp.buf - 4);
+	save_com[0] = save_com[1] = ' ';
+	sc_end = &save_com[2];
     }
 
     *comment_buffered = true;

Reply via email to