Skip to content

Commit

Permalink
Add isInitialized to nix::Value
Browse files Browse the repository at this point in the history
Add a method to check if a value has been initialized. This helps avoid
segfaults when calling `type()`.
Useful in the context of the new C API.

Closes #10524
  • Loading branch information
jlesquembre committed Apr 18, 2024
1 parent 40499bc commit ec0d8f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/libexpr/value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ public:
internalType = newType;
}

inline bool isInitialized()
{
return internalType != 0;
}

inline void mkInt(NixInt n)
{
finishValue(tInt, { .integer = n });
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/libexpr/value/value.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "tests/libstore.hh"

#include "value.hh"

namespace nix {

using namespace testing;

TEST_F(LibStoreTest, unsetValue)
{
Value unsetValue;
ASSERT_EQ(false, unsetValue.isInitialized());
ASSERT_EQ(tThunk, unsetValue.type(true));
ASSERT_DEATH(unsetValue.type(), "");
}

TEST_F(LibStoreTest, vInt)
{
Value vInt;
vInt.mkInt(42);
ASSERT_EQ(true, vInt.isInitialized());
}

} // namespace nix

0 comments on commit ec0d8f6

Please sign in to comment.