From 089c625e66329ee282782d71b09fa629c291d80a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Thu, 23 May 2024 16:05:42 +0300 Subject: [PATCH] wrappers: Print an understandable error if the total command line ends up too long This can happen if the input command line is barely under the limit, but ends up exceeding it due to the extra arguments that the wrapper adds. One could hope that _tspawnvp would set errno to a helpful error code, to identify the issue when printing the error with _tperror(), but in practice, it doesn't. --- wrappers/native-wrapper.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/wrappers/native-wrapper.h b/wrappers/native-wrapper.h index 1ac87cd..950ef50 100644 --- a/wrappers/native-wrapper.h +++ b/wrappers/native-wrapper.h @@ -94,10 +94,19 @@ static inline int _tspawnvp_escape(int mode, const TCHAR *filename, const TCHAR while (argv[num_args]) num_args++; const TCHAR **escaped_argv = malloc((num_args + 1) * sizeof(*escaped_argv)); - for (int i = 0; argv[i]; i++) + int total = 0; + for (int i = 0; argv[i]; i++) { escaped_argv[i] = escape(argv[i]); + total += 1 + _tcslen(escaped_argv[i]); + } escaped_argv[num_args] = NULL; - return _tspawnvp(mode, filename, escaped_argv); + int ret = _tspawnvp(mode, filename, escaped_argv); + if (ret == -1 && total >= 32767) { + int err = errno; + fprintf(stderr, "command line too long; %d characters\n", total); + errno = err; + } + return ret; } #else static inline int _tcsicmp(const TCHAR *a, const TCHAR *b) {