dynamically allocate CMD_MAX for subprocess and adb error message

This commit is contained in:
Wirtos_new 2021-06-19 18:47:57 +03:00
commit 57219da7c6
3 changed files with 43 additions and 23 deletions

View file

@ -81,14 +81,15 @@ show_adb_installation_msg() {
static void static void
show_adb_err_msg(enum process_result err, const char *const argv[]) { show_adb_err_msg(enum process_result err, const char *const argv[]) {
char buf[512]; char *buf = malloc(CMD_MAX);
if (!buf) return;
switch (err) { switch (err) {
case PROCESS_ERROR_GENERIC: case PROCESS_ERROR_GENERIC:
argv_to_string(argv, buf, sizeof(buf)); argv_to_string(argv, buf, CMD_MAX);
LOGE("Failed to execute: %s", buf); LOGE("Failed to execute: %s", buf);
break; break;
case PROCESS_ERROR_MISSING_BINARY: case PROCESS_ERROR_MISSING_BINARY:
argv_to_string(argv, buf, sizeof(buf)); argv_to_string(argv, buf, CMD_MAX);
LOGE("Command not found: %s", buf); LOGE("Command not found: %s", buf);
LOGE("(make 'adb' accessible from your PATH or define its full" LOGE("(make 'adb' accessible from your PATH or define its full"
"path in the ADB environment variable)"); "path in the ADB environment variable)");
@ -98,29 +99,33 @@ show_adb_err_msg(enum process_result err, const char *const argv[]) {
// do nothing // do nothing
break; break;
} }
free(buf);
} }
process_t process_t
adb_execute(const char *serial, const char *const adb_cmd[], size_t len) { adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
const char *cmd[len + 4]; const char **argv;
int i; int i;
process_t process; process_t process;
cmd[0] = get_adb_command(); argv = malloc(sizeof(char *) * (len + 3 + 1));
if (!argv) return PROCESS_NONE;
argv[0] = get_adb_command();
if (serial) { if (serial) {
cmd[1] = "-s"; argv[1] = "-s";
cmd[2] = serial; argv[2] = serial;
i = 3; i = 3;
} else { } else {
i = 1; i = 1;
} }
memcpy(&cmd[i], adb_cmd, len * sizeof(const char *)); memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
cmd[len + i] = NULL; argv[len + i] = NULL;
enum process_result r = process_execute(cmd, &process); enum process_result r = process_execute(argv, &process);
if (r != PROCESS_SUCCESS) { if (r != PROCESS_SUCCESS) {
show_adb_err_msg(r, cmd); show_adb_err_msg(r, argv);
return PROCESS_NONE; process = PROCESS_NONE;
} }
free(argv);
return process; return process;
} }

View file

@ -6,6 +6,11 @@
#include "util/log.h" #include "util/log.h"
#include "util/str_util.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 static int
build_cmd(char *cmd, size_t len, const char *const argv[]) { build_cmd(char *cmd, size_t len, const char *const argv[]) {
// Windows command-line parsing is WTF: // Windows command-line parsing is WTF:
@ -24,34 +29,43 @@ enum process_result
process_execute(const char *const argv[], HANDLE *handle) { process_execute(const char *const argv[], HANDLE *handle) {
STARTUPINFOW si; STARTUPINFOW si;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
enum process_result res = PROCESS_SUCCESS;
wchar_t *wide = NULL;
char *cmd = NULL;
memset(&si, 0, sizeof(si)); memset(&si, 0, sizeof(si));
si.cb = sizeof(si); si.cb = sizeof(si);
char cmd[256]; cmd = malloc(CMD_MAX);
if (build_cmd(cmd, sizeof(cmd), argv)) { if (cmd == NULL || build_cmd(cmd, CMD_MAX, argv) != 0) {
*handle = NULL; *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) { if (!wide) {
LOGC("Could not allocate wide char string"); 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, if (!CreateProcessW(NULL, wide, NULL, NULL, FALSE, 0, NULL, NULL, &si,
&pi)) { &pi)) {
free(wide);
*handle = NULL; *handle = NULL;
if (GetLastError() == ERROR_FILE_NOT_FOUND) { 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; *handle = pi.hProcess;
return PROCESS_SUCCESS;
end:
free(wide);
free(cmd);
return res;
} }
bool bool

View file

@ -18,7 +18,6 @@
# define NO_EXIT_CODE -1u // max value as unsigned # define NO_EXIT_CODE -1u // max value as unsigned
typedef HANDLE process_t; typedef HANDLE process_t;
typedef DWORD exit_code_t; typedef DWORD exit_code_t;
#else #else
# include <sys/types.h> # include <sys/types.h>
@ -32,6 +31,8 @@
#endif #endif
#define CMD_MAX 8192
enum process_result { enum process_result {
PROCESS_SUCCESS, PROCESS_SUCCESS,
PROCESS_ERROR_GENERIC, PROCESS_ERROR_GENERIC,