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

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

Log Message:
indent: fix assertion failure in search_stmt_comment

I have no idea why the code was written in such a convoluted way before.
By removing all the code that didn't make sense, everything just works
as expected, and the existing tests all pass, especially those in
token_comment.c that mention search_stmt_comment.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/tests/usr.bin/indent/t_errors.sh
cvs rdiff -u -r1.186 -r1.187 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.13 src/tests/usr.bin/indent/t_errors.sh:1.14
--- src/tests/usr.bin/indent/t_errors.sh:1.13	Fri Oct 29 20:05:58 2021
+++ src/tests/usr.bin/indent/t_errors.sh	Sat Oct 30 13:30:26 2021
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_errors.sh,v 1.13 2021/10/29 20:05:58 rillig Exp $
+# $NetBSD: t_errors.sh,v 1.14 2021/10/30 13:30:26 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -411,9 +411,19 @@ 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
+	# trying to format the following artificial code.
+
 	printf '{if(expr\n)/*c*/;}\n' > code.c
 
-	atf_check -s 'signal' -o 'ignore' -e 'match:assert' \
+	cat <<\EOF > code.exp
+{
+	if (expr
+		)		/* c */
+		;
+}
+EOF
+	atf_check -o 'file:code.exp' \
 	    "$indent" code.c -st
 }
 

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.186 src/usr.bin/indent/indent.c:1.187
--- src/usr.bin/indent/indent.c:1.186	Sat Oct 30 11:37:38 2021
+++ src/usr.bin/indent/indent.c	Sat Oct 30 13:30:26 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.186 2021/10/30 11:37:38 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.187 2021/10/30 13:30:26 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.186 2021/10/30 11:37:38 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.187 2021/10/30 13:30:26 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -189,17 +189,10 @@ search_stmt_comment(bool *comment_buffer
 	 * process_comment() will use that to calculate original indentation
 	 * of a boxed comment.
 	 */
-	/*
-	 * 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];
+	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;
     }
 
     *comment_buffered = true;

Reply via email to