Skip to content

Use GetEnvironmentVariableA() instead of _dupenv_s() #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake)
include(FindThreads)

set(UMF_UTILS_SOURCES_POSIX
utils_posix_common.c
utils_posix_concurrency.c
utils_posix_math.c
)

set(UMF_UTILS_SOURCES_WINDOWS
utils_windows_common.c
utils_windows_concurrency.c
utils_windows_math.c
)
Expand Down
46 changes: 19 additions & 27 deletions src/utils/utils_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef _WIN32
#include <sys/syscall.h>
Expand All @@ -36,45 +35,38 @@ extern "C" {

#define __TLS __declspec(thread)

static inline char *util_getenv(const char *name) {
char *buffer;
size_t numberOfElements;
errno_t err = _dupenv_s(&buffer, &numberOfElements, name);
if (err) {
return NULL;
}

return buffer;
}

static inline void util_free_getenv(char *val) { free(val); }

// TODO: implement util_get_page_size() for Windows
static inline size_t util_get_page_size(void) { return 4096; }

#else /* Linux */

#define __TLS __thread

static inline char *util_getenv(const char *name) { return getenv(name); }
static inline void util_free_getenv(const char *val) {
(void)val; // unused
}

static inline size_t util_get_page_size(void) { return sysconf(_SC_PAGE_SIZE); }

#endif /* _WIN32 */

// util_env_var - populate the given buffer with the value
// of the given environment variable
// Return value
// If the function succeeds, the return value is the number of characters
// stored in the buffer pointed to by buffer, not including
// the terminating null character.
//
// If the buffer is not large enough to hold the data, then:
// 1) the return value equals (-1) * the buffer size (in characters)
// required to hold the string and its terminating null character,
// 2) the content of the buffer is undefined.
//
// If the function fails, the return value is zero.
int util_env_var(const char *envvar, char *buffer, size_t buffer_size);

// Check if the environment variable contains the given string.
int util_env_var_has_str(const char *envvar, const char *str);

// check if we are running in the proxy library
static inline int is_running_in_proxy_lib(void) {
int is_in_proxy_lib_val = 0;
char *ld_preload = util_getenv("LD_PRELOAD");
if (ld_preload && strstr(ld_preload, "libumf_proxy.so")) {
is_in_proxy_lib_val = 1;
}

util_free_getenv(ld_preload);
return is_in_proxy_lib_val;
return util_env_var_has_str("LD_PRELOAD", "libumf_proxy.so");
}

#define NOFUNCTION \
Expand Down
38 changes: 38 additions & 0 deletions src/utils/utils_posix_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#include <stdlib.h>
#include <string.h>

int util_env_var(const char *envvar, char *buffer, size_t buffer_size) {
char *value = getenv(envvar);
if (!value) {
return 0;
}

size_t len = strlen(value) + 1;
if (len > buffer_size) {
return -len;
}

strncpy(buffer, value, buffer_size - 1);
// make sure the string is NULL-terminated
buffer[buffer_size - 1] = 0;

return (len - 1);
}

int util_env_var_has_str(const char *envvar, const char *str) {
char *value = getenv(envvar);
if (value && strstr(value, str)) {
return 1;
}

return 0;
}
32 changes: 32 additions & 0 deletions src/utils/utils_windows_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#include <windows.h>

#include <processenv.h>

#define BUFFER_SIZE 1024

int util_env_var(const char *envvar, char *buffer, size_t buffer_size) {
int ret = GetEnvironmentVariableA(envvar, buffer, (DWORD)buffer_size);
if (ret >= buffer_size) {
return -ret;
}

return ret;
}

int util_env_var_has_str(const char *envvar, const char *str) {
char buffer[BUFFER_SIZE];
if (util_env_var(envvar, buffer, BUFFER_SIZE) > 0) {
return (strstr(buffer, str) != NULL);
}

return 0;
}