Skip to content
Closed
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
22 changes: 8 additions & 14 deletions ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,20 +263,17 @@ private IRubyObject extractBegLen(Ruby runtime, int beg, int len) {
private IRubyObject scan(ThreadContext context, IRubyObject regex, boolean succptr, boolean getstr, boolean headonly) {
final Ruby runtime = context.runtime;
check(context);

ByteList strBL = str.getByteList();
int strBeg = strBL.getBegin();

clearMatched();

if (restLen() < 0) {
return context.nil;
}

ByteList strBL = str.getByteList();
int currPtr = currPtr();

if (regex instanceof RubyRegexp) {
pattern = ((RubyRegexp) regex).preparePattern(str);

int currPtr = currPtr();
int range = currPtr + restLen();

Matcher matcher = pattern.matcher(strBL.getUnsafeBytes(), matchTarget(), range);
Expand All @@ -300,23 +297,20 @@ private IRubyObject scan(ThreadContext context, IRubyObject regex, boolean succp
if (ret < 0) return context.nil;
} else {
RubyString pattern = regex.convertToString();

Encoding patternEnc = str.checkEncoding(pattern);

if (restLen() < pattern.size()) {
return context.nil;
}

ByteList patternBL = pattern.getByteList();
int patternSize = patternBL.realSize();

if (headonly) {
if (ByteList.memcmp(strBL.unsafeBytes(), strBeg + curr, patternBL.unsafeBytes(), patternBL.begin(), patternSize) != 0) {
if (restLen() < pattern.size()) {
return context.nil;
}
if (ByteList.memcmp(strBL.unsafeBytes(), currPtr, patternBL.unsafeBytes(), patternBL.begin(), patternSize) != 0) {
return context.nil;
}
setRegisters(patternSize);
} else {
int pos = StringSupport.index(strBL, patternBL, strBeg + curr, patternEnc);
int pos = StringSupport.index(strBL, patternBL, currPtr, patternEnc);
if (pos == -1) {
return context.nil;
}
Expand Down
13 changes: 7 additions & 6 deletions ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,19 +709,20 @@ strscan_do_scan(VALUE self, VALUE pattern, int succptr, int getstr, int headonly
}
else {
StringValue(pattern);
rb_enc_check(p->str, pattern);
if (S_RESTLEN(p) < RSTRING_LEN(pattern)) {
return Qnil;
}
rb_encoding *enc = rb_enc_check(p->str, pattern);

if (headonly) {
if (S_RESTLEN(p) < RSTRING_LEN(pattern)) {
return Qnil;
}
Comment on lines +715 to +717
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this move? Why is this needless for !headonly?

Copy link
Contributor Author

@naitoh naitoh Oct 15, 2024

Choose a reason for hiding this comment

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

A similar check is made within rb_memsearch() within !headonly.

https://github.com/ruby/ruby/blob/cf8388f76c4c2ff2f46d0d2aa2cf5186e05ff606/re.c#L251-L256

long
rb_memsearch(const void *x0, long m, const void *y0, long n, rb_encoding *enc)
{
    const unsigned char *x = x0, *y = y0;

    if (m > n) return -1;
  • m = RSTRING_LEN(pattern)
  • n = S_RESTLEN(p)

This means the following :
if (RSTRING_LEN(pattern) > S_RESTLEN(p)) return -1;

if (memcmp(CURPTR(p), RSTRING_PTR(pattern), RSTRING_LEN(pattern)) != 0) {
return Qnil;
}
set_registers(p, RSTRING_LEN(pattern));
} else {
}
else {
long pos = rb_memsearch(RSTRING_PTR(pattern), RSTRING_LEN(pattern),
CURPTR(p), S_RESTLEN(p), rb_enc_get(pattern));
CURPTR(p), S_RESTLEN(p), enc);
if (pos == -1) {
return Qnil;
}
Expand Down