[Fix](inverted index) fix memory leak when create bkd reader (#18914)

The function compoundReader->openInput is called three times, and if any of these calls fail,
an error is logged, and the function returns early. If one or two of the calls succeed, but the others fail,
there might be a situation where the allocated memory for the IndexInput objects is not freed.

To fix this, you could use std::unique_ptr to manage the memory for IndexInput objects.
This would automatically clean up the memory when the function goes out of scope.
This commit is contained in:
airborne12
2023-04-23 23:21:44 +08:00
committed by GitHub
parent c3baa65de3
commit 07ea350201
3 changed files with 20 additions and 6 deletions

View File

@ -244,6 +244,17 @@ int64_t DorisCompoundReader::fileLength(const char* name) const {
return e->length;
}
bool DorisCompoundReader::openInput(const char* name,
std::unique_ptr<lucene::store::IndexInput>& ret,
CLuceneError& error, int32_t bufferSize) {
lucene::store::IndexInput* tmp;
bool success = openInput(name, tmp, error, bufferSize);
if (success) {
ret.reset(tmp);
}
return success;
}
bool DorisCompoundReader::openInput(const char* name, lucene::store::IndexInput*& ret,
CLuceneError& error, int32_t bufferSize) {
if (stream == nullptr) {

View File

@ -27,6 +27,7 @@
#include <stdint.h>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
@ -82,6 +83,8 @@ public:
int64_t fileLength(const char* name) const override;
bool openInput(const char* name, lucene::store::IndexInput*& ret, CLuceneError& err,
int32_t bufferSize = -1) override;
bool openInput(const char* name, std::unique_ptr<lucene::store::IndexInput>& ret,
CLuceneError& err, int32_t bufferSize = -1);
void renameFile(const char* from, const char* to) override;
void touchFile(const char* name) override;
lucene::store::IndexOutput* createOutput(const char* name) override;

View File

@ -579,9 +579,9 @@ Status BkdIndexReader::get_bkd_reader(std::shared_ptr<lucene::util::bkd::bkd_rea
return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>();
}
CLuceneError err;
lucene::store::IndexInput* data_in;
lucene::store::IndexInput* meta_in;
lucene::store::IndexInput* index_in;
std::unique_ptr<lucene::store::IndexInput> data_in;
std::unique_ptr<lucene::store::IndexInput> meta_in;
std::unique_ptr<lucene::store::IndexInput> index_in;
if (!compoundReader->openInput(
InvertedIndexDescriptor::get_temporary_bkd_index_data_file_name().c_str(), data_in,
@ -596,12 +596,12 @@ Status BkdIndexReader::get_bkd_reader(std::shared_ptr<lucene::util::bkd::bkd_rea
return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>();
}
bkdReader = std::make_shared<lucene::util::bkd::bkd_reader>(data_in);
if (0 == bkdReader->read_meta(meta_in)) {
bkdReader = std::make_shared<lucene::util::bkd::bkd_reader>(data_in.release());
if (0 == bkdReader->read_meta(meta_in.get())) {
return Status::EndOfFile("bkd index file is empty");
}
bkdReader->read_index(index_in);
bkdReader->read_index(index_in.get());
_type_info = get_scalar_type_info((FieldType)bkdReader->type);
if (_type_info == nullptr) {