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: revert previous fix of assertion failure



details:   https://anonhg.NetBSD.org/src/rev/67baa8dac483
branches:  trunk
changeset: 1024658:67baa8dac483
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Oct 30 15:26:58 2021 +0000

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

diffstat:

 tests/usr.bin/indent/t_errors.sh     |   8 +++++---
 tests/usr.bin/indent/token_comment.c |  21 ++++++++++++++++++++-
 usr.bin/indent/indent.c              |  19 +++++++++++++------
 3 files changed, 38 insertions(+), 10 deletions(-)

diffs (105 lines):

diff -r ab2215443a1e -r 67baa8dac483 tests/usr.bin/indent/t_errors.sh
--- a/tests/usr.bin/indent/t_errors.sh  Sat Oct 30 14:05:40 2021 +0000
+++ b/tests/usr.bin/indent/t_errors.sh  Sat Oct 30 15:26:58 2021 +0000
@@ -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 @@
 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 @@
                ;
 }
 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
 }
 
diff -r ab2215443a1e -r 67baa8dac483 tests/usr.bin/indent/token_comment.c
--- a/tests/usr.bin/indent/token_comment.c      Sat Oct 30 14:05:40 2021 +0000
+++ b/tests/usr.bin/indent/token_comment.c      Sat Oct 30 15:26:58 2021 +0000
@@ -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 @@
 
 
 /*
+ * 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.
  */
diff -r ab2215443a1e -r 67baa8dac483 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Oct 30 14:05:40 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Oct 30 15:26:58 2021 +0000
@@ -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 @@
 
 #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 @@
         * 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;



Home | Main Index | Thread Index | Old Index