Skip to content

Commit eedec0b

Browse files
authored
fix: add bounds check to prevent out-of-range access in sparse table
1 parent f1eddf4 commit eedec0b

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

range_queries/sparse_table_range_queries.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,19 @@ std::vector<std::vector<T> > buildTable(const std::vector<T>& A,
8383
template <typename T>
8484
int getMinimum(int beg, int end, const std::vector<T>& logs,
8585
const std::vector<std::vector<T> >& table) {
86+
if(beg<0||end<<beg||end>=(int)table[0].size()){
87+
cout<<"Error:querry range ["<<beg<<","<<end<<"] is invalid."<<endl;
88+
return -1;
89+
}
8690
int p = logs[end - beg + 1];
8791
int pLen = 1 << p;
92+
if (p >= (int)table.size() || beg >= (int)table[p].size() || (end - pLen + 1) >= (int)table[p].size()) {
93+
std::cerr << "Error: index out of bounds when accessing sparse table." << std::endl;
94+
return -1;
95+
}
8896
return std::min(table[p][beg], table[p][end - pLen + 1]);
8997
}
98+
9099
} // namespace sparse_table
91100
} // namespace range_queries
92101

0 commit comments

Comments
 (0)