Module Name:    src
Committed By:   sjg
Date:           Fri Jan  5 22:20:07 UTC 2024

Modified Files:
        src/usr.bin/make: main.c

Log Message:
Cmd_Exec use tempfile if cmd is too big

To avoid blowing commandline/env limits, if "cmd"
is more than 1000 bytes, write it to a file and pass that
to shell.

Reviewed by: rillig


To generate a diff of this commit:
cvs rdiff -u -r1.606 -r1.607 src/usr.bin/make/main.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/main.c
diff -u src/usr.bin/make/main.c:1.606 src/usr.bin/make/main.c:1.607
--- src/usr.bin/make/main.c:1.606	Wed Dec 27 00:45:37 2023
+++ src/usr.bin/make/main.c	Fri Jan  5 22:20:07 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.606 2023/12/27 00:45:37 sjg Exp $	*/
+/*	$NetBSD: main.c,v 1.607 2024/01/05 22:20:07 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.606 2023/12/27 00:45:37 sjg Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.607 2024/01/05 22:20:07 sjg Exp $");
 #if defined(MAKE_NATIVE)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -1705,14 +1705,38 @@ Cmd_Exec(const char *cmd, char **error)
 	char *output;
 	char *p;
 	int saved_errno;
+	char cmd_file[MAXPATHLEN];
+	size_t cmd_len;
+	int cmd_fd = -1;
 
 	if (shellPath == NULL)
 		Shell_Init();
 
+	cmd_len = strlen(cmd);
+	if (cmd_len > 1000) {
+		cmd_fd = mkTempFile(NULL, cmd_file, sizeof(cmd_file));
+		if (cmd_fd >= 0) {
+			ssize_t n;
+
+			n = write(cmd_fd, cmd, cmd_len);
+			close(cmd_fd);
+			if (n < (ssize_t)cmd_len) {
+				unlink(cmd_file);
+				cmd_fd = -1;
+			}
+		}
+	}
+
 	args[0] = shellName;
-	args[1] = "-c";
-	args[2] = cmd;
-	args[3] = NULL;
+	if (cmd_fd >= 0) {
+		args[1] = cmd_file;
+		args[2] = NULL;
+	} else {
+		cmd_file[0] = '\0';
+		args[1] = "-c";
+		args[2] = cmd;
+		args[3] = NULL;
+	}
 	DEBUG1(VAR, "Capturing the output of command \"%s\"\n", cmd);
 
 	if (pipe(pipefds) == -1) {
@@ -1775,6 +1799,8 @@ Cmd_Exec(const char *cmd, char **error)
 		    "Couldn't read shell's output for \"", cmd, "\"");
 	else
 		*error = NULL;
+	if (cmd_file[0] != '\0')
+		unlink(cmd_file);
 	return output;
 }
 
@@ -2129,7 +2155,7 @@ getTmpdir(void)
 
 /*
  * Create and open a temp file using "pattern".
- * If out_fname is provided, set it to a copy of the filename created.
+ * If tfile is provided, set it to a copy of the filename created.
  * Otherwise unlink the file once open.
  */
 int

Reply via email to