Skip to content

Fix getenv segfault#14774

Merged
Mic92 merged 3 commits intoNixOS:masterfrom
roberth:fix-getenv-segfault
Dec 13, 2025
Merged

Fix getenv segfault#14774
Mic92 merged 3 commits intoNixOS:masterfrom
roberth:fix-getenv-segfault

Conversation

@roberth
Copy link
Member

@roberth roberth commented Dec 12, 2025

Motivation

  • Fix crash when running tests outside of meson
  • Fix getEnv related problems

See commit messages.

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added new-cli Relating to the "nix" command c api Nix as a C library with a stable interface labels Dec 12, 2025
* variable is set to ""
*/
std::optional<std::string> getEnvNonEmpty(const std::string & key);
std::optional<std::string> getEnvNonEmptyOptional(const std::string & key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming this seems unnecessarily churny, plus the "optional" part is already part of the type signature so there's no need to put it in the name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that it looks deceptively simple, and you can't easily tell at a call site. Certainly not when reviewing.

Copy link
Member

@Mic92 Mic92 Dec 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is using .value() not enough of an indication?

@Mic92 Mic92 force-pushed the fix-getenv-segfault branch from d3de95d to db78bf1 Compare December 13, 2025 07:33
@Mic92
Copy link
Member

Mic92 commented Dec 13, 2025

Dropped 92767ee for now, so we can already merge the other commits.

The previous implementation called .value() on std::optional without
checking if it had a value. When _NIX_TEST_UNIT_DATA was not set, this
would throw std::bad_optional_access or cause a segfault in code that
used the raw getenv() result.

The new implementation checks the optional first and throws an Error
with a helpful message directing users to run tests via meson. The
example includes --gdb since this situation may arise when trying to
debug tests without knowing about meson's test infrastructure.
The nix_api_store.cc tests and derivation-parser-bench.cc were using raw
getenv() calls or unsafe .value() calls on optional, which would segfault
when passed to std::filesystem::path constructor if the
_NIX_TEST_UNIT_DATA environment variable was not set.
This is mostly theoretical, but the code was calling getenv("TZ")
twice: once to check if it's non-null, and again to get its value.
This creates a potential race condition where the environment could
change between calls.
@Mic92 Mic92 force-pushed the fix-getenv-segfault branch from db78bf1 to 76c09bf Compare December 13, 2025 07:34
Copy link
Contributor

@xokdvium xokdvium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly cosmetic, but overall lgtm and improves DX

@Mic92 Mic92 enabled auto-merge December 13, 2025 07:43
@Mic92 Mic92 added this pull request to the merge queue Dec 13, 2025
Merged via the queue into NixOS:master with commit a6eb2e9 Dec 13, 2025
16 checks passed
@roberth
Copy link
Member Author

roberth commented Dec 13, 2025

Thank you @Mic92

The remaining commit 92767ee is ready for merge, so if someone on the team wants it they can go ahead, or tell me to open a PR.

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

Labels

c api Nix as a C library with a stable interface new-cli Relating to the "nix" command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants