From d31b9bcedad07c14aecfeea17e16c7cc2306351b Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 15 Feb 2020 10:34:21 +0100 Subject: [PATCH 1/2] src: remove unused include from node_file.cc --- src/node_file.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/node_file.cc b/src/node_file.cc index 2f66080e5caa14..558cb207a1b590 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -32,7 +32,6 @@ #include "req_wrap-inl.h" #include "stream_base-inl.h" #include "string_bytes.h" -#include "string_search.h" #include #include From ed8b67167e1ace1aa73a709150797d2f1c3b6956 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 15 Feb 2020 10:34:21 +0100 Subject: [PATCH 2/2] src: fix -Wmaybe-uninitialized compiler warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Fixes: https://github.com/nodejs/node/pull/31532 Fixes: https://github.com/nodejs/node/pull/31798 --- src/string_search.h | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/string_search.h b/src/string_search.h index b339c355fe168d..3d06f32058f673 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::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::InitialSearch( badness += j; } else { PopulateBoyerMooreHorspoolTable(); - strategy_ = &StringSearch::BoyerMooreHorspoolSearch; + strategy_ = SearchStrategy::kBoyerMooreHorspool; return BoyerMooreHorspoolSearch(subject, i); } }