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: fix lost function name (since 2019-04...



details:   https://anonhg.NetBSD.org/src/rev/073375f17386
branches:  trunk
changeset: 1026337:073375f17386
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Nov 19 18:52:32 2021 +0000

description:
indent: fix lost function name (since 2019-04-04)

When indent searched for an identifier followed by a '(', to see whether
the identifier is a function name, it didn't care that the input buffer
could be resized due to a long line, which had made the pointer 'tp'
invalid.  Fix this by stopping the search at the end of the line.  A
better approach would be to have an unlimited lookahead buffer for
situations like these.  The code that deals with character input has
already been extracted to io.c, so it's possible to implement that now.

While here, fix another access to undefined memory, after the loop.

There is still the issue of overwriting procname[0] with a blank, which
results in inconsistent formatting depending on the function name,
probably another case of accessing undefined memory, although the
results have been reproducible, but that may have been pure luck.

The formatted code looks clearly broken, but that's still better than
losing a token and destroying the whole file.

diffstat:

 tests/usr.bin/indent/fmt_decl.c |   6 +++---
 usr.bin/indent/lexi.c           |  23 +++++++++--------------
 2 files changed, 12 insertions(+), 17 deletions(-)

diffs (65 lines):

diff -r aae54788a31e -r 073375f17386 tests/usr.bin/indent/fmt_decl.c
--- a/tests/usr.bin/indent/fmt_decl.c   Fri Nov 19 18:25:50 2021 +0000
+++ b/tests/usr.bin/indent/fmt_decl.c   Fri Nov 19 18:52:32 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fmt_decl.c,v 1.15 2021/11/18 23:06:51 rillig Exp $     */
+/*     $NetBSD: fmt_decl.c,v 1.16 2021/11/19 18:52:33 rillig Exp $     */
 /* $FreeBSD: head/usr.bin/indent/tests/declarations.0 334478 2018-06-01 09:41:15Z pstef $ */
 
 /* See FreeBSD r303570 */
@@ -512,8 +512,8 @@
 {}
 #indent end
 
-/* FIXME: The function name is missing. */
 #indent run
-int           *(void){
+int           *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+               (void){
 }
 #indent end
diff -r aae54788a31e -r 073375f17386 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Fri Nov 19 18:25:50 2021 +0000
+++ b/usr.bin/indent/lexi.c     Fri Nov 19 18:52:32 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.143 2021/11/19 17:30:10 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.144 2021/11/19 18:52:32 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.143 2021/11/19 17:30:10 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.144 2021/11/19 18:52:32 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -682,19 +682,14 @@
        }
 
        if (ps.in_decl) {
-           const char *tp = inp_p();
+           const char *tp = inp_p(), *e = inp_line_end();
 
-           while (isalpha((unsigned char)*tp) ||
-                   isspace((unsigned char)*tp)) {
-               if (++tp >= inp_line_end()) {
-                   const char *p_before = inp_p();
-                   inp_read_line();
-                   if (inp_p() != p_before)
-                       abort();
-               }
-           }
-           if (*tp == '(')
-               ps.procname[0] = ' ';
+           while (tp < e && (isalpha((unsigned char)*tp) ||
+                   isspace((unsigned char)*tp)))
+               tp++;
+
+           if (tp < e && *tp == '(')
+               ps.procname[0] = ' ';   /* XXX: why not '\0'? */
        }
 
        lsym = lsym_unary_op;



Home | Main Index | Thread Index | Old Index