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

libexpr: Make unsafeGetAttrPos not crash on noPos #2010

Merged
merged 1 commit into from Apr 23, 2018

Conversation

dezgeg
Copy link
Contributor

@dezgeg dezgeg commented Mar 26, 2018

Currently e.g. builtins.unsafeGetAttrPos "path" builtins will
eventually segfault because pos->file is an unset Symbol.

Found by afl-fuzz.

@dezgeg
Copy link
Contributor Author

dezgeg commented Mar 26, 2018

Not sure what's the best fix here, just picked one.

@shlevy
Copy link
Member

shlevy commented Mar 26, 2018

I have a mild preference for null instead of a special string

@dezgeg
Copy link
Contributor Author

dezgeg commented Mar 26, 2018

I did think of null, but I have the prediction that whoever calls this will immediately do something like:

let pos = builtins.unsafeGetAttrPos foo bar; in "${pos.file}:${toString pos.line}:${toString pos.column}"

which will then blow up on null.

@shlevy
Copy link
Member

shlevy commented Mar 26, 2018

It should IMO 😀

@shlevy
Copy link
Member

shlevy commented Mar 26, 2018

Less facetiously: What is the "right" behavior for that code in the noPos case?

@dezgeg
Copy link
Contributor Author

dezgeg commented Mar 26, 2018

Well I don't think "(unknown file):0:0" is too bad of an output, but I guess it could be beautified to handle noPos like this:

let
    pos = builtins.unsafeGetAttrPos foo bar;
in if pos.line == 0
     then "(undefined position)"
     else "${pos.file}:${toString pos.line}:${toString pos.column}"

@shlevy
Copy link
Member

shlevy commented Mar 26, 2018

If you're doing that check then why not just check for null (or attr non-existence?)

@dezgeg
Copy link
Contributor Author

dezgeg commented Mar 26, 2018

Oh I had somehow misinterpreted that question as returning { column = 0; file = null; line = 0; } instead of returning null.

IMHO the problem of null there is that now null can mean both "the attribute didn't exist" and "position of that attribute is not known" while it previously could only be the former. So before this change, one could reasonably write code like:

{ name, version, sha256 } @ args:
let
    pos = builtins.unsafeGetAttrPos args "name";
in mkDerivation (args // ...)

with the expectation that pos is always non-null.

@edolstra
Copy link
Member

unsafeGetAttrPos should probably just throw an exception if the position is undefined.

@shlevy shlevy added the backlog label Apr 1, 2018
@shlevy shlevy self-assigned this Apr 1, 2018
Currently e.g. `builtins.unsafeGetAttrPos "abort" builtins` will
eventually segfault because pos->file is an unset Symbol.

Found by afl-fuzz.
@dezgeg
Copy link
Contributor Author

dezgeg commented Apr 3, 2018

Throwing an exception sounds pretty inconsistent given that the function already returns null in one error case.

I guess returning null in the crashing case is mostly fine given that nothing in nixpkgs would break on a sudden null. I've updated the commit.

Copy link
Member

@shlevy shlevy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me. @edolstra ?

@shlevy shlevy merged commit af86132 into NixOS:master Apr 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants