Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geometry->difference() crashes #290

Closed
duvifn opened this issue Mar 9, 2020 · 7 comments
Closed

Geometry->difference() crashes #290

duvifn opened this issue Mar 9, 2020 · 7 comments

Comments

@duvifn
Copy link

duvifn commented Mar 9, 2020

I originally opened this issue in gdal repository, but it turns out to be a geos error.
So open it here.

With the following code I expect to get the difference between two geometries

Instead I get the following error:

/usr/include/c++/7/debug/vector:424:
Error: attempt to subscript container with out-of-bounds index 2, but 
container only holds 2 elements.

Objects involved in the operation:
    sequence "this" @ 0x0x555555772548 {
      type = std::__debug::vector<geos::geom::Coordinate, std::allocator<geos::geom::Coordinate> >;
    }

which happens in this vector access

Steps to reproduce the problem.

Compile the following code with geos 3.8.0.

#include <iostream>
#include <memory>

#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Geometry.h>
#include <geos/io/WKTWriter.h>
#include <geos/io/WKTReader.h>

struct Rectangle {
  double west = -180.0;
  double east = 180.0;
  double south = -90.0;
  double north = 90.0;
};
std::unique_ptr< geos::geom::Geometry >  CreateRectangleGeometry(const Rectangle& rect) {
  auto wkt_reader = geos::io::WKTReader();
  std::string wkt = "POLYGON((" + 
                    std::to_string(rect.west) + " " + 
                    std::to_string(rect.north) + ", " + 
                    std::to_string(rect.east) + " " + 
                    std::to_string(rect.north) + ", "+ 
                    std::to_string(rect.east) + " " + 
                    std::to_string(rect.south) + ", " + 
                    std::to_string(rect.west) + " " + 
                    std::to_string(rect.south) +", " + 
                    std::to_string(rect.west) + " " + 
                    std::to_string(rect.north) +
                    "))";
  auto geom = wkt_reader.read(wkt);
  return geom;

}

int main(int argc, char* argv[]){
  Rectangle rect;
  rect.west = 0.0;
  rect.east = 2.0;
  rect.south = 0.0;
  rect.north = 2.0;

  Rectangle rect2;
  rect2.west = 0.1;
  rect2.east = 4.0;
  rect2.south = 0.1;
  rect2.north = 1.9;
  
  auto geom1 = CreateRectangleGeometry(rect);
  auto geom2 = CreateRectangleGeometry(rect2);
  auto geom3 = geom2->difference(geom1.get());

  geos::io::WKTWriter writer;
  std::cout << writer.write(geom3.get());
  return 0;
}

Operating system

Ubuntu 18.04.2 LTS 64 bit

GEOS version

Self compiled GEOS 3.8.0.

@dr-jts
Copy link
Contributor

dr-jts commented Mar 9, 2020

Yup, looks like a bug, due to this commit: e1fd59f

Looks like the simplest fix is to move the assignment to curr to the top of the loop. @dbaston any chance you can fix this (and add a unit test!)

@pramsey
Copy link
Member

pramsey commented Mar 9, 2020

Looks like it was already fixed, 2 weeks after release of 3.8.0 at 3fc6528

@dr-jts
Copy link
Contributor

dr-jts commented Mar 9, 2020

Good that it is fixed. A unit test would be nice (which should be easy to provide in XML).

@pramsey
Copy link
Member

pramsey commented Mar 9, 2020

Tests are done and in PRs, will merge when CI gives me a thumbs up.

@pramsey
Copy link
Member

pramsey commented Mar 9, 2020

Tests are in 3.8 and master now, next patch release will have this fix.

@pramsey pramsey closed this as completed Mar 9, 2020
@pramsey
Copy link
Member

pramsey commented Mar 9, 2020

Link for reference https://trac.osgeo.org/geos/ticket/1018

@duvifn
Copy link
Author

duvifn commented Mar 10, 2020

@dr-jts @pramsey thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants