Module Name:    src
Committed By:   rillig
Date:           Sat Jun 10 12:59:31 UTC 2023

Modified Files:
        src/usr.bin/indent: indent.c indent.h io.c lexi.c pr_comment.c

Log Message:
indent: in debug mode, null-terminate buffers


To generate a diff of this commit:
cvs rdiff -u -r1.352 -r1.353 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.185 -r1.186 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.214 -r1.215 src/usr.bin/indent/io.c
cvs rdiff -u -r1.222 -r1.223 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.159 -r1.160 src/usr.bin/indent/pr_comment.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.352 src/usr.bin/indent/indent.c:1.353
--- src/usr.bin/indent/indent.c:1.352	Sat Jun 10 08:17:04 2023
+++ src/usr.bin/indent/indent.c	Sat Jun 10 12:59:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.352 2023/06/10 08:17:04 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.352 2023/06/10 08:17:04 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -110,12 +110,23 @@ buf_expand(struct buffer *buf, size_t ad
 	buf->s = nonnull(realloc(buf->s, buf->cap));
 }
 
+#ifdef debug
+void
+buf_terminate(struct buffer *buf)
+{
+	if (buf->len == buf->cap)
+		buf_expand(buf, 1);
+	buf->s[buf->len] = '\0';
+}
+#endif
+
 void
 buf_add_char(struct buffer *buf, char ch)
 {
 	if (buf->len == buf->cap)
 		buf_expand(buf, 1);
 	buf->s[buf->len++] = ch;
+	buf_terminate(buf);
 }
 
 void
@@ -127,6 +138,7 @@ buf_add_chars(struct buffer *buf, const 
 		buf_expand(buf, len);
 	memcpy(buf->s + buf->len, s, len);
 	buf->len += len;
+	buf_terminate(buf);
 }
 
 static void
@@ -327,7 +339,7 @@ move_com_to_code(lexer_symbol lsym)
 	if (ps.want_blank)
 		buf_add_char(&code, ' ');
 	buf_add_buf(&code, &com);
-	com.len = 0;
+	buf_clear(&com);
 	ps.want_blank = lsym != lsym_rparen && lsym != lsym_rbracket;
 }
 
@@ -433,6 +445,7 @@ read_preprocessing_line(void)
 
 	while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1]))
 		lab.len--;
+	buf_terminate(&lab);
 }
 
 static void
@@ -805,7 +818,7 @@ process_colon_label(void)
 {
 	buf_add_buf(&lab, &code);
 	buf_add_char(&lab, ':');
-	code.len = 0;
+	buf_clear(&code);
 
 	if (ps.seen_case)
 		out.line_kind = lk_case_or_default;

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.185 src/usr.bin/indent/indent.h:1.186
--- src/usr.bin/indent/indent.h:1.185	Sat Jun 10 07:42:41 2023
+++ src/usr.bin/indent/indent.h	Sat Jun 10 12:59:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.185 2023/06/10 07:42:41 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.186 2023/06/10 12:59:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -141,7 +141,7 @@ typedef enum parser_symbol {
 	psym_while_expr,	/* 'while' '(' expr ')' */
 } parser_symbol;
 
-/* A range of characters, not null-terminated. */
+/* A range of characters, only null-terminated in debug mode. */
 struct buffer {
 	char *s;
 	size_t len;
@@ -522,3 +522,16 @@ next_tab(int ind)
 {
 	return ind - ind % opt.tabsize + opt.tabsize;
 }
+
+#ifdef debug
+void buf_terminate(struct buffer *);
+#else
+#define buf_terminate(buf) debug_noop()
+#endif
+
+static inline void
+buf_clear(struct buffer *buf)
+{
+	buf->len = 0;
+	buf_terminate(buf);
+}

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.214 src/usr.bin/indent/io.c:1.215
--- src/usr.bin/indent/io.c:1.214	Sat Jun 10 11:01:58 2023
+++ src/usr.bin/indent/io.c	Sat Jun 10 12:59:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.214 2023/06/10 11:01:58 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.215 2023/06/10 12:59:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.214 2023/06/10 11:01:58 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.215 2023/06/10 12:59:31 rillig Exp $");
 
 #include <stdio.h>
 
@@ -61,7 +61,7 @@ static int paren_indent;
 static void
 inp_read_next_line(FILE *f)
 {
-	inp.len = 0;
+	buf_clear(&inp);
 
 	for (;;) {
 		int ch = getc(f);
@@ -79,6 +79,7 @@ inp_read_next_line(FILE *f)
 		if (ch == '\n')
 			break;
 	}
+	buf_terminate(&inp);
 	inp_p = inp.s;
 }
 
@@ -86,7 +87,7 @@ void
 inp_read_line(void)
 {
 	if (indent_enabled == indent_on)
-		out.indent_off_text.len = 0;
+		buf_clear(&out.indent_off_text);
 	buf_add_chars(&out.indent_off_text, inp.s, inp.len);
 	inp_read_next_line(input);
 }
@@ -316,6 +317,7 @@ output_line_comment(void)
 
 	while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
 		com.len--;
+	buf_terminate(&com);
 
 	write_indent(target_ind);
 	write_range(p, com.len - (size_t)(p - com.s));
@@ -377,12 +379,12 @@ output_line(void)
 	else if (indent_enabled == indent_last_off_line) {
 		indent_enabled = indent_on;
 		write_range(out.indent_off_text.s, out.indent_off_text.len);
-		out.indent_off_text.len = 0;
+		buf_clear(&out.indent_off_text);
 	}
 
-	lab.len = 0;
-	code.len = 0;
-	com.len = 0;
+	buf_clear(&lab);
+	buf_clear(&code);
+	buf_clear(&com);
 
 	ps.line_has_decl = ps.in_decl;
 	ps.line_has_func_def = false;

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.222 src/usr.bin/indent/lexi.c:1.223
--- src/usr.bin/indent/lexi.c:1.222	Sat Jun 10 07:42:41 2023
+++ src/usr.bin/indent/lexi.c	Sat Jun 10 12:59:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.222 2023/06/10 07:42:41 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.223 2023/06/10 12:59:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.222 2023/06/10 07:42:41 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.223 2023/06/10 12:59:31 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -529,7 +529,7 @@ lex_indent_comment(void)
 lexer_symbol
 lexi(void)
 {
-	token.len = 0;
+	buf_clear(&token);
 	ps.curr_col_1 = ps.next_col_1;
 	ps.next_col_1 = false;
 
@@ -664,7 +664,7 @@ lexi(void)
 			enum indent_enabled prev = indent_enabled;
 			lex_indent_comment();
 			if (prev == indent_on && indent_enabled == indent_off)
-				out.indent_off_text.len = 0;
+				buf_clear(&out.indent_off_text);
 			token_add_char(*inp_p++);
 			lsym = lsym_comment;
 			next_unary = ps.next_unary;

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.159 src/usr.bin/indent/pr_comment.c:1.160
--- src/usr.bin/indent/pr_comment.c:1.159	Sat Jun 10 06:38:21 2023
+++ src/usr.bin/indent/pr_comment.c	Sat Jun 10 12:59:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.159 2023/06/10 06:38:21 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.160 2023/06/10 12:59:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.159 2023/06/10 06:38:21 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.160 2023/06/10 12:59:31 rillig Exp $");
 
 #include <string.h>
 
@@ -248,7 +248,7 @@ copy_comment_wrap_finish(int line_length
 		if (com.len > 3)
 			output_line();
 		else
-			com.len = 0;
+			buf_clear(&com);
 		com_add_char(' ');
 	} else {
 		size_t len = com.len;
@@ -263,6 +263,7 @@ copy_comment_wrap_finish(int line_length
 	    && ch_isblank(com.s[com.len - 1])
 	    && ch_isblank(com.s[com.len - 2]))
 		com.len--;
+	buf_terminate(&com);
 
 	inp_p += 2;
 	if (com.len > 0 && ch_isblank(com.s[com.len - 1]))

Reply via email to