Skip to content

Commit

Permalink
[BUGFIX] investigate null pointer free problem in getGraphStatistics …
Browse files Browse the repository at this point in the history
…API (#173)
  • Loading branch information
kpango authored Nov 8, 2024
1 parent 6f471de commit 948a3a7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
15 changes: 9 additions & 6 deletions lib/NGT/Capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ NGTGraphStatistics ngt_get_graph_statistics(const NGTIndex index, char mode, siz
return cStats;
}

NGT::Index* pindex = static_cast<NGT::Index*>(index);
NGT::GraphIndex &graph = static_cast<NGT::GraphIndex&>(pindex->getIndex());
NGT::GraphIndex::GraphStatistics stats = NGT::GraphIndex::getGraphStatistics(graph, mode, edgeSize);
try {
NGT::Index* pindex = static_cast<NGT::Index*>(index);
NGT::GraphIndex &graph = static_cast<NGT::GraphIndex&>(pindex->getIndex());
NGT::GraphIndex::GraphStatistics stats = NGT::GraphIndex::getGraphStatistics(graph, mode, edgeSize);
cStats.numberOfObjects = stats.getNumberOfObjects();
cStats.numberOfIndexedObjects = stats.getNumberOfIndexedObjects();
cStats.sizeOfObjectRepository = stats.getSizeOfObjectRepository();
Expand Down Expand Up @@ -151,15 +151,18 @@ NGTGraphStatistics ngt_get_graph_statistics(const NGTIndex index, char mode, siz

cStats.indegreeCountSize = stats.getIndegreeCount().size();
cStats.indegreeCount = new int64_t[cStats.indegreeCountSize];
std::copy(stats.getIndegreeCount().begin(), stats.getIndegreeCount().end(), cStats.indegreeCount);
std::memcpy(cStats.indegreeCount, stats.getIndegreeCount().data(),
cStats.indegreeCountSize * sizeof(size_t));

cStats.outdegreeHistogramSize = stats.getOutdegreeHistogram().size();
cStats.outdegreeHistogram = new size_t[cStats.outdegreeHistogramSize];
std::copy(stats.getOutdegreeHistogram().begin(), stats.getOutdegreeHistogram().end(), cStats.outdegreeHistogram);
std::memcpy(cStats.outdegreeHistogram, stats.getOutdegreeHistogram().data(),
cStats.outdegreeHistogramSize * sizeof(size_t));

cStats.indegreeHistogramSize = stats.getIndegreeHistogram().size();
cStats.indegreeHistogram = new size_t[cStats.indegreeHistogramSize];
std::copy(stats.getIndegreeHistogram().begin(), stats.getIndegreeHistogram().end(), cStats.indegreeHistogram);
std::memcpy(cStats.indegreeHistogram, stats.getIndegreeHistogram().data(),
cStats.indegreeHistogramSize * sizeof(size_t));

cStats.valid = stats.isValid();
}catch(std::exception &err){
Expand Down
32 changes: 12 additions & 20 deletions lib/NGT/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,8 @@ NGT::GraphIndex::getGraphStatistics(NGT::GraphIndex &outGraph, char mode, size_t
valid = false;
continue;
}
if (node == nullptr)
continue;
numberOfNodes++;
size_t esize = std::min(node->size(), edgeSize); // edge size limitation by using edgeSize argument.
if (esize == 0) {
Expand All @@ -2102,7 +2104,7 @@ NGT::GraphIndex::getGraphStatistics(NGT::GraphIndex &outGraph, char mode, size_t
NGT::ObjectDistance &n = (*node)[i];
#endif
if (std::isnan(n.distance)) {
stringstream msg;
std::stringstream msg;
msg << "NGT::GraphIndex::getGraphStatistics: Fatal inner error! The graph has a node with nan distance. " << id << ":" << n.id << ":" << n.distance;
NGTThrowException(msg);
}
Expand All @@ -2113,8 +2115,7 @@ NGT::GraphIndex::getGraphStatistics(NGT::GraphIndex &outGraph, char mode, size_t
indegreeCount[n.id]++;
indegree[n.id].push_back(n.distance);
numberOfOutdegree++;
double d = n.distance;
distance += d;
distance += n.distance;
}
}

Expand All @@ -2129,6 +2130,8 @@ NGT::GraphIndex::getGraphStatistics(NGT::GraphIndex &outGraph, char mode, size_t
} catch (NGT::Exception &err) {
continue;
}
if (n == nullptr)
continue;
NGT::GraphNode &node = *n;
for (size_t i = 0; i < node.size(); i++) {
NGT::GraphNode *nn = nullptr;
Expand All @@ -2140,12 +2143,6 @@ NGT::GraphIndex::getGraphStatistics(NGT::GraphIndex &outGraph, char mode, size_t
#endif
} catch (NGT::Exception &err) {
count++;
#if defined(NGT_SHARED_MEMORY_ALLOCATOR)
std::cerr << "Directed edge! " << id << "->" << node.at(i, graph.allocator).id << " no object. "
<< node.at(i, graph.allocator).id << std::endl;
#else
std::cerr << "Directed edge! " << id << "->" << node[i].id << " no object. " << node[i].id << std::endl;
#endif
continue;
}
NGT::GraphNode &nnode = *nn;
Expand All @@ -2160,15 +2157,8 @@ NGT::GraphIndex::getGraphStatistics(NGT::GraphIndex &outGraph, char mode, size_t
break;
}
}
if (!found) {
#if defined(NGT_SHARED_MEMORY_ALLOCATOR)
std::cerr << "Directed edge! " << id << "->" << node.at(i, graph.allocator).id << " no edge. "
<< node.at(i, graph.allocator).id << "->" << id << std::endl;
#else
std::cerr << "Directed edge! " << id << "->" << node[i].id << " no edge. " << node[i].id << "->" << id << std::endl;
#endif
if (!found)
count++;
}
}
}
std::cerr << "The number of directed edges=" << count << std::endl;
Expand All @@ -2188,6 +2178,8 @@ NGT::GraphIndex::getGraphStatistics(NGT::GraphIndex &outGraph, char mode, size_t
std::cerr << "ngt info: Warning. Cannot get the node. ID=" << id << ":" << err.what() << std::endl;
continue;
}
if (n == nullptr)
continue;
NGT::GraphNode &node = *n;
if (node.size() < dcsize) {
d10SkipCount++;
Expand Down Expand Up @@ -2364,9 +2356,9 @@ NGT::GraphIndex::getGraphStatistics(NGT::GraphIndex &outGraph, char mode, size_t
stats.setModeIndegree(modeIndegree);
stats.setC5Indegree(c5);
stats.setC1Indegree(c1);
stats.setIndegreeCount(indegreeCount);
stats.setOutdegreeHistogram(outdegreeHistogram);
stats.setIndegreeHistogram(indegreeHistogram);
stats.setIndegreeCount(std::move(indegreeCount));
stats.setOutdegreeHistogram(std::move(outdegreeHistogram));
stats.setIndegreeHistogram(std::move(indegreeHistogram));
stats.setValid(valid);

return stats;
Expand Down
6 changes: 3 additions & 3 deletions lib/NGT/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,9 @@ namespace NGT {
void setModeIndegree(size_t value) { modeIndegree = value; }
void setC5Indegree(double value) { c5Indegree = value; }
void setC1Indegree(double value) { c1Indegree = value; }
void setIndegreeCount(std::vector<int64_t> value) { indegreeCount = value; }
void setOutdegreeHistogram(std::vector<size_t> value) { outdegreeHistogram = value; }
void setIndegreeHistogram(std::vector<size_t> value) { indegreeHistogram = value; }
void setIndegreeCount(std::vector<int64_t>&& value) { indegreeCount = std::move(value); }
void setIndegreeHistogram(std::vector<size_t>&& value) { indegreeHistogram = std::move(value); }
void setOutdegreeHistogram(std::vector<size_t>&& value) { outdegreeHistogram = std::move(value); }
void setValid(bool value) { valid = value; }

size_t numberOfObjects;
Expand Down

0 comments on commit 948a3a7

Please sign in to comment.