Skip to content

Commit 6f33f64

Browse files
committed
C API: Need to try-catch around new
Per https://en.cppreference.com/w/cpp/memory/new/operator_new.html, it can throw if the allocation fails.
1 parent c72f3dc commit 6f33f64

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/libstore-c/nix_api_store.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ void nix_derivation_free(nix_derivation * drv)
215215

216216
StorePath * nix_store_path_clone(const StorePath * p)
217217
{
218-
return new StorePath{p->path};
218+
try {
219+
return new StorePath{p->path};
220+
} catch (...) {
221+
return nullptr;
222+
}
219223
}
220224

221225
nix_derivation * nix_derivation_from_json(nix_c_context * context, Store * store, const char * json)

src/libutil-c/nix_api_util.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ extern "C" {
1313

1414
nix_c_context * nix_c_context_create()
1515
{
16-
return new nix_c_context();
16+
try {
17+
return new nix_c_context();
18+
} catch (...) {
19+
return nullptr;
20+
}
1721
}
1822

1923
void nix_c_context_free(nix_c_context * context)

0 commit comments

Comments
 (0)