Skip to content

Commit

Permalink
Switch to iterative version of WKT format parser
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <heemin@amazon.com>
  • Loading branch information
heemin32 committed Jun 8, 2024
1 parent fbe048f commit 1ef63ad
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix double invocation of postCollection when MultiBucketCollector is present ([#14015](https://github.com/opensearch-project/OpenSearch/pull/14015))

### Security
- Switch to iterative version of WKT format parser ([#14086](https://github.com/opensearch-project/OpenSearch/pull/14086))

[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.13...2.x
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Stack;

/**
* Utility class for converting to and from WKT
Expand Down Expand Up @@ -301,16 +302,95 @@ private Geometry parseGeometry(StreamTokenizer stream) throws IOException, Parse
throw new IllegalArgumentException("Unknown geometry type: " + type);
}

/**
* Iterative version of
* <!--
* ```java
* private GeometryCollection<Geometry> parseGeometryCollectionA(StreamTokenizer stream) throws IOException, ParseException {
* if (nextEmptyOrOpen(stream).equals(EMPTY)) {
* return GeometryCollection.EMPTY;
* }
* List<Geometry> shapes = new ArrayList<>();
* shapes.add(parseGeometry(stream));
* while (nextCloserOrComma(stream).equals(COMMA)) {
* shapes.add(parseGeometry(stream));
* }
* return new GeometryCollection<>(shapes);
* }
* -->
* to avoid StackOverflowError when there is a deeply nested structure of GeometryCollection.
*/
private GeometryCollection<Geometry> parseGeometryCollection(StreamTokenizer stream) throws IOException, ParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return GeometryCollection.EMPTY;
}
List<Geometry> shapes = new ArrayList<>();
shapes.add(parseGeometry(stream));
while (nextCloserOrComma(stream).equals(COMMA)) {
shapes.add(parseGeometry(stream));

List<Geometry> topLevelShapes = new ArrayList<>();
Stack<List<Geometry>> stack = new Stack<>();
stack.push(topLevelShapes);
boolean isFirstIteration = true;
List<Geometry> currentLevelShapes = null;
while (!stack.isEmpty()) {
List<Geometry> previousShapes = stack.pop();
if (currentLevelShapes != null) {
previousShapes.add(new GeometryCollection<>(currentLevelShapes));
}
currentLevelShapes = previousShapes;

if (isFirstIteration == true) {
isFirstIteration = false;
} else {
if (!nextCloserOrComma(stream).equals(COMMA)) {
continue;
}
}
while (true) {
final String type = nextWord(stream).toLowerCase(Locale.ROOT);
switch (type) {
case "point":
currentLevelShapes.add(parsePoint(stream));
break;
case "multipoint":
currentLevelShapes.add(parseMultiPoint(stream));
break;
case "linestring":
currentLevelShapes.add(parseLine(stream));
break;
case "multilinestring":
currentLevelShapes.add(parseMultiLine(stream));
break;
case "polygon":
currentLevelShapes.add(parsePolygon(stream));
break;
case "multipolygon":
currentLevelShapes.add(parseMultiPolygon(stream));
break;
case "bbox":
currentLevelShapes.add(parseBBox(stream));
break;
case "geometrycollection":
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
currentLevelShapes.add(GeometryCollection.EMPTY);
break;

Check warning on line 374 in libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java

View check run for this annotation

Codecov / codecov/patch

libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java#L373-L374

Added lines #L373 - L374 were not covered by tests
} else {
stack.push(currentLevelShapes);
currentLevelShapes = new ArrayList<>();
continue;
}
case "circle": // Not part of the standard, but we need it for internal serialization
currentLevelShapes.add(parseCircle(stream));
break;
default:
throw new IllegalArgumentException("Unknown geometry type: " + type);

Check warning on line 384 in libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java

View check run for this annotation

Codecov / codecov/patch

libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java#L384

Added line #L384 was not covered by tests
}

if (!nextCloserOrComma(stream).equals(COMMA)) {
break;
}
}
}
return new GeometryCollection<>(shapes);

return new GeometryCollection<>(topLevelShapes);
}

private Point parsePoint(StreamTokenizer stream) throws IOException, ParseException {
Expand Down

0 comments on commit 1ef63ad

Please sign in to comment.