Skip to content

Commit 6bc52c3

Browse files
[GH-2036] update more robust instream count for Geography decode (#2305)
* update to more robust check instream count * empty commit to retrigger ci
1 parent 856e145 commit 6bc52c3

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

common/src/main/java/org/apache/sedona/common/S2Geography/GeographyCollection.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,25 @@ public static GeographyCollection decode(UnsafeInput in, EncodeTag tag) throws I
135135
// Skip any covering data
136136
tag.skipCovering(in);
137137

138-
// 3) Ensure we have at least 4 bytes for the count
139-
if (in.available() < Integer.BYTES) {
140-
throw new IOException("GeographyCollection.decodeTagged error: insufficient header bytes");
141-
}
138+
try {
139+
int count = in.readInt();
140+
if (count < 0) {
141+
throw new IOException("GeographyCollection.decodeTagged error: negative count: " + count);
142+
}
143+
144+
for (int i = 0; i < count; i++) {
145+
tag = EncodeTag.decode(in);
146+
// avoid creating new stream, directly call S2Geography.decode
147+
Geography feature = Geography.decode(in, tag);
148+
geo.features.add(feature);
149+
}
150+
geo.countShapes();
142151

143-
// Read feature count
144-
int n = in.readInt();
145-
for (int i = 0; i < n; i++) {
146-
tag = EncodeTag.decode(in);
147-
// avoid creating new stream, directly call S2Geography.decode
148-
Geography feature = Geography.decode(in, tag);
149-
geo.features.add(feature);
152+
} catch (EOFException e) {
153+
throw new IOException(
154+
"GeographyCollection.decodeTagged error: insufficient data to decode all parts of the geography.",
155+
e);
150156
}
151-
geo.countShapes();
152157
return geo;
153158
}
154159

common/src/main/java/org/apache/sedona/common/S2Geography/PolylineGeography.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,26 @@ public static PolylineGeography decode(UnsafeInput in, EncodeTag tag) throws IOE
138138
// 2) Skip past any covering cell-IDs written by encodeTagged
139139
tag.skipCovering(in);
140140

141-
// 3) Ensure we have at least 4 bytes for the count
142-
if (in.available() < Integer.BYTES) {
143-
throw new IOException("PolylineGeography.decodeTagged error: insufficient header bytes");
144-
}
145-
146-
// 5) Read the number of polylines (4-byte)
147-
int count = in.readInt();
141+
// 3) Read the number of polylines (4-byte)
142+
try {
143+
int count = in.readInt();
144+
if (count < 0) {
145+
throw new IOException("PolylineGeography.decodeTagged error: negative count: " + count);
146+
}
147+
if (tag.getKind() == GeographyKind.SINGLEPOLYLINE) {
148+
return new SinglePolylineGeography(S2Polyline.decode(in));
149+
}
148150

149-
if (tag.getKind() == GeographyKind.SINGLEPOLYLINE) {
150-
return new SinglePolylineGeography(S2Polyline.decode(in));
151-
}
151+
// 4) For each polyline, read its block and let S2Polyline.decode(InputStream) do the rest
152+
for (int i = 0; i < count; i++) {
153+
S2Polyline pl = S2Polyline.decode(in);
154+
geo.polylines.add(pl);
155+
}
152156

153-
// 6) For each polyline, read its block and let S2Polyline.decode(InputStream) do the rest
154-
for (int i = 0; i < count; i++) {
155-
S2Polyline pl = S2Polyline.decode(in);
156-
geo.polylines.add(pl);
157+
} catch (EOFException e) {
158+
throw new IOException(
159+
"PolylineGeography.decodeTagged error: insufficient data to decode all parts of the geography.",
160+
e);
157161
}
158162
return geo;
159163
}

0 commit comments

Comments
 (0)