Sat Mar 13 13:14:14 2021 UTC ()
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 '*/'.


(rillig)
diff -r1.3 -r1.4 src/tests/usr.bin/indent/token-preprocessing.0.stdout
diff -r1.54 -r1.55 src/usr.bin/indent/indent.c

cvs diff -r1.3 -r1.4 src/tests/usr.bin/indent/Attic/token-preprocessing.0.stdout (expand / switch to unified diff)

--- src/tests/usr.bin/indent/Attic/token-preprocessing.0.stdout 2021/03/13 13:04:13 1.3
+++ src/tests/usr.bin/indent/Attic/token-preprocessing.0.stdout 2021/03/13 13:14:14 1.4
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: token-preprocessing.0.stdout,v 1.3 2021/03/13 13:04:13 rillig Exp $ */ 1/* $NetBSD: token-preprocessing.0.stdout,v 1.4 2021/03/13 13:14:14 rillig Exp $ */
2/* $FreeBSD$ */ 2/* $FreeBSD$ */
3 3
4/*- 4/*-
5 * Tests for indenting preprocessing directives: 5 * Tests for indenting preprocessing directives:
6 * 6 *
7 * #define 7 * #define
8 * #ifdef 8 * #ifdef
9 * #pragma 9 * #pragma
10 * #line 10 * #line
11 */ 11 */
12 12
13#include <system-header.h> 13#include <system-header.h>
14#include "local-header.h" 14#include "local-header.h"
@@ -24,16 +24,14 @@ @@ -24,16 +24,14 @@
24#if 0 /* outer if comment */ 24#if 0 /* outer if comment */
25/* $ XXX: The indentation is removed, which can get confusing */ 25/* $ XXX: The indentation is removed, which can get confusing */
26#if nested /* inner if comment */ 26#if nested /* inner if comment */
27#else /* inner else comment */ 27#else /* inner else comment */
28#endif /* inner endif comment */ 28#endif /* inner endif comment */
29#endif /* outer endif comment */ 29#endif /* outer endif comment */
30 30
31#define multi_line_definition /* first line 31#define multi_line_definition /* first line
32 * middle 32 * middle
33 * final line 33 * final line
34 */ actual_value 34 */ actual_value
35 35
36#define comment_in_string_literal "/* no comment " 36#define comment_in_string_literal "/* no comment "
37int this_is_an_ordinary_line_again; 37int this_is_an_ordinary_line_again;
38 
39/* $ FIXME: The above empty line is wrong. */ 

cvs diff -r1.54 -r1.55 src/usr.bin/indent/indent.c (expand / switch to unified diff)

--- src/usr.bin/indent/indent.c 2021/03/13 12:52:24 1.54
+++ src/usr.bin/indent/indent.c 2021/03/13 13:14:14 1.55
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.54 2021/03/13 12:52:24 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.55 2021/03/13 13:14:14 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1976 Board of Trustees of the University of Illinois. 7 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
8 * Copyright (c) 1980, 1993 8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved. 9 * The Regents of the University of California. All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -36,27 +36,27 @@ @@ -36,27 +36,27 @@
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#if 0 40#if 0
41#ifndef lint 41#ifndef lint
42static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93"; 42static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
43#endif /* not lint */ 43#endif /* not lint */
44#endif 44#endif
45 45
46#include <sys/cdefs.h> 46#include <sys/cdefs.h>
47#ifndef lint 47#ifndef lint
48#if defined(__NetBSD__) 48#if defined(__NetBSD__)
49__RCSID("$NetBSD: indent.c,v 1.54 2021/03/13 12:52:24 rillig Exp $"); 49__RCSID("$NetBSD: indent.c,v 1.55 2021/03/13 13:14:14 rillig Exp $");
50#elif defined(__FreeBSD__) 50#elif defined(__FreeBSD__)
51__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); 51__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
52#endif 52#endif
53#endif 53#endif
54 54
55#include <sys/param.h> 55#include <sys/param.h>
56#if HAVE_CAPSICUM 56#if HAVE_CAPSICUM
57#include <sys/capsicum.h> 57#include <sys/capsicum.h>
58#include <capsicum_helpers.h> 58#include <capsicum_helpers.h>
59#endif 59#endif
60#include <err.h> 60#include <err.h>
61#include <errno.h> 61#include <errno.h>
62#include <fcntl.h> 62#include <fcntl.h>
@@ -1111,61 +1111,65 @@ process_comma(int dec_ind, int tabs_to_v @@ -1111,61 +1111,65 @@ process_comma(int dec_ind, int tabs_to_v
1111} 1111}
1112 1112
1113static void 1113static void
1114process_preprocessing(void) 1114process_preprocessing(void)
1115{ 1115{
1116 if (s_com != e_com || s_lab != e_lab || s_code != e_code) 1116 if (s_com != e_com || s_lab != e_lab || s_code != e_code)
1117 dump_line(); 1117 dump_line();
1118 check_size_label(1); 1118 check_size_label(1);
1119 *e_lab++ = '#'; /* move whole line to 'label' buffer */ 1119 *e_lab++ = '#'; /* move whole line to 'label' buffer */
1120 1120
1121 { 1121 {
1122 int in_comment = 0; 1122 int in_comment = 0;
1123 int com_start = 0; 1123 int com_start = 0;
1124 char quote = 0; 1124 char quote = '\0';
1125 int com_end = 0; 1125 int com_end = 0;
1126 1126
1127 while (*buf_ptr == ' ' || *buf_ptr == '\t') { 1127 while (*buf_ptr == ' ' || *buf_ptr == '\t') {
1128 buf_ptr++; 1128 buf_ptr++;
1129 if (buf_ptr >= buf_end) 1129 if (buf_ptr >= buf_end)
1130 fill_buffer(); 1130 fill_buffer();
1131 } 1131 }
1132 while (*buf_ptr != '\n' || (in_comment && !had_eof)) { 1132 while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
1133 check_size_label(2); 1133 check_size_label(2);
1134 *e_lab = *buf_ptr++; 1134 *e_lab = *buf_ptr++;
1135 if (buf_ptr >= buf_end) 1135 if (buf_ptr >= buf_end)
1136 fill_buffer(); 1136 fill_buffer();
1137 switch (*e_lab++) { 1137 switch (*e_lab++) {
1138 case '\\': 1138 case '\\':
1139 if (!in_comment) { 1139 if (!in_comment) {
1140 *e_lab++ = *buf_ptr++; 1140 *e_lab++ = *buf_ptr++;
1141 if (buf_ptr >= buf_end) 1141 if (buf_ptr >= buf_end)
1142 fill_buffer(); 1142 fill_buffer();
1143 } 1143 }
1144 break; 1144 break;
1145 case '/': 1145 case '/':
1146 if (*buf_ptr == '*' && !in_comment && !quote) { 1146 if (*buf_ptr == '*' && !in_comment && quote == '\0') {
1147 in_comment = 1; 1147 in_comment = 1;
1148 *e_lab++ = *buf_ptr++; 1148 *e_lab++ = *buf_ptr++;
1149 com_start = e_lab - s_lab - 2; 1149 com_start = e_lab - s_lab - 2;
1150 } 1150 }
1151 break; 1151 break;
1152 case '"': 1152 case '"':
1153 if (quote == '"') 1153 if (quote == '"')
1154 quote = 0; 1154 quote = '\0';
 1155 else if (quote == '\0')
 1156 quote = '"';
1155 break; 1157 break;
1156 case '\'': 1158 case '\'':
1157 if (quote == '\'') 1159 if (quote == '\'')
1158 quote = 0; 1160 quote = '\0';
 1161 else if (quote == '\0')
 1162 quote = '\'';
1159 break; 1163 break;
1160 case '*': 1164 case '*':
1161 if (*buf_ptr == '/' && in_comment) { 1165 if (*buf_ptr == '/' && in_comment) {
1162 in_comment = 0; 1166 in_comment = 0;
1163 *e_lab++ = *buf_ptr++; 1167 *e_lab++ = *buf_ptr++;
1164 com_end = e_lab - s_lab; 1168 com_end = e_lab - s_lab;
1165 } 1169 }
1166 break; 1170 break;
1167 } 1171 }
1168 } 1172 }
1169 1173
1170 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 1174 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1171 e_lab--; 1175 e_lab--;