Module Name: src
Committed By: rillig
Date: Sat Nov 27 20:33:39 UTC 2021
Modified Files:
src/tests/usr.bin/indent: fmt_decl.c
src/usr.bin/indent: lexi.c
Log Message:
indent: fix out of bounds memory access (since 2021-11-25)
To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/tests/usr.bin/indent/fmt_decl.c
cvs rdiff -u -r1.164 -r1.165 src/usr.bin/indent/lexi.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/tests/usr.bin/indent/fmt_decl.c
diff -u src/tests/usr.bin/indent/fmt_decl.c:1.31 src/tests/usr.bin/indent/fmt_decl.c:1.32
--- src/tests/usr.bin/indent/fmt_decl.c:1.31 Sat Nov 27 19:21:42 2021
+++ src/tests/usr.bin/indent/fmt_decl.c Sat Nov 27 20:33:39 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: fmt_decl.c,v 1.31 2021/11/27 19:21:42 rillig Exp $ */
+/* $NetBSD: fmt_decl.c,v 1.32 2021/11/27 20:33:39 rillig Exp $ */
/* $FreeBSD: head/usr.bin/indent/tests/declarations.0 334478 2018-06-01 09:41:15Z pstef $ */
/*
@@ -831,13 +831,13 @@ char str[sizeof(**ptr)];
/*
- * FIXME: Whether or not the function 'a' is a declaration or a definition
- * depends on the preceding struct, in particular the length of the 'pn'
- * line. This doesn't make sense at all and looks like an out-of-bounds memory
- * access.
+ * Since lexi.c 1.158 from 2021-11-25, whether the function 'a' was considered
+ * a declaration or a definition depended on the preceding struct, in
+ * particular the length of the 'pn' line. This didn't make sense at all and
+ * was due to an out-of-bounds memory access.
*
- * Since lexi.c 1.158 from 2021-11-25.
* Seen amongst others in args.c 1.72, function add_typedefs_from_file.
+ * Fixed in lexi.c 1.165 from 2021-11-27.
*/
#indent input
struct {
@@ -868,7 +868,8 @@ struct {
};
static void
- a(char *fe){
+a(char *fe)
+{
}
struct {
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.164 src/usr.bin/indent/lexi.c:1.165
--- src/usr.bin/indent/lexi.c:1.164 Thu Nov 25 18:48:37 2021
+++ src/usr.bin/indent/lexi.c Sat Nov 27 20:33:39 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.164 2021/11/25 18:48:37 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.165 2021/11/27 20:33:39 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 (
#include <sys/cdefs.h>
#if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.164 2021/11/25 18:48:37 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.165 2021/11/27 20:33:39 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
#endif
@@ -450,21 +450,27 @@ cmp_keyword_by_name(const void *key, con
return strcmp(key, ((const struct keyword *)elem)->name);
}
+/*
+ * Looking at a line starting with 'function_name(something)', guess whether
+ * this starts a function definition or a declaration.
+ */
static bool
probably_looking_at_definition(void)
{
int paren_level = 0;
for (const char *p = inp_p(), *e = inp_line_end(); p < e; p++) {
-proceed:
if (*p == '(')
paren_level++;
if (*p == ')' && --paren_level == 0) {
p++;
while (p < e && (ch_isspace(*p) || is_identifier_part(*p)))
p++;
- if (*p == '(')
- goto proceed;
- return !(*p == ';' || *p == ',');
+ if (p < e && (*p == ';' || *p == ','))
+ return false;
+ if (p < e && *p == '(')
+ paren_level++;
+ else
+ break;
}
}