From a5ce01cc386a682a5461ad19bb5e2f3265532318 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 12 Apr 2022 19:41:26 +0200 Subject: [PATCH] Print a hint if the exported variables appear too large If we get an E2BIG while executing a process, we check how large the exported variables are. We already did this, but then immediately added it to the total. So now we keep the tally just for the variables around, and if it's over half (which is an atypical value if your system has an ARG_MAX of 2MB), we mention that in the error. Figuring out which variable is too big (in case it's just one) is probably too complicated, but we can at least complain if things seem suspect. Untested because I don't know *how* to do so portably --- src/postfork.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/postfork.cpp b/src/postfork.cpp index 9c15a1bd6..7392f2104 100644 --- a/src/postfork.cpp +++ b/src/postfork.cpp @@ -374,14 +374,16 @@ void safe_report_exec_error(int err, const char *actual_cmd, const char *const * long arg_max = -1; size_t sz = 0; + size_t szenv = 0; const char *const *p; for (p = argv; *p; p++) { sz += std::strlen(*p) + 1; } for (p = envv; *p; p++) { - sz += std::strlen(*p) + 1; + szenv += std::strlen(*p) + 1; } + sz += szenv; format_size_safe(sz1, sz); arg_max = sysconf(_SC_ARG_MAX); @@ -402,6 +404,12 @@ void safe_report_exec_error(int err, const char *actual_cmd, const char *const * "Failed to execute process '%s': An argument exceeds the OS " "argument length limit.", actual_cmd); } + + if (szenv >= static_cast(arg_max) / 2) { + FLOGF_SAFE(exec, + "Hint: Your exported variables take up over half the limit. Try erasing or unexporting variables." + ); + } } else { FLOGF_SAFE(exec, "Failed to execute process '%s': the total size of the argument list and "