make: fix memory leak for command strings

This commit is contained in:
rillig 2024-05-25 21:07:48 +00:00
parent cd90f040db
commit 734fc80208
5 changed files with 35 additions and 20 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: compat.c,v 1.256 2024/05/25 15:37:17 rillig Exp $ */
/* $NetBSD: compat.c,v 1.257 2024/05/25 21:07:48 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -91,7 +91,7 @@
#include "pathnames.h"
/* "@(#)compat.c 8.2 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: compat.c,v 1.256 2024/05/25 15:37:17 rillig Exp $");
MAKE_RCSID("$NetBSD: compat.c,v 1.257 2024/05/25 21:07:48 rillig Exp $");
static GNode *curTarg = NULL;
static pid_t compatChild;
@ -278,11 +278,13 @@ Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
* usual '$$'.
*/
Lst_Append(&endNode->commands, cmdStart);
return true;
goto register_command;
}
}
if (strcmp(cmdStart, "...") == 0) {
gn->type |= OP_SAVE_CMDS;
register_command:
Parse_RegisterCommand(cmdStart);
return true;
}
@ -302,7 +304,7 @@ Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
while (ch_isspace(*cmd))
cmd++;
if (cmd[0] == '\0')
return true;
goto register_command;
useShell = UseShell(cmd);
@ -312,7 +314,7 @@ Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
}
if (!doIt && !GNode_ShouldExecute(gn))
return true;
goto register_command;
DEBUG1(JOB, "Execute: '%s'\n", cmd);

View File

@ -1,4 +1,4 @@
/* $NetBSD: job.c,v 1.472 2024/05/25 08:03:19 rillig Exp $ */
/* $NetBSD: job.c,v 1.473 2024/05/25 21:07:48 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -141,7 +141,7 @@
#include "trace.h"
/* "@(#)job.c 8.2 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: job.c,v 1.472 2024/05/25 08:03:19 rillig Exp $");
MAKE_RCSID("$NetBSD: job.c,v 1.473 2024/05/25 21:07:48 rillig Exp $");
/*
* A shell defines how the commands are run. All commands for a target are
@ -1035,6 +1035,7 @@ JobSaveCommands(Job *job)
EvalStack_Pop();
/* TODO: handle errors */
Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd);
Parse_RegisterCommand(expanded_cmd);
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: make.h,v 1.334 2024/05/25 00:00:25 rillig Exp $ */
/* $NetBSD: make.h,v 1.335 2024/05/25 21:07:48 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -915,6 +915,15 @@ void Targ_PrintType(GNodeType);
void Targ_PrintGraph(int);
void Targ_Propagate(void);
const char *GNodeMade_Name(GNodeMade) MAKE_ATTR_USE;
#ifdef CLEANUP
void Parse_RegisterCommand(char *);
#else
/* ARGSUSED */
MAKE_INLINE
void Parse_RegisterCommand(char *cmd MAKE_ATTR_UNUSED)
{
}
#endif
/* var.c */
void Var_Init(void);

View File

@ -1,4 +1,4 @@
/* $NetBSD: parse.c,v 1.725 2024/05/25 08:03:19 rillig Exp $ */
/* $NetBSD: parse.c,v 1.726 2024/05/25 21:07:48 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -105,7 +105,7 @@
#include "pathnames.h"
/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: parse.c,v 1.725 2024/05/25 08:03:19 rillig Exp $");
MAKE_RCSID("$NetBSD: parse.c,v 1.726 2024/05/25 21:07:48 rillig Exp $");
/* Detects a multiple-inclusion guard in a makefile. */
typedef enum {
@ -218,9 +218,9 @@ static GNodeList *targets;
#ifdef CLEANUP
/*
* All shell commands for all targets, in no particular order and possibly
* with duplicates. Kept in a separate list since the commands from .USE or
* .USEBEFORE nodes are shared with other GNodes, thereby giving up the
* easily understandable ownership over the allocated strings.
* with duplicate values. Kept in a separate list since the commands from
* .USE or .USEBEFORE nodes are shared with other GNodes, thereby giving up
* the easily understandable ownership over the allocated strings.
*/
static StringList targCmds = LST_INIT;
#endif
@ -2674,6 +2674,13 @@ FinishDependencyGroup(void)
targets = NULL;
}
#ifdef CLEANUP
void Parse_RegisterCommand(char *cmd)
{
Lst_Append(&targCmds, cmd);
}
#endif
/* Add the command to each target from the current dependency spec. */
static void
ParseLine_ShellCommand(const char *p)
@ -2696,9 +2703,7 @@ ParseLine_ShellCommand(const char *p)
GNode *gn = ln->datum;
GNode_AddCommand(gn, cmd);
}
#ifdef CLEANUP
Lst_Append(&targCmds, cmd);
#endif
Parse_RegisterCommand(cmd);
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: targ.c,v 1.182 2024/05/25 00:00:25 rillig Exp $ */
/* $NetBSD: targ.c,v 1.183 2024/05/25 21:07:48 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -107,7 +107,7 @@
#include "dir.h"
/* "@(#)targ.c 8.2 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: targ.c,v 1.182 2024/05/25 00:00:25 rillig Exp $");
MAKE_RCSID("$NetBSD: targ.c,v 1.183 2024/05/25 21:07:48 rillig Exp $");
/*
* All target nodes that appeared on the left-hand side of one of the
@ -219,9 +219,7 @@ GNode_New(const char *name)
static void
GNode_Free(GNode *gn)
{
#ifdef CLEANUP
Var_DeleteAll(gn);
#endif
free(gn->name);
free(gn->uname);