DLu / navigation_layers

154 stars 136 forks source link

Range_sensor_layer: Type conversion from int to unsigned int will cause loop error #66

Open TonyRockrobo opened 4 years ago

TonyRockrobo commented 4 years ago

@DLu In range_sensor_layer.cpp line 323 and 325, variable bx0, bx1, by0, by1 is force converted to unsigned int type from int. When those variable are negative, it will cause infinite loop.

  // Limit Bounds to Grid
  bx0 = std::max(0, bx0);
  by0 = std::max(0, by0);
  bx1 = std::min(static_cast<int>(size_x_), bx1);
  by1 = std::min(static_cast<int>(size_y_), by1);

  for (unsigned int x = bx0; x <= (unsigned int)bx1; x++)
  {
    for (unsigned int y = by0; y <= (unsigned int)by1; y++)
    {
      bool update_xy_cell = true;

This situation will occur when range sensor layer was used in local costmap, and robot pose abruptly changed a lot. It will make ox and oy out of local costmap bounds, and worldToMapNobounds (line 281) may generate negative values.

  // Bounds includes the origin
  worldToMapNoBounds(ox, oy, Ox, Oy);
  bx1 = bx0 = Ox;
  by1 = by0 = Oy;
  touch(ox, oy, &min_x_, &min_y_, &max_x_, &max_y_);

  // Update Map with Target Point
  unsigned int aa, ab;
  if (worldToMap(tx, ty, aa, ab))
  {
    setCost(aa, ab, 233);
    touch(tx, ty, &min_x_, &min_y_, &max_x_, &max_y_);
  }

  double mx, my;

  // Update left side of sonar cone
  mx = ox + cos(theta - max_angle_) * d * 1.2;
  my = oy + sin(theta - max_angle_) * d * 1.2;
  worldToMapNoBounds(mx, my, Ax, Ay);
  bx0 = std::min(bx0, Ax);
  bx1 = std::max(bx1, Ax);
  by0 = std::min(by0, Ay);
  by1 = std::max(by1, Ay);
  touch(mx, my, &min_x_, &min_y_, &max_x_, &max_y_);

Please correct me if I'm wrong.