-
Notifications
You must be signed in to change notification settings - Fork 38
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
LocatedSpan::get_unoffsetted_slice can lead to UB #88
Comments
Hmm, that's a nasty side-effect of #76. I don't see how to fix this without going back on the generalization added by #76. (though at least we wouldn't need to revert to the macros) |
Well, I guess something like this would work: diff --git a/src/lib.rs b/src/lib.rs
index 66a19f4..cd097f4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -118,6 +118,12 @@ use nom::{
#[cfg(feature = "stable-deref-trait")]
use stable_deref_trait::StableDeref;
+pub unsafe trait WellBehavedFragment {}
+
+unsafe impl<'a> WellBehavedFragment for &'a [u8] {}
+unsafe impl<'a> WellBehavedFragment for &'a str {}
+unsafe impl WellBehavedFragment for String {}
+
/// A LocatedSpan is a set of meta information about the location of a token, including extra
/// information.
///
@@ -323,7 +329,7 @@ impl<T, X> LocatedSpan<T, X> {
}
}
-impl<T: AsBytes, X> LocatedSpan<T, X> {
+impl<T: AsBytes + WellBehavedFragment, X> LocatedSpan<T, X> {
// Attempt to get the "original" data slice back, by extending
// self.fragment backwards by self.offset.
// Note that any bytes truncated from after self.fragment will not
diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs
index 74dd0db..48b7662 100644
--- a/tests/integration_tests.rs
+++ b/tests/integration_tests.rs
@@ -1,5 +1,5 @@
use nom::{error::ErrorKind, error_position, AsBytes, FindSubstring, IResult, InputLength, Slice};
-use nom_locate::LocatedSpan;
+use nom_locate::{LocatedSpan, WellBehavedFragment};
use std::cmp;
use std::fmt::Debug;
use std::ops::{Range, RangeFull};
@@ -59,7 +59,7 @@ struct Position {
fn test_str_fragments<'a, F, T>(parser: F, input: T, positions: Vec<Position>)
where
F: Fn(LocatedSpan<T>) -> IResult<LocatedSpan<T>, Vec<LocatedSpan<T>>>,
- T: InputLength + Slice<Range<usize>> + Slice<RangeFull> + Debug + PartialEq + AsBytes,
+ T: InputLength + Slice<Range<usize>> + Slice<RangeFull> + Debug + PartialEq + AsBytes + WellBehavedFragment,
{
let res = parser(LocatedSpan::new(input.slice(..)))
.map_err(|err| { as it requires library users to explicitly use |
This function is called from public and safe functions like
get_line_beginning
. It assumes that the current fragment is part of a larger fragment and attempts to read before the beginning of the current fragment. This assumption may be incorrect as demonstrated by the following program that exhibits UB withoutunsafe
and outputs garbage (which can change on every run).Example output:
Related to #45.
The text was updated successfully, but these errors were encountered: