From be72339b2de9fd3f2cd12b2f1b559b2a32f595ad Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 12 Aug 2014 09:59:00 -0400 Subject: [PATCH] filter: Check for empty results when filtering The user could search for a term that isn't stored in the filter index. This is represented through a NULL pointer returned from the Index.find() function. Let's check this pointer before attempting to dereference it ... Signed-off-by: Anna Schumaker --- core/filter.cpp | 15 +++++++++++---- tests/core/filter.cpp | 3 +++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/core/filter.cpp b/core/filter.cpp index 941c797f..a998c759 100644 --- a/core/filter.cpp +++ b/core/filter.cpp @@ -90,14 +90,21 @@ std::string filter :: add(const std::string &text, unsigned int track_id) return reassemble_text(parsed); } +static void do_set_intersection(std::set &a, + std::set &b, + std::set &res) +{ + set_intersection(a.begin(), a.end(), b.begin(), b.end(), + std::inserter >(res, res.begin())); +} + static void find_intersection(std::string &text, std::set &res) { - IndexEntry *it = filter_index.find(text); std::set tmp; + IndexEntry *it = filter_index.find(text); - set_intersection(it->values.begin(), it->values.end(), - res.begin(), res.end(), - std::inserter >(tmp, tmp.begin())); + if (it) + do_set_intersection(res, it->values, tmp); res.swap(tmp); } diff --git a/tests/core/filter.cpp b/tests/core/filter.cpp index 75b27767..7e5781c0 100644 --- a/tests/core/filter.cpp +++ b/tests/core/filter.cpp @@ -96,6 +96,9 @@ static void test_search() unsigned int res5[] = {}; do_test_search("unknown terms", 0, res5); + + unsigned int res6[] = {}; + do_test_search("the chestZ", 0, res6); } int main(int argc, char **argv)