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 handling of '/*' in string litera...



details:   https://anonhg.NetBSD.org/src/rev/b855ecf61257
branches:  trunk
changeset: 1019523:b855ecf61257
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Mar 13 13:14:14 2021 +0000

description:
indent: fix handling of '/*' in string literal in preprocessing line

Previously, the '/*' in the string literal had been interpreted as the
beginning of a comment, which was wrong.  Because of that, the variable
declaration in the following line was still interpreted as part of the
comment.  The comment even continued until the end of the file.

Due to indent's forgiving nature, it neither complained nor even
mentioned that anything had gone wrong.  The decision of rather
producing wrong output than failing early is a dangerous one.

At least, there should have been an error message that at the end of the
file, the parser was still in a a comment, expecting the closing '*/'.

diffstat:

 tests/usr.bin/indent/token-preprocessing.0.stdout |   6 ++----
 usr.bin/indent/indent.c                           |  16 ++++++++++------
 2 files changed, 12 insertions(+), 10 deletions(-)

diffs (71 lines):

diff -r fdfa0be0761a -r b855ecf61257 tests/usr.bin/indent/token-preprocessing.0.stdout
--- a/tests/usr.bin/indent/token-preprocessing.0.stdout Sat Mar 13 13:04:13 2021 +0000
+++ b/tests/usr.bin/indent/token-preprocessing.0.stdout Sat Mar 13 13:14:14 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: token-preprocessing.0.stdout,v 1.3 2021/03/13 13:04:13 rillig Exp $ */
+/* $NetBSD: token-preprocessing.0.stdout,v 1.4 2021/03/13 13:14:14 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*-
@@ -34,6 +34,4 @@
  */ actual_value
 
 #define comment_in_string_literal "/* no comment "
-int this_is_an_ordinary_line_again;
-
-/* $ FIXME: The above empty line is wrong. */
+int            this_is_an_ordinary_line_again;
diff -r fdfa0be0761a -r b855ecf61257 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Mar 13 13:04:13 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Mar 13 13:14:14 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.54 2021/03/13 12:52:24 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.55 2021/03/13 13:14:14 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.54 2021/03/13 12:52:24 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.55 2021/03/13 13:14:14 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -1121,7 +1121,7 @@
     {
        int         in_comment = 0;
        int         com_start = 0;
-       char        quote = 0;
+       char        quote = '\0';
        int         com_end = 0;
 
        while (*buf_ptr == ' ' || *buf_ptr == '\t') {
@@ -1143,7 +1143,7 @@
                }
                break;
            case '/':
-               if (*buf_ptr == '*' && !in_comment && !quote) {
+               if (*buf_ptr == '*' && !in_comment && quote == '\0') {
                    in_comment = 1;
                    *e_lab++ = *buf_ptr++;
                    com_start = e_lab - s_lab - 2;
@@ -1151,11 +1151,15 @@
                break;
            case '"':
                if (quote == '"')
-                   quote = 0;
+                   quote = '\0';
+               else if (quote == '\0')
+                   quote = '"';
                break;
            case '\'':
                if (quote == '\'')
-                   quote = 0;
+                   quote = '\0';
+               else if (quote == '\0')
+                   quote = '\'';
                break;
            case '*':
                if (*buf_ptr == '/' && in_comment) {



Home | Main Index | Thread Index | Old Index