Skip to contentSkip to conversation

toolchain/toolchain-wrapper.c: slightly simplify cmdline copying

PATCH Applied Peter Korsgaard 2 participants 4 messages May 18, 2025 – Aug 7, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 toolchain/toolchain-wrapper.c: slightly simplify cmdline copying

C99 section 5.1.2.2.1p2 mandates that:

- argv[argc] shall be a null pointer.

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

So we might as well copy the null pointer along in the memcpy() rather than copy everything up to the null pointer and then add one afterwards for simplicity.

---
 toolchain/toolchain-wrapper.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
toolchain/toolchain-wrapper.c +2 -6
index e644c1e7bb..d6ce74fc94 100644
@@ -497,12 +497,8 @@ int main(int argc, char **argv)
 #endif
 	}
 
-	/* append forward args */
-	memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
-	cur += argc - 1;
-
-	/* finish with NULL termination */
-	*cur = NULL;
+	/* append forward args and terminating NULL */
+	memcpy(cur, &argv[1], sizeof(char *) * argc);
 
 	exec_args = args;
 #ifdef BR_CCACHE
Signature · 6 lines
2.39.5

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw

PATCH 2/2 toolchain/toolchain-wrapper.c: get rid of EXCLUSIVE_ARGS

Rather than having a hard coded amount of exclusive args (with the risk of overflow when new logic is added), simplify the argument buffer allocation logic to always allocate room for DEFAULT_MAX_ARGS (1024) arguments and just realloc to grow for the rare situation where that is not enough.

---
 toolchain/toolchain-wrapper.c | 37 ++++++++++++++---------------------
 1 file changed, 15 insertions(+), 22 deletions(-)
toolchain/toolchain-wrapper.c +15 -22
index d6ce74fc94..fdfedf20ef 100644
@@ -36,25 +36,9 @@ static char sysroot[PATH_MAX];
 static char _time_[sizeof("-D__TIME__=\"HH:MM:SS\"")];
 static char _date_[sizeof("-D__DATE__=\"MMM DD YYYY\"")];
 
-/**
- * GCC errors out with certain combinations of arguments (examples are
- * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure
- * that we only pass the predefined one to the real compiler if the inverse
- * option isn't in the argument list.
- * This specifies the worst case number of extra arguments we might pass
- * Currently, we may have:
- * 	-mfloat-abi=
- * 	-march=
- * 	-mcpu=
- * 	-D__TIME__=
- * 	-D__DATE__=
- * 	-Wno-builtin-macro-redefined
- * 	-Wl,-z,now
- * 	-Wl,-z,relro
- * 	-fPIE
- * 	-pie
- */
-#define EXCLUSIVE_ARGS	10
+/* Maximum amount of arguments to reserve space for by default.
+   Must be > predef_args */
+#define DEFAULT_MAX_ARGS	1024
 
 static char *predef_args[] = {
 #ifdef BR_CCACHE
@@ -246,6 +230,7 @@ int main(int argc, char **argv)
 	char *basename;
 	char *env_debug;
 	int ret, i, count = 0, debug = 0, found_shared = 0, found_nonoption = 0;
+	size_t n_args;
 
 	/* Debug the wrapper to see arguments it was called with.
 	 * If environment variable BR2_DEBUG_WRAPPER is:
@@ -354,8 +339,7 @@ int main(int argc, char **argv)
 		return 3;
 	}
 
-	cur = args = malloc(sizeof(predef_args) +
-			    (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
+	cur = args = malloc(DEFAULT_MAX_ARGS * sizeof(char *));
 	if (args == NULL) {
 		perror(__FILE__ ": malloc");
 		return 2;
@@ -497,8 +481,17 @@ int main(int argc, char **argv)
 #endif
 	}
 
+        n_args = (cur - args);
+        if ((n_args + argc) > DEFAULT_MAX_ARGS) {
+		args = realloc(args, (n_args + argc) * sizeof(char *));
+		if (args == NULL) {
+			perror(__FILE__ ": realloc");
+			return 2;
+		}
+        }
+
 	/* append forward args and terminating NULL */
-	memcpy(cur, &argv[1], sizeof(char *) * argc);
+	memcpy(&args[n_args], &argv[1], sizeof(char *) * argc);
 
 	exec_args = args;
 #ifdef BR_CCACHE
Signature · 6 lines
2.39.5

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#2 lore raw
In reply to Peter Korsgaard
Quoted 11 lines · Peter Korsgaard

C99 section 5.1.2.2.1p2 mandates that:

- argv[argc] shall be a null pointer.

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

So we might as well copy the null pointer along in the memcpy() rather than copy everything up to the null pointer and then add one afterwards for simplicity.

Series applied to master, thanks.
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#3 lore raw
In reply to Peter Korsgaard

In reply of:

Quoted 11 lines

C99 section 5.1.2.2.1p2 mandates that:

- argv[argc] shall be a null pointer.

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

So we might as well copy the null pointer along in the memcpy() rather than copy everything up to the null pointer and then add one afterwards for simplicity.

Applied to 2025.02.x & 2025.05.x. Thanks

Quoted 30 lines
---
 toolchain/toolchain-wrapper.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
toolchain/toolchain-wrapper.c +2 -6
index e644c1e7bb..d6ce74fc94 100644
@@ -497,12 +497,8 @@ int main(int argc, char **argv)
 #endif
 	}
 
-	/* append forward args */
-	memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
-	cur += argc - 1;
-
-	/* finish with NULL termination */
-	*cur = NULL;
+	/* append forward args and terminating NULL */
+	memcpy(cur, &argv[1], sizeof(char *) * argc);
 
 	exec_args = args;
 #ifdef BR_CCACHE
Signature · 6 lines
2.39.5

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot