Skip to content

Commit fa376f4

Browse files
bnoordhuisMylesBorins
authored andcommitted
src: fix -Wmaybe-uninitialized compiler warning
Turn the `strategy_` method pointer into an enum-based static dispatch. It's both safer and more secure (no chance of method pointer corruption) and it helps GCC see that the shift and suffix tables it's complaining about are unused in single char search mode. Fixes the following warning: ../src/string_search.h:113:30: warning: ‘search’ may be used uninitialized in this function [-Wmaybe-uninitialized] return (this->*strategy_)(subject, index); Fixes: #26733 Refs: #31532 Refs: #31798 PR-URL: #31809 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c3aa3e7 commit fa376f4

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/string_search.h

+27-8
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,29 @@ class StringSearch : private StringSearchBase {
100100
CHECK_GT(pattern_length, 0);
101101
if (pattern_length < kBMMinPatternLength) {
102102
if (pattern_length == 1) {
103-
strategy_ = &StringSearch::SingleCharSearch;
103+
strategy_ = SearchStrategy::kSingleChar;
104104
return;
105105
}
106-
strategy_ = &StringSearch::LinearSearch;
106+
strategy_ = SearchStrategy::kLinear;
107107
return;
108108
}
109-
strategy_ = &StringSearch::InitialSearch;
109+
strategy_ = SearchStrategy::kInitial;
110110
}
111111

112112
size_t Search(Vector subject, size_t index) {
113-
return (this->*strategy_)(subject, index);
113+
switch (strategy_) {
114+
case kBoyerMooreHorspool:
115+
return BoyerMooreHorspoolSearch(subject, index);
116+
case kBoyerMoore:
117+
return BoyerMooreSearch(subject, index);
118+
case kInitial:
119+
return InitialSearch(subject, index);
120+
case kLinear:
121+
return LinearSearch(subject, index);
122+
case kSingleChar:
123+
return SingleCharSearch(subject, index);
124+
}
125+
UNREACHABLE();
114126
}
115127

116128
static inline int AlphabetSize() {
@@ -149,10 +161,17 @@ class StringSearch : private StringSearchBase {
149161
return bad_char_occurrence[equiv_class];
150162
}
151163

164+
enum SearchStrategy {
165+
kBoyerMooreHorspool,
166+
kBoyerMoore,
167+
kInitial,
168+
kLinear,
169+
kSingleChar,
170+
};
171+
152172
// The pattern to search for.
153173
Vector pattern_;
154-
// Pointer to implementation of the search.
155-
SearchFunction strategy_;
174+
SearchStrategy strategy_;
156175
// Cache value of Max(0, pattern_length() - kBMMaxShift)
157176
size_t start_;
158177
};
@@ -476,7 +495,7 @@ size_t StringSearch<Char>::BoyerMooreHorspoolSearch(
476495
badness += (pattern_length - j) - last_char_shift;
477496
if (badness > 0) {
478497
PopulateBoyerMooreTable();
479-
strategy_ = &StringSearch::BoyerMooreSearch;
498+
strategy_ = SearchStrategy::kBoyerMoore;
480499
return BoyerMooreSearch(subject, index);
481500
}
482501
}
@@ -548,7 +567,7 @@ size_t StringSearch<Char>::InitialSearch(
548567
badness += j;
549568
} else {
550569
PopulateBoyerMooreHorspoolTable();
551-
strategy_ = &StringSearch::BoyerMooreHorspoolSearch;
570+
strategy_ = SearchStrategy::kBoyerMooreHorspool;
552571
return BoyerMooreHorspoolSearch(subject, i);
553572
}
554573
}

0 commit comments

Comments
 (0)