grid_map icon indicating copy to clipboard operation
grid_map copied to clipboard

LineIterator has potential bug?

Open victl opened this issue 8 years ago • 4 comments

I've been using grid_map for a while. I found LineIterator occasionally crash if I initialize it with two 'Position's, and access the grid map with

LineIterator iter(*grid_map_, pos1, pos2);
for (; !iter.isPastEnd(); ++iter)
{
  grid_map_->at("map", *iter) = foo;
}

This is most likely when both 'pos1' and 'pos2' are out of valid range.

I looked into the source code of 'LineIterator' and found that the constructor will NOT initialize anything if 'getIndexLimitedToMapRange()' returns false. In this case, when you invoke 'isPastEnd()', it'll probably return false because it actually returns 'iCell_ > nCells_'. NOTE the 'iCell_' and 'nCells_' were not initialized properly.

victl avatar May 21 '17 08:05 victl

Thanks for reporting this! We have a unit test for the case where both start and end point are outside the map, so I'm unsure what happens in your case.

Could I ask you to recreate the erroneous behavior from your description in a unit test and create a PR?

pfankhauser avatar May 24 '17 13:05 pfankhauser

I met the same problem. When both getIndexLimitedToMapRange failed, iCell_ and nCells_ were not initialized. The problem is solved if I simply set them to zero in the constructor.

jinkunw avatar Dec 07 '18 22:12 jinkunw

The problem with current implementation is that if the start and end points are outside of the gridmap size and the (start,end) line doesn't intersect with the grid_map, it will throw std::invalid_argument("Failed to construct LineIterator."); (line 24 of LineIterator.cpp). This is because the getIndexLimitedToMapRange() function starts from start points and moves towards the end point and if no point inside the gridmap is found (index), then it will return false.

mkhayatian avatar Feb 02 '23 18:02 mkhayatian

Also, a better implementation for getIndexLimitedToMapRange() is finding the intersection between two lines (start,end) and (gridmapEdge[i], gridmapEdge[i+1]) instead of a while loop.

mkhayatian avatar Feb 02 '23 18:02 mkhayatian