Skip to content

Conversation

@Fruitchan5
Copy link

Description of Change

Fix the out-of-bounds access in `getMinimum()` function in `sparse_table_range_queries.cpp`.

Added a boundary check to ensure stable behavior for invalid range queries.

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:
This fix prevents crashes due to out-of-bounds access in RMQ function.

int p = logs[end - beg + 1];
int pLen = 1 << p;
if (p >= (int)table.size() || beg >= (int)table[p].size() || (end - pLen + 1) >= (int)table[p].size()) {
std::cerr << "Error: index out of bounds when accessing sparse table." << std::endl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw an error instead

int getMinimum(int beg, int end, const std::vector<T>& logs,
const std::vector<std::vector<T> >& table) {
if(beg<0||end<<beg||end>=(int)table[0].size()){
cout<<"Error:querry range ["<<beg<<","<<end<<"] is invalid."<<endl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cout<<"Error:querry range ["<<beg<<","<<end<<"] is invalid."<<endl;
throw std::invalid_argument("Error:query range ["<<beg<<","<<end<<"] is invalid.");

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also force beg > 0 by using unsigned integer instead preferably uint32_t

template <typename T>
int getMinimum(int beg, int end, const std::vector<T>& logs,
const std::vector<std::vector<T> >& table) {
if(beg<0||end<<beg||end>=(int)table[0].size()){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also why are we left shifting end by beg?
shouldnt it be

Suggested change
if(beg<0||end<<beg||end>=(int)table[0].size()){
if(beg < 0 || end < beg || end >= (int) table[0].size()){

@github-actions
Copy link
Contributor

This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale Author has not responded to the comments for over 2 weeks label Oct 19, 2025
@github-actions
Copy link
Contributor

Please ping one of the maintainers once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions!

@github-actions github-actions bot closed this Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Author has not responded to the comments for over 2 weeks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants