From b54dfb66dd5bbaae430d4883e6cc7589b8d2e98b Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 11 Dec 2025 13:48:04 +0100 Subject: [PATCH] Fix segfault in getUnitTestData() when env var not set 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. --- .../include/nix/util/tests/characterization.hh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libutil-test-support/include/nix/util/tests/characterization.hh b/src/libutil-test-support/include/nix/util/tests/characterization.hh index d8fad1df9..67ad8fb51 100644 --- a/src/libutil-test-support/include/nix/util/tests/characterization.hh +++ b/src/libutil-test-support/include/nix/util/tests/characterization.hh @@ -15,7 +15,11 @@ namespace nix { */ static inline std::filesystem::path getUnitTestData() { - return getEnv("_NIX_TEST_UNIT_DATA").value(); + auto data = getEnv("_NIX_TEST_UNIT_DATA"); + if (!data) + throw Error( + "_NIX_TEST_UNIT_DATA environment variable is not set. Recommendation: use meson, example: 'meson test -C build --gdb'"); + return std::filesystem::path(*data); } /**