diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2020-02-15 12:34:21 +0300 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2020-03-11 21:21:09 +0300 |
commit | fa376f420ce76daf05ab3fd4ea214c8c238e1aae (patch) | |
tree | bf5a5aa64c751ce577d5455e23f3738817af01ed /src/string_search.h | |
parent | c3aa3e70f0cbf8b0e7547d4c01d50e6eb1c7b314 (diff) |
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: https://github.com/nodejs/node/issues/26733
Refs: https://github.com/nodejs/node/pull/31532
Refs: https://github.com/nodejs/node/pull/31798
PR-URL: https://github.com/nodejs/node/pull/31809
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/string_search.h')
-rw-r--r-- | src/string_search.h | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/string_search.h b/src/string_search.h index b339c355fe1..3d06f32058f 100644 --- a/src/string_search.h +++ b/src/string_search.h @@ -100,17 +100,29 @@ class StringSearch : private StringSearchBase { CHECK_GT(pattern_length, 0); if (pattern_length < kBMMinPatternLength) { if (pattern_length == 1) { - strategy_ = &StringSearch::SingleCharSearch; + strategy_ = SearchStrategy::kSingleChar; return; } - strategy_ = &StringSearch::LinearSearch; + strategy_ = SearchStrategy::kLinear; return; } - strategy_ = &StringSearch::InitialSearch; + strategy_ = SearchStrategy::kInitial; } size_t Search(Vector subject, size_t index) { - return (this->*strategy_)(subject, index); + switch (strategy_) { + case kBoyerMooreHorspool: + return BoyerMooreHorspoolSearch(subject, index); + case kBoyerMoore: + return BoyerMooreSearch(subject, index); + case kInitial: + return InitialSearch(subject, index); + case kLinear: + return LinearSearch(subject, index); + case kSingleChar: + return SingleCharSearch(subject, index); + } + UNREACHABLE(); } static inline int AlphabetSize() { @@ -149,10 +161,17 @@ class StringSearch : private StringSearchBase { return bad_char_occurrence[equiv_class]; } + enum SearchStrategy { + kBoyerMooreHorspool, + kBoyerMoore, + kInitial, + kLinear, + kSingleChar, + }; + // The pattern to search for. Vector pattern_; - // Pointer to implementation of the search. - SearchFunction strategy_; + SearchStrategy strategy_; // Cache value of Max(0, pattern_length() - kBMMaxShift) size_t start_; }; @@ -476,7 +495,7 @@ size_t StringSearch<Char>::BoyerMooreHorspoolSearch( badness += (pattern_length - j) - last_char_shift; if (badness > 0) { PopulateBoyerMooreTable(); - strategy_ = &StringSearch::BoyerMooreSearch; + strategy_ = SearchStrategy::kBoyerMoore; return BoyerMooreSearch(subject, index); } } @@ -548,7 +567,7 @@ size_t StringSearch<Char>::InitialSearch( badness += j; } else { PopulateBoyerMooreHorspoolTable(); - strategy_ = &StringSearch::BoyerMooreHorspoolSearch; + strategy_ = SearchStrategy::kBoyerMooreHorspool; return BoyerMooreHorspoolSearch(subject, i); } } |