Module Name:    src
Committed By:   rillig
Date:           Sat Mar 13 13:14:14 UTC 2021

Modified Files:
        src/tests/usr.bin/indent: token-preprocessing.0.stdout
        src/usr.bin/indent: indent.c

Log Message:
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 '*/'.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 \
    src/tests/usr.bin/indent/token-preprocessing.0.stdout
cvs rdiff -u -r1.54 -r1.55 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/token-preprocessing.0.stdout
diff -u src/tests/usr.bin/indent/token-preprocessing.0.stdout:1.3 src/tests/usr.bin/indent/token-preprocessing.0.stdout:1.4
--- src/tests/usr.bin/indent/token-preprocessing.0.stdout:1.3	Sat Mar 13 13:04:13 2021
+++ src/tests/usr.bin/indent/token-preprocessing.0.stdout	Sat Mar 13 13:14:14 2021
@@ -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;

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.54 src/usr.bin/indent/indent.c:1.55
--- src/usr.bin/indent/indent.c:1.54	Sat Mar 13 12:52:24 2021
+++ src/usr.bin/indent/indent.c	Sat Mar 13 13:14:14 2021
@@ -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 @@ static char sccsid[] = "@(#)indent.c	5.1
 #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 @@ process_preprocessing(void)
     {
 	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 @@ process_preprocessing(void)
 		}
 		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 @@ process_preprocessing(void)
 		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) {

Reply via email to