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: fix assertion failure in search_stmt_...



details:   https://anonhg.NetBSD.org/src/rev/4c625b63261c
branches:  trunk
changeset: 1024654:4c625b63261c
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Oct 30 13:30:26 2021 +0000

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

diffstat:

 tests/usr.bin/indent/t_errors.sh |  14 ++++++++++++--
 usr.bin/indent/indent.c          |  19 ++++++-------------
 2 files changed, 18 insertions(+), 15 deletions(-)

diffs (71 lines):

diff -r 5082d7c89e0a -r 4c625b63261c tests/usr.bin/indent/t_errors.sh
--- a/tests/usr.bin/indent/t_errors.sh  Sat Oct 30 13:06:43 2021 +0000
+++ b/tests/usr.bin/indent/t_errors.sh  Sat Oct 30 13:30:26 2021 +0000
@@ -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 @@
 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
 }
 
diff -r 5082d7c89e0a -r 4c625b63261c usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Oct 30 13:06:43 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Oct 30 13:30:26 2021 +0000
@@ -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 @@
 
 #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 @@
         * 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;



Home | Main Index | Thread Index | Old Index