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: replace unreachable code with assertions



details:   https://anonhg.NetBSD.org/src/rev/7a408e60242a
branches:  trunk
changeset: 988671:7a408e60242a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Oct 08 22:37:33 2021 +0000

description:
indent: replace unreachable code with assertions

The input buffer is always supposed to be terminated with a newline. The
function inbuf_read_line silently skips null characters. Since the input
buffer is redirected to a temporary buffer in some cases, do not simply
remove this supposed dead code, but replace it with assertions.

In any case, if the code for calling inbuf_read_line had been reachable
and actually allocated a different buffer, continuing to use the pointer
p would have invoked undefined behavior anyway.

diffstat:

 usr.bin/indent/pr_comment.c |  11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)

diffs (36 lines):

diff -r f8f644af8234 -r 7a408e60242a usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Fri Oct 08 22:27:52 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Fri Oct 08 22:37:33 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.67 2021/10/08 22:27:52 rillig Exp $   */
+/*     $NetBSD: pr_comment.c,v 1.68 2021/10/08 22:37:33 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,11 +43,12 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.67 2021/10/08 22:27:52 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.68 2021/10/08 22:37:33 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
 
+#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -169,9 +170,9 @@
 
     /* Don't put a break delimiter if this is a one-liner that won't wrap. */
     if (break_delim) {
-       for (const char *p = inp.s; *p != '\0' && *p != '\n'; p++) {
-           if (p >= inp.e)
-               inbuf_read_line();
+       for (const char *p = inp.s; *p != '\n'; p++) {
+           assert(*p != '\0');
+           assert(p < inp.e);
            if (p[0] == '*' && p[1] == '/') {
                /*
                 * XXX: This computation ignores the leading " * ", as well as



Home | Main Index | Thread Index | Old Index