Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility of a memory leak in zim::Search::begin() #516

Closed
veloman-yunkan opened this issue Mar 6, 2021 · 0 comments · Fixed by #530
Closed

Possibility of a memory leak in zim::Search::begin() #516

veloman-yunkan opened this issue Mar 6, 2021 · 0 comments · Fixed by #530
Assignees
Labels
Milestone

Comments

@veloman-yunkan
Copy link
Collaborator

queryParser in zim::Search::begin() may leak in case of an invalid query:

libzim/src/search.cpp

Lines 337 to 368 in e7fb949

Xapian::QueryParser* queryParser = new Xapian::QueryParser();
if (verbose) {
std::cout << "Setup queryparser using language " << language << std::endl;
}
setup_queryParser(queryParser, internal->database, language, stopwords, suggestion_mode, hasNewSuggestionFormat);
std::string prefix = "";
unsigned flags = Xapian::QueryParser::FLAG_DEFAULT;
if (suggestion_mode) {
if (verbose) {
std::cout << "Mark query as 'partial'" << std::endl;
}
flags |= Xapian::QueryParser::FLAG_PARTIAL;
if ( !hasNewSuggestionFormat
&& this->prefixes.find("S") != std::string::npos ) {
if (verbose) {
std::cout << "Searching in title namespace" << std::endl;
}
prefix = "S";
}
}
Xapian::Query query;
try {
query = parse_query(queryParser, this->query, flags, prefix, suggestion_mode);
} catch (Xapian::QueryParserError& e) {
estimated_matches_number = 0;
return nullptr;
}
if (verbose) {
std::cout << "Parsed query '" << this->query << "' to " << query.get_description() << std::endl;
}
delete queryParser;

This can be fixed easily by making queryParser a stack-allocated object (currently the only justification for it being allocated dynamically could be the large value of sizeof(Xapian::QueryParser) which it isn't). Yet, it would be nice to refactor zim::Search::begin() along the way, breaking that 150+ line function down to smaller functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants