Skip to content

Commit

Permalink
Replace VLA by dynamic allocation
Browse files Browse the repository at this point in the history
And increase the command buffer size.

Refs #1358 <#1358 (comment)>
PR #2405 <#2405>

Signed-off-by: Romain Vimont <[email protected]>
  • Loading branch information
Wirtos authored and rom1v committed Jun 20, 2021
1 parent fda3292 commit a9d9cbf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
25 changes: 20 additions & 5 deletions app/src/adb.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,20 @@ show_adb_installation_msg() {

static void
show_adb_err_msg(enum process_result err, const char *const argv[]) {
char buf[512];
#define MAX_COMMAND_STRING_LEN 1024
char *buf = malloc(MAX_COMMAND_STRING_LEN);
if (!buf) {
LOGE("Failed to execute (could not allocate error message)");
return;
}

switch (err) {
case PROCESS_ERROR_GENERIC:
argv_to_string(argv, buf, sizeof(buf));
argv_to_string(argv, buf, MAX_COMMAND_STRING_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, MAX_COMMAND_STRING_LEN);
LOGE("Command not found: %s", buf);
LOGE("(make 'adb' accessible from your PATH or define its full"
"path in the ADB environment variable)");
Expand All @@ -98,13 +104,20 @@ show_adb_err_msg(enum process_result err, const char *const argv[]) {
// do nothing
break;
}

free(buf);
}

process_t
adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
const char *argv[len + 4];
int i;
process_t process;

const char **argv = malloc((len + 4) * sizeof(*argv));
if (!argv) {
return PROCESS_NONE;
}

argv[0] = get_adb_command();
if (serial) {
argv[1] = "-s";
Expand All @@ -119,8 +132,10 @@ adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
enum process_result r = process_execute(argv, &process);
if (r != PROCESS_SUCCESS) {
show_adb_err_msg(r, argv);
return PROCESS_NONE;
process = PROCESS_NONE;
}

free(argv);
return process;
}

Expand Down
7 changes: 5 additions & 2 deletions app/src/sys/win/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "util/log.h"
#include "util/str_util.h"

#define CMD_MAX_LEN 8192

static bool
build_cmd(char *cmd, size_t len, const char *const argv[]) {
// Windows command-line parsing is WTF:
Expand All @@ -27,13 +29,14 @@ process_execute(const char *const argv[], HANDLE *handle) {
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);

char cmd[256];
if (!build_cmd(cmd, sizeof(cmd), argv)) {
char *cmd = malloc(CMD_MAX_LEN);
if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) {
*handle = NULL;
return PROCESS_ERROR_GENERIC;
}

wchar_t *wide = utf8_to_wide_char(cmd);
free(cmd);
if (!wide) {
LOGC("Could not allocate wide char string");
return PROCESS_ERROR_GENERIC;
Expand Down

0 comments on commit a9d9cbf

Please sign in to comment.