From 157c60a09ff02cd89397fc480bb86dd539dd456e Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 1 Sep 2003 15:44:39 +0000 Subject: * re.c (rb_memsearch): fix overrun. [ruby-talk:80759] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4477 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- re.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 're.c') diff --git a/re.c b/re.c index 53c6832c2a..7460d6ae3d 100644 --- a/re.c +++ b/re.c @@ -107,7 +107,8 @@ rb_memsearch(x0, m, y0, n) #define KR_REHASH(a, b, h) (((h) << 1) - ((a)< n) return -1; + s = y; e = s + n - m; /* Preprocessing */ /* computes d = 2^(m-1) with @@ -116,36 +117,38 @@ rb_memsearch(x0, m, y0, n) if (d > m) d = m; if (ruby_ignorecase) { + if (n == m) { + return rb_memcicmp(x, s, m) == 0 ? 0 : -1; + } /* Prepare hash value */ for (hy = hx = i = 0; i < d; ++i) { hx = KR_REHASH(0, casetable[x[i]], hx); hy = KR_REHASH(0, casetable[s[i]], hy); } /* Searching */ - while (s < e) { - if (hx == hy && rb_memcicmp(x, s, m) == 0) { - return s-y; - } + while (hx != hy || rb_memcicmp(x, s, m)) { + if (s >= e) return -1; hy = KR_REHASH(casetable[*s], casetable[*(s+d)], hy); s++; } } else { + if (n == m) { + return memcmp(x, s, m) == 0 ? 0 : -1; + } /* Prepare hash value */ for (hy = hx = i = 0; i < d; ++i) { hx = KR_REHASH(0, x[i], hx); hy = KR_REHASH(0, s[i], hy); } /* Searching */ - while (s < e) { - if (hx == hy && memcmp(x, s, m) == 0) { - return s-y; - } + while (hx != hy || memcmp(x, s, m)) { + if (s >= e) return -1; hy = KR_REHASH(*s, *(s+d), hy); s++; } } - return -1; + return s-y; } #define REG_CASESTATE FL_USER0 -- cgit v1.2.3