Skip to content

Commit

Permalink
Use GetEnvironmentVariableA on Windows
Browse files Browse the repository at this point in the history
getenv does not see env variables set with SetEnvironmentVariable.
  • Loading branch information
fmeum committed Mar 14, 2022
1 parent 6e42adb commit 028843e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
32 changes: 8 additions & 24 deletions jni/tools/libjvm_stub/libjvm_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,18 @@ static int try_load_libjvm_under_basepath(const char* basepath) {
/* The returned string has to be freed by the caller. */
static char* find_java_executable() {
char* java_path_candidate = NULL;
const char* path_env;
char* path_env_copy = NULL;
char* path_env;
const char* path_env_entry;
char* res = NULL;

path_env = getenv("PATH");
path_env = get_env_copy("PATH");
if (path_env == NULL) {
trace("PATH not set");
goto cleanup;
}
trace("PATH is set: %s", path_env);
path_env_copy = our_strdup(path_env);
if (path_env_copy == NULL) {
perror(MSG_PREFIX "Failed to allocate copy of PATH");
exit(EXIT_FAILURE);
}

path_env_entry = strtok(path_env_copy, PATH_ENV_SEPARATOR);
path_env_entry = strtok(path_env, PATH_ENV_SEPARATOR);
while (path_env_entry) {
java_path_candidate =
(char*)malloc(strlen(path_env_entry) + strlen(JAVA_EXECUTABLE) + 1);
Expand Down Expand Up @@ -179,32 +173,22 @@ static char* find_java_executable() {
trace("java executable not found in PATH");

cleanup:
if (path_env_copy != NULL) {
free(path_env_copy);
}
if (java_path_candidate != NULL) {
free(java_path_candidate);
}
free(path_env);
free(java_path_candidate);
return res;
}

/* The returned string has to be freed by the caller. */
static char* find_java_home() {
const char* java_home_env;
char* java_home_fallback = NULL;
char* java_executable_path = NULL;
char* pos;
size_t separator_count_from_end;
char* res = NULL;

java_home_env = getenv("JAVA_HOME");
if (java_home_env != NULL) {
trace("JAVA_HOME is set: %s", java_home_env);
res = our_strdup(java_home_env);
if (res == NULL) {
perror(MSG_PREFIX "Failed to allocate copy of JAVA_HOME");
exit(EXIT_FAILURE);
}
res = get_env_copy("JAVA_HOME");
if (res != NULL) {
trace("JAVA_HOME is set: %s", res);
return res;
}

Expand Down
11 changes: 9 additions & 2 deletions jni/tools/libjvm_stub/unix/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ static const size_t MAX_CANDIDATE_PATH_LENGTH = 22;

static int executable_exists(const char* path) { return access(path, X_OK); }

#ifdef __APPLE__
static char* our_strdup(const char* src);
#endif

/* The returned string has to be freed by the caller. */
static char* get_env_copy(const char* key) {
const char* value = getenv(key);
if (value == NULL) {
return NULL;
}
return our_strdup(value);
}

/* The returned string has to be freed by the caller. */
static char* get_java_home_fallback() {
Expand Down
22 changes: 22 additions & 0 deletions jni/tools/libjvm_stub/windows/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ static int executable_exists(const char* path) {
return 0;
}

/* The returned string has to be freed by the caller. */
static char* get_env_copy(const char* key) {
/* On Windows, getenv does not see variables set with SetEnvironmentVariable.
* https://curl.se/mail/lib-2020-01/0006.html */
DWORD size = GetEnvironmentVariableA(key, NULL, 0);
char* value;
if (size == 0) {
/* We treat any error as indicating that the variable is not set. If it were
* empty, the returned size would have been 1 instead to account for the
* terminating null character. */
return NULL;
}
value = (char*)malloc(size);
if (value == NULL) {
return NULL;
}
if (GetEnvironmentVariableA(key, value, size) == 0) {
return NULL;
}
return value;
}

static char* get_java_home_fallback() { return NULL; }

static void* load_library(const char* path) { return LoadLibrary(path); }
Expand Down

0 comments on commit 028843e

Please sign in to comment.