use dynamic buffer for argv

This commit is contained in:
Wirtos_new 2021-06-19 17:55:56 +03:00
parent e9468b25e1
commit ffd9f0868b
2 changed files with 33 additions and 13 deletions

View file

@ -81,14 +81,18 @@ show_adb_installation_msg() {
static void
show_adb_err_msg(enum process_result err, const char *const argv[]) {
char buf[512];
size_t n_args, len;
char *buf = NULL;
len = xargvlen(argv, &n_args) + (n_args * 3 - 1) + 1;
/* buffer large enough to store argv_to_string repr + nul terminator */
buf = malloc(len);
switch (err) {
case PROCESS_ERROR_GENERIC:
argv_to_string(argv, buf, sizeof(buf));
argv_to_string(argv, buf, len);
LOGE("Failed to execute: %s", buf);
break;
case PROCESS_ERROR_MISSING_BINARY:
argv_to_string(argv, buf, sizeof(buf));
argv_to_string(argv, buf, len);
LOGE("Command not found: %s", buf);
LOGE("(make 'adb' accessible from your PATH or define its full"
"path in the ADB environment variable)");
@ -98,6 +102,7 @@ show_adb_err_msg(enum process_result err, const char *const argv[]) {
// do nothing
break;
}
free(buf);
}
process_t

View file

@ -6,6 +6,11 @@
#include "util/log.h"
#include "util/str_util.h"
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
static int
build_cmd(char *cmd, size_t len, const char *const argv[]) {
// Windows command-line parsing is WTF:
@ -24,34 +29,44 @@ enum process_result
process_execute(const char *const argv[], HANDLE *handle) {
STARTUPINFOW si;
PROCESS_INFORMATION pi;
wchar_t *wide = NULL;
char *cmd = NULL;
enum process_result res = PROCESS_SUCCESS;
size_t len = xargvlen(argv, NULL) + 1;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
char cmd[256];
if (build_cmd(cmd, sizeof(cmd), argv)) {
cmd = malloc(len);
if (cmd == NULL || build_cmd(cmd, len, argv) != 0) {
*handle = NULL;
return PROCESS_ERROR_GENERIC;
res = PROCESS_ERROR_GENERIC;
goto end;
}
wchar_t *wide = utf8_to_wide_char(cmd);
wide = utf8_to_wide_char(cmd);
if (!wide) {
LOGC("Could not allocate wide char string");
return PROCESS_ERROR_GENERIC;
res = PROCESS_ERROR_GENERIC;
goto end;
}
if (!CreateProcessW(NULL, wide, NULL, NULL, FALSE, 0, NULL, NULL, &si,
&pi)) {
free(wide);
*handle = NULL;
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
return PROCESS_ERROR_MISSING_BINARY;
res = PROCESS_ERROR_MISSING_BINARY;
} else {
res = PROCESS_ERROR_GENERIC;
}
return PROCESS_ERROR_GENERIC;
goto end;
}
free(wide);
*handle = pi.hProcess;
return PROCESS_SUCCESS;
end:
free(wide);
free(cmd);
return res;
}
bool