Skip to content

Verify subject length before passing subject pointer to pcre2_match_8 #11

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

Merged
merged 4 commits into from
Sep 4, 2019

Conversation

sharksforarms
Copy link
Contributor

See #10

@sharksforarms sharksforarms marked this pull request as ready for review September 4, 2019 02:45
Copy link
Owner

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

Thanks very much! However, I think the code can be simplified a bit, and with a clearer comment.

Copy link
Owner

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

Thanks! Just one more nit and one more idea: could you add your test case from #10 as a unit test? Ideally, that test would segfault without this patch.

@sharksforarms
Copy link
Contributor Author

Thanks! Just one more nit and one more idea: could you add your test case from #10 as a unit test? Ideally, that test would segfault without this patch.

There's a unit test called jit_test_lazy_alloc_subject in bytes.rs which segfaults if the patch isn't there, do you mean another test case in ffi.rs ?

@BurntSushi
Copy link
Owner

There's a unit test called jit_test_lazy_alloc_subject in bytes.rs which segfaults if the patch isn't there, do you mean another test case in ffi.rs ?

Oh, no, I must have just missed it. What you have is good!

Copy link
Owner

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

Great, thanks!

@BurntSushi BurntSushi merged commit 97d8d11 into BurntSushi:master Sep 4, 2019
@BurntSushi
Copy link
Owner

Okay, this PR is on crates.io in pcre2 0.2.2! Thanks again!

BurntSushi added a commit that referenced this pull request Jul 30, 2024
To work around likely bugs in (older versions of) PCRE2. Namely, at one
point, PCRE2 would dereference the haystack pointer even when the length
was zero.

This was reported in #10 and we worked around this in #11 by passing a
pointer to a const `&[]`, with the (erroneous) presumption that this
would be a valid pointer to dereference. In retrospect though, this was
a little silly, because you should never be dereferencing a pointer to
an empty slice. It's not valid. Alas, at that time, Rust did actually
hand you a valid pointer that could be dereferenced. But [this
PR][rust-pull] changed that. And thus, we're back to where we started:
handing buggy versions of PCRE2 a zero length haystack with a dangling
pointer.

So we fix this once and for all by passing a slice of length 1, but with
a haystack length of 0, to the PCRE2 search routine when searching an
empty haystack. This will guarantee the provision of a dereferencable
pointer should PCRE2 decide to dereference it.

Fixes #42

[rust-pull]: rust-lang/rust#123936
BurntSushi added a commit that referenced this pull request Jul 30, 2024
To work around likely bugs in (older versions of) PCRE2. Namely, at one
point, PCRE2 would dereference the haystack pointer even when the length
was zero.

This was reported in #10 and we worked around this in #11 by passing a
pointer to a const `&[]`, with the (erroneous) presumption that this
would be a valid pointer to dereference. In retrospect though, this was
a little silly, because you should never be dereferencing a pointer to
an empty slice. It's not valid. Alas, at that time, Rust did actually
hand you a valid pointer that could be dereferenced. But [this
PR][rust-pull] changed that. And thus, we're back to where we started:
handing buggy versions of PCRE2 a zero length haystack with a dangling
pointer.

So we fix this once and for all by passing a slice of length 1, but with
a haystack length of 0, to the PCRE2 search routine when searching an
empty haystack. This will guarantee the provision of a dereferencable
pointer should PCRE2 decide to dereference it.

Fixes #42

[rust-pull]: rust-lang/rust#123936
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants