Skip to content

Conversation

@geverl
Copy link

@geverl geverl commented Jul 18, 2025

This pull request fixes a bug in the loadEnv function.

The Bug:

  • The code used putenv(envVar.c_str()), which is incorrect on many systems because c_str() returns a const char* while putenv expects a char*.
  • More importantly, envVar is a local variable. Passing its memory to putenv results in a dangling pointer when the variable goes out of scope, leading to undefined behavior.

The Solution:

  • Replaced putenv with setenv(key.c_str(), value.c_str(), 1).
  • Added #include <cstdlib> for setenv.
  • setenv is the correct, portable, and safe way to set environment variables, as it manages its own memory for the values.

This resolves the compilation error and fixes the underlying memory bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant