Skip to content

Commit adc9b60

Browse files
authored
ggml-cpu: replace putenv with setenv for const-correctness (ggml-org#16573)
## Why it failed When compiling with strict compiler flags (-Wwrite-strings -Werror=discarded-qualifiers), the build fails with the following error: ``` cmake \ -S . \ -B ../llama.cpp.build \ --preset=x64-linux-gcc-debug \ -DCMAKE_INSTALL_PREFIX=/tmp/local \ -DCMAKE_C_FLAGS="-Wwrite-strings -Werror=discarded-qualifiers" && \ cmake --build ../llama.cpp.build/ ... /home/otegami/work/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c: In function ‘ggml_cpu_init’: /home/otegami/work/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c:3572:24: error: passing argument 1 of ‘putenv’ discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers] 3572 | putenv("KMP_BLOCKTIME=200"); // 200ms | ^~~~~~~~~~~~~~~~~~~ In file included from /home/otegami/work/cpp/llama.cpp/ggml/src/./ggml-impl.h:10, from /home/otegami/work/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h:6, from /home/otegami/work/cpp/llama.cpp/ggml/src/ggml-cpu/traits.h:3, from /home/otegami/work/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c:6: /usr/include/stdlib.h:786:26: note: expected ‘char *’ but argument is of type ‘const char *’ 786 | extern int putenv (char *__string) __THROW __nonnull ((1)); | ~~~~~~^~~~~~~~ cc1: some warnings being treated as errors ninja: build stopped: subcommand failed. ``` The issue is that putenv() expects a non-const char * but receives a string literal (const char *). ## How to fix This PR replaces putenv("KMP_BLOCKTIME=200") with setenv("KMP_BLOCKTIME", "200", 0). Benefits of setenv(): - Accepts const char * parameters (no qualifier warnings) - Makes copies of the strings (safer memory handling) - The third parameter (0) ensures we don't overwrite if already set
1 parent ee50ee1 commit adc9b60

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

ggml/src/ggml-cpu/ggml-cpu.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3567,13 +3567,17 @@ void ggml_cpu_init(void) {
35673567
#ifdef GGML_USE_OPENMP
35683568
//if (!getenv("OMP_WAIT_POLICY")) {
35693569
// // set the wait policy to active, so that OpenMP threads don't sleep
3570-
// putenv("OMP_WAIT_POLICY=active");
3570+
// setenv("OMP_WAIT_POLICY", "active", 0)
35713571
//}
35723572

35733573
if (!getenv("KMP_BLOCKTIME")) {
35743574
// set the time to wait before sleeping a thread
35753575
// this is less aggressive than setting the wait policy to active, but should achieve similar results in most cases
3576-
putenv("KMP_BLOCKTIME=200"); // 200ms
3576+
#ifdef _WIN32
3577+
_putenv_s("KMP_BLOCKTIME", "200"); // 200ms
3578+
#else
3579+
setenv("KMP_BLOCKTIME", "200", 0); // 200ms
3580+
#endif
35773581
}
35783582
#endif
35793583
}

0 commit comments

Comments
 (0)