add safty check 4 annoy load index (#2009)

Signed-off-by: bo-huang <1013767522@qq.com>
This commit is contained in:
bo huang 2020-04-21 14:23:42 +08:00 committed by GitHub
parent f039032c8c
commit 574ff66c01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1140,6 +1140,10 @@ public:
_n_nodes = (S)(index_size / _s);
// _nodes = (Node*)malloc(_s * _n_nodes);
_nodes = (Node*)malloc((size_t)index_size);
if (_nodes == nullptr) {
set_error_from_errno(error, "alloc failed when load_index 4 annoy");
return false;
}
memcpy(_nodes, index_data, (size_t)index_size);
// Find the roots by scanning the end of the file and taking the nodes with most descendants