Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rust/ruby-prism/src/string_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ mod tests {
assert_eq!(Ok(false), query("End").is_keyword());
}

#[test]
fn subslice_test() {
// The byte after the slice is outside it and must not affect the result.
assert_eq!(Ok(true), StringQuery::new(&b"a!"[..1]).is_local());
assert_eq!(Ok(true), StringQuery::new(&b"Foo="[..3]).is_constant());
assert_eq!(Ok(true), StringQuery::new(&b"foo?"[..3]).is_method_name());
}

#[test]
fn invalid_encoding_test() {
assert_eq!(Err(InvalidEncodingError), query("A").encoding("UTF-16LE").is_local());
Expand Down
2 changes: 1 addition & 1 deletion src/string_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pm_slice_type(const uint8_t *source, size_t length, const char *encoding_name) {
}

// accept a ! or ? at the end of the slice as a method name
if (*source == '!' || *source == '?' || *source == '=') {
if (source < end && (*source == '!' || *source == '?' || *source == '=')) {
source++;
result = PM_SLICE_TYPE_METHOD_NAME;
}
Expand Down
14 changes: 14 additions & 0 deletions test/prism/ruby/string_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,19 @@ def test_invalid_encoding
StringQuery.new("end".encode("UTF-16LE")).keyword?
end
end

if BACKEND == :FFI
# Ruby strings always have a readable byte after their contents, so call
# the C functions directly with a slice that is immediately followed by a
# method name suffix. That byte is outside the slice and must be ignored.
def test_does_not_read_past_slice
local = FFI::MemoryPointer.from_string("foo?")
assert_equal :PM_STRING_QUERY_TRUE, LibRubyParser.pm_string_query_local(local, 3, "UTF-8")
assert_equal :PM_STRING_QUERY_TRUE, LibRubyParser.pm_string_query_method_name(local, 3, "UTF-8")

constant = FFI::MemoryPointer.from_string("Foo=")
assert_equal :PM_STRING_QUERY_TRUE, LibRubyParser.pm_string_query_constant(constant, 3, "UTF-8")
end
end
end
end
Loading