Skip to content

Commit 55d12df

Browse files
committed
libstore-tests: Don't leak memory in tests
We shouldn't leak memory in unit tests in order to make enabling ASAN easier.
1 parent 448cfb7 commit 55d12df

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/libstore-test-support/include/nix/store/tests/nix_api_store.hh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public:
3434
Store * store;
3535
std::string nixDir;
3636
std::string nixStoreDir;
37+
std::string nixStateDir;
38+
std::string nixLogDir;
3739

3840
protected:
3941
void init_local_store()
@@ -53,11 +55,13 @@ protected:
5355
#endif
5456

5557
nixStoreDir = nixDir + "/my_nix_store";
58+
nixStateDir = nixDir + "/my_state";
59+
nixLogDir = nixDir + "/my_log";
5660

5761
// Options documented in `nix help-stores`
5862
const char * p1[] = {"store", nixStoreDir.c_str()};
59-
const char * p2[] = {"state", (new std::string(nixDir + "/my_state"))->c_str()};
60-
const char * p3[] = {"log", (new std::string(nixDir + "/my_log"))->c_str()};
63+
const char * p2[] = {"state", nixStateDir.c_str()};
64+
const char * p3[] = {"log", nixLogDir.c_str()};
6165

6266
const char ** params[] = {p1, p2, p3, nullptr};
6367

src/libstore-tests/nix_api_store.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,21 @@ TEST_F(nix_api_store_test, ReturnsValidStorePath)
7171
ASSERT_NE(result, nullptr);
7272
ASSERT_STREQ("name", result->path.name().data());
7373
ASSERT_STREQ(PATH_SUFFIX.substr(1).c_str(), result->path.to_string().data());
74+
nix_store_path_free(result);
7475
}
7576

7677
TEST_F(nix_api_store_test, SetsLastErrCodeToNixOk)
7778
{
78-
nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
79+
StorePath * path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
7980
ASSERT_EQ(ctx->last_err_code, NIX_OK);
81+
nix_store_path_free(path);
8082
}
8183

8284
TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull)
8385
{
84-
ASSERT_NO_THROW(nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str()));
86+
StorePath * path = nullptr;
87+
ASSERT_NO_THROW(path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str()));
88+
nix_store_path_free(path);
8589
}
8690

8791
TEST_F(nix_api_store_test, get_version)
@@ -119,6 +123,7 @@ TEST_F(nix_api_store_test, nix_store_is_valid_path_not_in_store)
119123
{
120124
StorePath * path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
121125
ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, path));
126+
nix_store_path_free(path);
122127
}
123128

124129
TEST_F(nix_api_store_test, nix_store_real_path)

0 commit comments

Comments
 (0)