Integer underflow and buffer overrun in detail::count_rows on disconnected distance matrices
维护者通常 3 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 src/cpp_common/to_postgres.cpp 中的 detail::count_rows 开始,然后阅读 matrix_to_tuple 以及确定缓冲区大小的 pgr_alloc(tupcount, tuples) 调用。按照 issue 的建议,将大小计算中的减法替换为显式统计非对角线上的有限单元格。当包含不可达对角线的非连通矩阵单元测试得到正确的行数且没有缓冲区溢出时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
Integer underflow and buffer overrun in detail::count_rows on disconnected or sparse distance matrices
Description
In src/cpp_common/to_postgres.cpp, detail::count_rows calculates the number of valid off-diagonal entries in an all-pairs distance matrix (pgr_johnson, pgr_floydWarshall):
size_t
count_rows(const std::vector<std::vector<double>> &matrix) {
int64_t count = 0;
for (size_t i = 0; i < matrix.size(); i++) {
count += std::count_if(
matrix[i].begin(), matrix[i].end(),
[](double value) {
return value != (std::numeric_limits<double>::max)();
});
}
return static_cast<size_t>(count) - matrix.size();
}
This implementation assumes that every diagonal entry matrix[i][i] is non-infinity (!= max()) and is counted in count. However, on disconnected graphs, sparse inputs, or subgraphs with unreachable self-distances:
-
Underallocation & Buffer Overrun: If any diagonal entry equals
max(),countdid not include that cell, yetmatrix.size()is still subtracted. As a result,count_rowsreturns fewer rows than the actual number of valid off-diagonal pairs.matrix_to_tuplethen iterates over all valid off-diagonal entries and writes past the end of thetuplesbuffer allocated bypgr_alloc(tupcount, tuples), resulting in memory corruption. -
Unsigned Integer Underflow: If
count < matrix.size()(for example, on an empty or largely unreachable graph where fewer thanNcells are finite),static_cast<size_t>(count) - matrix.size()underflows unsigned subtraction to a huge value (~1.84e19).pgr_allocthen attempts to allocate an astronomical amount of memory, terminating the PostgreSQL backend process with an out-of-memory error.
Expected Behavior
count_rows should count exactly the valid off-diagonal cells (i != j && matrix[i][j] != max()) without underflowing or underallocating.
Proposed Solution
Replace the subtraction assumption with an explicit off-diagonal count:
size_t
count_rows(const std::vector<std::vector<double>> &matrix) {
size_t count = 0;
for (size_t i = 0; i < matrix.size(); ++i) {
for (size_t j = 0; j < matrix[i].size(); ++j) {
if (i != j && matrix[i][j] != (std::numeric_limits<double>::max)()) {
++count;
}
}
}
return count;
}
I can open a PR with this fix and a unit test covering disconnected matrices.
- 主要语言
- C++
- 星标
- 1.4k
- 派生
- 397
- 平均合并
- 2 天 16 小时
- 30 天内合并 PR
- 8
环境准备
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pgRouting/pgrouting 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 3 天内回复
-
Administrative Enhancement
难度 1/5 1 小时以内 新手友好度 80/100
维护者通常 3 天内回复
-
points_input Developers doc可能已有人在做 @withaman69 于 100 天前认领。 未关闭C/C++ Developers Doc. good first issue
难度 2/5 1-3 小时 新手友好度 70/100
pgRouting/pgrouting#973 · 10 条评论 ·
维护者通常 3 天内回复
-
get_check_data developers doc可能已有人在做 @withaman69 于 102 天前认领。 未关闭C/C++ Developers Doc. good first issue
难度 2/5 1-3 小时 新手友好度 70/100
pgRouting/pgrouting#972 · 7 条评论 ·
维护者通常 3 天内回复
-
pickdeliver: Create and use process/driver pair可能已有人在做 @cvvergara 于 6 天前认领。 未关闭Enhancement Pick & Delivery
难度 4/5 3-5 天 新手友好度 20/100
维护者通常 3 天内回复
查看 pgRouting/pgrouting 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
KLayout/klayout#2554 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
LaneDibello/Kotor-Patch-Manager#375 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复