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

RE2.QuoteMeta doesn’t work with strings containing null byte. #130

Closed
manueljacob opened this issue Jan 19, 2024 · 2 comments
Closed

RE2.QuoteMeta doesn’t work with strings containing null byte. #130

manueljacob opened this issue Jan 19, 2024 · 2 comments
Assignees

Comments

@manueljacob
Copy link

I’d expect RE2.QuoteMeta("abc\0def") to return "abc\\x00def" but it returns "abc". The reason is that in https://github.com/mudge/re2/blob/v2.6.0/ext/re2/re2.cc#L1756, RSTRING_PTR is used to convert the Ruby String to char *. RE2::QuoteMeta then interprets it as a null-terminated string. In addition to the truncation problem, according to the Ruby documentation, the result of RSTRING_PTR may not be NUL-terminated, potentially leading to a buffer over-read.

@mudge mudge self-assigned this Jan 20, 2024
mudge added a commit that referenced this issue Jan 20, 2024
GitHub: #130

Ensure that whenever we pass Ruby string data into RE2 we use the data's
explicit length as returned by RSTRING_LEN rather than relying on
null-termination. RSTRING_PTR doesn't guarantee this (see
https://docs.ruby-lang.org/en/3.3/extension_rdoc.html#label-VALUE+type+conversion)
and we can end up either truncating input or, worse, over-reading.
mudge added a commit that referenced this issue Jan 20, 2024
GitHub: #130

Ensure that whenever we pass Ruby string data into RE2 we use the data's
explicit length as returned by RSTRING_LEN rather than relying on
null-termination. RSTRING_PTR doesn't guarantee this (see
https://docs.ruby-lang.org/en/3.3/extension_rdoc.html#label-VALUE+type+conversion)
and we can end up either truncating input or, worse, over-reading.
mudge added a commit that referenced this issue Jan 20, 2024
GitHub: #130

Ensure that whenever we pass Ruby string data into RE2 we use the data's
explicit length as returned by RSTRING_LEN rather than relying on
null-termination. RSTRING_PTR doesn't guarantee this (see
https://docs.ruby-lang.org/en/3.3/extension_rdoc.html#label-VALUE+type+conversion)
and we can end up either truncating input or, worse, over-reading.
mudge added a commit that referenced this issue Jan 20, 2024
GitHub: #130

Ensure that whenever we pass Ruby string data into RE2 we use the data's
explicit length as returned by RSTRING_LEN rather than relying on
null-termination. RSTRING_PTR doesn't guarantee this (see
https://docs.ruby-lang.org/en/3.3/extension_rdoc.html#label-VALUE+type+conversion)
and we can end up either truncating input or, worse, over-reading.
mudge added a commit that referenced this issue Jan 20, 2024
GitHub: #130

Ensure that whenever we pass Ruby string data into RE2 we use the data's
explicit length as returned by RSTRING_LEN rather than relying on
null-termination. RSTRING_PTR doesn't guarantee this (see
https://docs.ruby-lang.org/en/3.3/extension_rdoc.html#label-VALUE+type+conversion)
and we can end up either truncating input or, worse, over-reading.
mudge added a commit that referenced this issue Jan 20, 2024
GitHub: #130

Ensure that whenever we pass Ruby string data into RE2 we use the data's
explicit length as returned by RSTRING_LEN rather than relying on
null-termination. RSTRING_PTR doesn't guarantee this (see
https://docs.ruby-lang.org/en/3.3/extension_rdoc.html#label-VALUE+type+conversion)
and we can end up either truncating input or, worse, over-reading.
@mudge
Copy link
Owner

mudge commented Jan 20, 2024

Thanks for reporting this, @manueljacob.

This should now be fixed in the newly released 2.7.0, can you please let me know if that resolves your issue?

It should also fix handling of null bytes across the library where possible (note that null bytes are not valid in capturing group names as that is dictated by RE2 itself).

@manueljacob
Copy link
Author

It works for me, thank you!

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

No branches or pull requests

2 participants