Skip to content

Commit 089c625

Browse files
committed
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.
1 parent 3847488 commit 089c625

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

wrappers/native-wrapper.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,19 @@ static inline int _tspawnvp_escape(int mode, const TCHAR *filename, const TCHAR
9494
while (argv[num_args])
9595
num_args++;
9696
const TCHAR **escaped_argv = malloc((num_args + 1) * sizeof(*escaped_argv));
97-
for (int i = 0; argv[i]; i++)
97+
int total = 0;
98+
for (int i = 0; argv[i]; i++) {
9899
escaped_argv[i] = escape(argv[i]);
100+
total += 1 + _tcslen(escaped_argv[i]);
101+
}
99102
escaped_argv[num_args] = NULL;
100-
return _tspawnvp(mode, filename, escaped_argv);
103+
int ret = _tspawnvp(mode, filename, escaped_argv);
104+
if (ret == -1 && total >= 32767) {
105+
int err = errno;
106+
fprintf(stderr, "command line too long; %d characters\n", total);
107+
errno = err;
108+
}
109+
return ret;
101110
}
102111
#else
103112
static inline int _tcsicmp(const TCHAR *a, const TCHAR *b) {

0 commit comments

Comments
 (0)