make(1): properly initialize For structure in For_Eval

Initializing a Buffer or a strlist_t with zero-valued bytes only works
by conincidence, but because it would be the correct way.  In the code
path "missing `in' in for", that zero-filled Buffer is freed using
Buf_Destroy, which could have invoked undefined behavior.
This commit is contained in:
rillig 2020-09-06 19:24:12 +00:00
parent 0244d862b4
commit 44d49322ad

View File

@ -1,4 +1,4 @@
/* $NetBSD: for.c,v 1.70 2020/09/06 19:19:49 rillig Exp $ */
/* $NetBSD: for.c,v 1.71 2020/09/06 19:24:12 rillig Exp $ */
/*
* Copyright (c) 1992, The Regents of the University of California.
@ -30,14 +30,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: for.c,v 1.70 2020/09/06 19:19:49 rillig Exp $";
static char rcsid[] = "$NetBSD: for.c,v 1.71 2020/09/06 19:24:12 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)for.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: for.c,v 1.70 2020/09/06 19:19:49 rillig Exp $");
__RCSID("$NetBSD: for.c,v 1.71 2020/09/06 19:24:12 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -152,7 +152,12 @@ For_Eval(char *line)
*/
new_for = bmake_malloc(sizeof *new_for);
memset(new_for, 0, sizeof *new_for);
Buf_Init(&new_for->buf, 0);
strlist_init(&new_for->vars);
strlist_init(&new_for->items);
new_for->parse_buf = NULL;
new_for->short_var = FALSE;
new_for->sub_next = 0;
/* Grab the variables. Terminate on "in". */
for (;; ptr += len) {
@ -248,7 +253,6 @@ For_Eval(char *line)
}
}
Buf_Init(&new_for->buf, 0);
accumFor = new_for;
forLevel = 1;
return 1;