Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Unified Diff: src/string-search.h

Issue 433463002: Avoid calling memchr with a zero range as this is undefined behavior. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/runtime.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/string-search.h
diff --git a/src/string-search.h b/src/string-search.h
index 09bc36ef82ea0e6f5e436da10d529d20248b4d4f..c8b2f155087485e65d33eec4b2f4de71352a7fc9 100644
--- a/src/string-search.h
+++ b/src/string-search.h
@@ -102,6 +102,17 @@ class StringSearch : private StringSearchBase {
return -1;
}
+ static inline const SubjectChar* SafeMemChr(const SubjectChar* string,
+ PatternChar pattern_char,
+ size_t search_length) {
+ if (search_length == 0) {
+ return NULL;
+ } else {
+ return reinterpret_cast<const SubjectChar*>(
+ memchr(string, pattern_char, search_length));
+ }
+ }
+
static int SingleCharSearch(StringSearch<PatternChar, SubjectChar>* search,
Vector<const SubjectChar> subject,
int start_index);
@@ -200,10 +211,8 @@ int StringSearch<PatternChar, SubjectChar>::SingleCharSearch(
PatternChar pattern_first_char = search->pattern_[0];
int i = index;
if (sizeof(SubjectChar) == 1 && sizeof(PatternChar) == 1) {
- const SubjectChar* pos = reinterpret_cast<const SubjectChar*>(
- memchr(subject.start() + i,
- pattern_first_char,
- subject.length() - i));
+ const SubjectChar* pos = SafeMemChr(subject.start() + i, pattern_first_char,
+ subject.length() - i);
if (pos == NULL) return -1;
return static_cast<int>(pos - subject.start());
} else {
@@ -256,10 +265,8 @@ int StringSearch<PatternChar, SubjectChar>::LinearSearch(
int n = subject.length() - pattern_length;
while (i <= n) {
if (sizeof(SubjectChar) == 1 && sizeof(PatternChar) == 1) {
- const SubjectChar* pos = reinterpret_cast<const SubjectChar*>(
- memchr(subject.start() + i,
- pattern_first_char,
- n - i + 1));
+ const SubjectChar* pos =
+ SafeMemChr(subject.start() + i, pattern_first_char, n - i + 1);
if (pos == NULL) return -1;
i = static_cast<int>(pos - subject.start()) + 1;
} else {
@@ -507,10 +514,8 @@ int StringSearch<PatternChar, SubjectChar>::InitialSearch(
badness++;
if (badness <= 0) {
if (sizeof(SubjectChar) == 1 && sizeof(PatternChar) == 1) {
- const SubjectChar* pos = reinterpret_cast<const SubjectChar*>(
- memchr(subject.start() + i,
- pattern_first_char,
- n - i + 1));
+ const SubjectChar* pos =
+ SafeMemChr(subject.start() + i, pattern_first_char, n - i + 1);
if (pos == NULL) {
return -1;
}
« no previous file with comments | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698