Module Name:    src
Committed By:   rillig
Date:           Sat May 25 21:07:48 UTC 2024

Modified Files:
        src/usr.bin/make: compat.c job.c make.h parse.c targ.c

Log Message:
make: fix memory leak for command strings


To generate a diff of this commit:
cvs rdiff -u -r1.256 -r1.257 src/usr.bin/make/compat.c
cvs rdiff -u -r1.472 -r1.473 src/usr.bin/make/job.c
cvs rdiff -u -r1.334 -r1.335 src/usr.bin/make/make.h
cvs rdiff -u -r1.725 -r1.726 src/usr.bin/make/parse.c
cvs rdiff -u -r1.182 -r1.183 src/usr.bin/make/targ.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/compat.c
diff -u src/usr.bin/make/compat.c:1.256 src/usr.bin/make/compat.c:1.257
--- src/usr.bin/make/compat.c:1.256	Sat May 25 15:37:17 2024
+++ src/usr.bin/make/compat.c	Sat May 25 21:07:48 2024
@@ -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, GNod
 			 * 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, GNod
 	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, GNod
 	}
 
 	if (!doIt && !GNode_ShouldExecute(gn))
-		return true;
+		goto register_command;
 
 	DEBUG1(JOB, "Execute: '%s'\n", cmd);
 

Index: src/usr.bin/make/job.c
diff -u src/usr.bin/make/job.c:1.472 src/usr.bin/make/job.c:1.473
--- src/usr.bin/make/job.c:1.472	Sat May 25 08:03:19 2024
+++ src/usr.bin/make/job.c	Sat May 25 21:07:48 2024
@@ -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);
 	}
 }
 

Index: src/usr.bin/make/make.h
diff -u src/usr.bin/make/make.h:1.334 src/usr.bin/make/make.h:1.335
--- src/usr.bin/make/make.h:1.334	Sat May 25 00:00:25 2024
+++ src/usr.bin/make/make.h	Sat May 25 21:07:48 2024
@@ -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);

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.725 src/usr.bin/make/parse.c:1.726
--- src/usr.bin/make/parse.c:1.725	Sat May 25 08:03:19 2024
+++ src/usr.bin/make/parse.c	Sat May 25 21:07:48 2024
@@ -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);
 	}
 }
 

Index: src/usr.bin/make/targ.c
diff -u src/usr.bin/make/targ.c:1.182 src/usr.bin/make/targ.c:1.183
--- src/usr.bin/make/targ.c:1.182	Sat May 25 00:00:25 2024
+++ src/usr.bin/make/targ.c	Sat May 25 21:07:48 2024
@@ -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);

Reply via email to