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

Prevent a segfault on deref of null typed pointer #769

Closed
wants to merge 0 commits into from

Conversation

jonathanstowe
Copy link
Contributor

Stops

perl6 -e 'use NativeCall; my $a = Pointer[int32].new; say $a.deref'
Segmentation fault (core dumped)

Some libraries may quite happily not set a passed pointer if there
isn't any data, this just handles it in a nicer way (obviously
one could do what this patch does in the user code.)

@salortiz
Copy link
Contributor

👍

@moritz
Copy link
Member

moritz commented May 16, 2016

Shouldn't it rather be an exception to dereference an uninitialized pointer?

@jonathanstowe
Copy link
Contributor Author

Possibly but a common use for a pointer is where it's passed to a function to fill and the function returns a status, it is entirely possible that it indicates okay but has no data so doesn't populate the pointer.

@moritz
Copy link
Member

moritz commented May 16, 2016

But shouldn't you check before dereferencing then? It sounds to me like the extra check wouldn't be very expensive, but debugging a potential programming error where a pointer isn't supposed to be NULL would be much more work.

I think I'd be much more comfortable if there was a separate method for "deref but return Int if NULL", and the default deref throwing an exception.

@jonathanstowe
Copy link
Contributor Author

I'm relaxed either way, but it shouldn't segfault whatever, it would be better if there was a Bool coercion on the Pointer to shorten the check if the exception is chosen.

@moritz
Copy link
Member

moritz commented May 16, 2016

👍 to a better Bool coercer.

@jonathanstowe
Copy link
Contributor Author

Shall I just change this so it throws an exception, and add a Bool?

@arnsholt
Copy link
Contributor

Yeah, I think .deref should probably throw an exception when dereferencing a NULL. Alternatively, it could return failure, which is much the same thing of course.

@jonathanstowe
Copy link
Contributor Author

👍 A failure sounds like a good idea :) I'll do that.

@jonathanstowe
Copy link
Contributor Author

Gah, should have rebased against nom :-\

@@ -96,6 +96,19 @@ my class Match is Capture is Cool {
}
}

multi sub infix:<eqv>(Match:D $a, Match:D $b) {
[&&] (
$a =:= $b,
Copy link
Contributor

Choose a reason for hiding this comment

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

can $a =:= $b (container check, right?) be True while $a.to and $b.to are different?

@jonathanstowe
Copy link
Contributor Author

@vendethiel that is already in nom afaik, I screwed up the rebase and added these to this PR by mistake - a2188ec is the only pertinent one :)

@jonathanstowe
Copy link
Contributor Author

jonathanstowe commented Sep 27, 2016

I'm going to do this again (in #888)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants