Skip to content

Commit c860b3a

Browse files
authored
Merge pull request #266 from ldorau/Use_GetEnvironmentVariableA_instead_of_dupenv_s
Use GetEnvironmentVariableA() instead of _dupenv_s()
2 parents e9070a8 + f83423b commit c860b3a

File tree

4 files changed

+91
-27
lines changed

4 files changed

+91
-27
lines changed

src/utils/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake)
66
include(FindThreads)
77

88
set(UMF_UTILS_SOURCES_POSIX
9+
utils_posix_common.c
910
utils_posix_concurrency.c
1011
utils_posix_math.c
1112
)
1213

1314
set(UMF_UTILS_SOURCES_WINDOWS
15+
utils_windows_common.c
1416
utils_windows_concurrency.c
1517
utils_windows_math.c
1618
)

src/utils/utils_common.h

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <stdint.h>
1414
#include <stdio.h>
1515
#include <stdlib.h>
16-
#include <string.h>
1716

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

3736
#define __TLS __declspec(thread)
3837

39-
static inline char *util_getenv(const char *name) {
40-
char *buffer;
41-
size_t numberOfElements;
42-
errno_t err = _dupenv_s(&buffer, &numberOfElements, name);
43-
if (err) {
44-
return NULL;
45-
}
46-
47-
return buffer;
48-
}
49-
50-
static inline void util_free_getenv(char *val) { free(val); }
51-
5238
// TODO: implement util_get_page_size() for Windows
5339
static inline size_t util_get_page_size(void) { return 4096; }
5440

5541
#else /* Linux */
5642

5743
#define __TLS __thread
5844

59-
static inline char *util_getenv(const char *name) { return getenv(name); }
60-
static inline void util_free_getenv(const char *val) {
61-
(void)val; // unused
62-
}
63-
6445
static inline size_t util_get_page_size(void) { return sysconf(_SC_PAGE_SIZE); }
6546

6647
#endif /* _WIN32 */
6748

49+
// util_env_var - populate the given buffer with the value
50+
// of the given environment variable
51+
// Return value
52+
// If the function succeeds, the return value is the number of characters
53+
// stored in the buffer pointed to by buffer, not including
54+
// the terminating null character.
55+
//
56+
// If the buffer is not large enough to hold the data, then:
57+
// 1) the return value equals (-1) * the buffer size (in characters)
58+
// required to hold the string and its terminating null character,
59+
// 2) the content of the buffer is undefined.
60+
//
61+
// If the function fails, the return value is zero.
62+
int util_env_var(const char *envvar, char *buffer, size_t buffer_size);
63+
64+
// Check if the environment variable contains the given string.
65+
int util_env_var_has_str(const char *envvar, const char *str);
66+
6867
// check if we are running in the proxy library
6968
static inline int is_running_in_proxy_lib(void) {
70-
int is_in_proxy_lib_val = 0;
71-
char *ld_preload = util_getenv("LD_PRELOAD");
72-
if (ld_preload && strstr(ld_preload, "libumf_proxy.so")) {
73-
is_in_proxy_lib_val = 1;
74-
}
75-
76-
util_free_getenv(ld_preload);
77-
return is_in_proxy_lib_val;
69+
return util_env_var_has_str("LD_PRELOAD", "libumf_proxy.so");
7870
}
7971

8072
#define NOFUNCTION \

src/utils/utils_posix_common.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#include <stdlib.h>
11+
#include <string.h>
12+
13+
int util_env_var(const char *envvar, char *buffer, size_t buffer_size) {
14+
char *value = getenv(envvar);
15+
if (!value) {
16+
return 0;
17+
}
18+
19+
size_t len = strlen(value) + 1;
20+
if (len > buffer_size) {
21+
return -len;
22+
}
23+
24+
strncpy(buffer, value, buffer_size - 1);
25+
// make sure the string is NULL-terminated
26+
buffer[buffer_size - 1] = 0;
27+
28+
return (len - 1);
29+
}
30+
31+
int util_env_var_has_str(const char *envvar, const char *str) {
32+
char *value = getenv(envvar);
33+
if (value && strstr(value, str)) {
34+
return 1;
35+
}
36+
37+
return 0;
38+
}

src/utils/utils_windows_common.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#include <windows.h>
11+
12+
#include <processenv.h>
13+
14+
#define BUFFER_SIZE 1024
15+
16+
int util_env_var(const char *envvar, char *buffer, size_t buffer_size) {
17+
int ret = GetEnvironmentVariableA(envvar, buffer, (DWORD)buffer_size);
18+
if (ret >= buffer_size) {
19+
return -ret;
20+
}
21+
22+
return ret;
23+
}
24+
25+
int util_env_var_has_str(const char *envvar, const char *str) {
26+
char buffer[BUFFER_SIZE];
27+
if (util_env_var(envvar, buffer, BUFFER_SIZE) > 0) {
28+
return (strstr(buffer, str) != NULL);
29+
}
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)