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: in debug mode, null-terminate buffers



details:   https://anonhg.NetBSD.org/src/rev/0b19018a0225
branches:  trunk
changeset: 376319:0b19018a0225
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Jun 10 12:59:31 2023 +0000

description:
indent: in debug mode, null-terminate buffers

diffstat:

 usr.bin/indent/indent.c     |  21 +++++++++++++++++----
 usr.bin/indent/indent.h     |  17 +++++++++++++++--
 usr.bin/indent/io.c         |  18 ++++++++++--------
 usr.bin/indent/lexi.c       |   8 ++++----
 usr.bin/indent/pr_comment.c |   7 ++++---
 5 files changed, 50 insertions(+), 21 deletions(-)

diffs (251 lines):

diff -r 7606aa2a93c6 -r 0b19018a0225 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Jun 10 12:46:32 2023 +0000
+++ b/usr.bin/indent/indent.c   Sat Jun 10 12:59:31 2023 +0000
@@ -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;
diff -r 7606aa2a93c6 -r 0b19018a0225 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat Jun 10 12:46:32 2023 +0000
+++ b/usr.bin/indent/indent.h   Sat Jun 10 12:59:31 2023 +0000
@@ -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);
+}
diff -r 7606aa2a93c6 -r 0b19018a0225 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat Jun 10 12:46:32 2023 +0000
+++ b/usr.bin/indent/io.c       Sat Jun 10 12:59:31 2023 +0000
@@ -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;
diff -r 7606aa2a93c6 -r 0b19018a0225 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sat Jun 10 12:46:32 2023 +0000
+++ b/usr.bin/indent/lexi.c     Sat Jun 10 12:59:31 2023 +0000
@@ -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;
diff -r 7606aa2a93c6 -r 0b19018a0225 usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sat Jun 10 12:46:32 2023 +0000
+++ b/usr.bin/indent/pr_comment.c       Sat Jun 10 12:59:31 2023 +0000
@@ -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]))



Home | Main Index | Thread Index | Old Index