Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function to check if a value has been initialized #10524

Closed
jlesquembre opened this issue Apr 16, 2024 · 0 comments · Fixed by #10555
Closed

Add helper function to check if a value has been initialized #10524

jlesquembre opened this issue Apr 16, 2024 · 0 comments · Fixed by #10555
Labels
c api Nix as a C library with a stable interface feature Feature request or proposal

Comments

@jlesquembre
Copy link
Member

Is your feature request related to a problem? Please describe.

In the C API, the way we set a value is roughly like this:

Value *value = nix_alloc_value(context , state);
nix_init_int(context, value, 42);
nix_get_int(ctx, value);

Calling nix_get_int without prior initialization with nix_init_int will result in a segmentation fault.
This is because attempts to get the type of an uninitialized value triggers a call to abort():

nix/src/libexpr/value.hh

Lines 268 to 287 in 74e4bc9

inline ValueType type(bool invalidIsThunk = false) const
{
switch (internalType) {
case tInt: return nInt;
case tBool: return nBool;
case tString: return nString;
case tPath: return nPath;
case tNull: return nNull;
case tAttrs: return nAttrs;
case tList1: case tList2: case tListN: return nList;
case tLambda: case tPrimOp: case tPrimOpApp: return nFunction;
case tExternal: return nExternal;
case tFloat: return nFloat;
case tThunk: case tApp: return nThunk;
}
if (invalidIsThunk)
return nThunk;
else
abort();
}

Describe the solution you'd like

I suggest to add an isInitialized method to Value for checking initialization in the C API. This would allow us to raise an exception instead of calling abort.

I'm happy to open a PR with these changes, but I'd like to confirm if the approach makes sense.

Priorities

Add 👍 to issues you find important.

@jlesquembre jlesquembre added the feature Feature request or proposal label Apr 16, 2024
jlesquembre added a commit to jlesquembre/nix that referenced this issue Apr 18, 2024
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 NixOS#10524
jlesquembre added a commit to jlesquembre/nix that referenced this issue Apr 18, 2024
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 NixOS#10524
@roberth roberth added the c api Nix as a C library with a stable interface label Apr 19, 2024
jlesquembre added a commit to jlesquembre/nix that referenced this issue Apr 21, 2024
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 NixOS#10524
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 feature Feature request or proposal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants