Skip to content

Commit 8a4c63d

Browse files
authored
Fix segfault in cellsToMultiPolygon due to reading invalid pointers (#129)
* Fix segfault in cellsToMultiPolygon due to reading invalid pointers * Add PR number
1 parent c8e738f commit 8a4c63d

File tree

4 files changed

+127
-9
lines changed

4 files changed

+127
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ file [H3Core.java](./src/main/java/com/uber/h3core/H3Core.java), and support
77
for the Linux x64 and Darwin x64 platforms.
88

99
## Unreleased Changes
10+
## Fixed
11+
- Fixed a potential segfault in `cellsToMultiPolygon` on error. (#129)
12+
1013
## [4.1.1] - 2023-02-03
1114
The changelog for this release is the same as v4.1.0. The release was run again due to an issue with the release process.
1215

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
<dependency>
169169
<groupId>junit</groupId>
170170
<artifactId>junit</artifactId>
171-
<version>4.13.1</version>
171+
<version>4.13.2</version>
172172
<scope>test</scope>
173173
</dependency>
174174
<dependency>

src/main/c/h3-java/src/jniapi.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -699,17 +699,18 @@ Java_com_uber_h3core_NativeMethods_cellsToLinkedMultiPolygon(JNIEnv *env,
699699
if (h3Elements != NULL) {
700700
H3Error err = cellsToLinkedMultiPolygon(h3Elements, numH3, &polygon);
701701

702-
// Parse the output now
703-
LinkedGeoPolygon *currentPolygon = &polygon;
702+
if (err) {
703+
(**env).ReleaseLongArrayElements(env, h3, h3Elements, 0);
704+
ThrowH3Exception(env, err);
705+
} else {
706+
// Parse the output now
707+
LinkedGeoPolygon *currentPolygon = &polygon;
704708

705-
ConvertLinkedGeoPolygonToManaged(env, currentPolygon, results);
709+
ConvertLinkedGeoPolygonToManaged(env, currentPolygon, results);
706710

707-
destroyLinkedMultiPolygon(&polygon);
711+
destroyLinkedMultiPolygon(&polygon);
708712

709-
(**env).ReleaseLongArrayElements(env, h3, h3Elements, 0);
710-
711-
if (err) {
712-
ThrowH3Exception(env, err);
713+
(**env).ReleaseLongArrayElements(env, h3, h3Elements, 0);
713714
}
714715
} else {
715716
ThrowOutOfMemoryError(env);

src/test/java/com/uber/h3core/TestRegion.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.google.common.collect.ImmutableList;
2222
import com.google.common.collect.ImmutableSet;
23+
import com.uber.h3core.exceptions.H3Exception;
2324
import com.uber.h3core.util.LatLng;
2425
import java.util.ArrayList;
2526
import java.util.List;
@@ -270,4 +271,117 @@ public void testH3SetToMultiPolygonLarge() {
270271
assertEquals(6, multiBounds.get(i).get(0).size());
271272
}
272273
}
274+
275+
@Test(expected = H3Exception.class)
276+
public void testH3SetToMultiPolygonIssue753() {
277+
// Test case reported in https://github.com/uber/h3/issues/753
278+
// Ensure it does not crash
279+
List<Long> cells =
280+
ImmutableList.of(
281+
617683643010646015L,
282+
617683648070287359L,
283+
617683642951663615L,
284+
617683648070287359L,
285+
617683648070549503L,
286+
617683643014840319L,
287+
617683643013791743L,
288+
617683642951663615L,
289+
617683642951663615L,
290+
617683648065044479L,
291+
617683648070549503L,
292+
617683643010383871L,
293+
617683643010646015L,
294+
617683643013791743L,
295+
617683643008024575L,
296+
617683643014840319L,
297+
617683643010383871L,
298+
617683642941177855L,
299+
617683642941177855L,
300+
617683642941439999L,
301+
617683642951663615L,
302+
617683642950615039L,
303+
617683642950877183L,
304+
617683648065044479L,
305+
617683648070549503L,
306+
617683648057180159L,
307+
617683648065044479L,
308+
617683648064520191L,
309+
617683643013791743L,
310+
617683643014316031L,
311+
617683643008548863L,
312+
617683643007238143L,
313+
617683643008024575L,
314+
617683643010383871L,
315+
617683642953498623L,
316+
617683642941177855L,
317+
617683643014316031L,
318+
617683642953760767L,
319+
617683642941439999L,
320+
617683642951139327L,
321+
617683642950615039L,
322+
617683642951663615L,
323+
617683648065044479L,
324+
617683642950877183L,
325+
617683648064258047L,
326+
617683648063471615L,
327+
617683648066093055L,
328+
617683648057180159L,
329+
617683648064520191L,
330+
617683648057180159L,
331+
617683648070549503L,
332+
617683648066093055L,
333+
617683643019296767L,
334+
617683643008548863L,
335+
617683643010383871L,
336+
617683643018510335L,
337+
617683643017723903L,
338+
617683642953760767L,
339+
617683642954285055L,
340+
617683642953236479L,
341+
617683642953498623L,
342+
617683642953498623L,
343+
617683642940129279L,
344+
617683642941177855L,
345+
617683642950877183L,
346+
617683644212576255L,
347+
617683644211003391L,
348+
617683644212314111L,
349+
617683648064258047L,
350+
617683648057180159L,
351+
617683648057442303L,
352+
617683648068190207L,
353+
617683648057180159L,
354+
617683648064520191L,
355+
617683648064782335L,
356+
617683648060850175L,
357+
617683648057442303L,
358+
617683648070811647L,
359+
617683648071335935L,
360+
617683648070287359L,
361+
617683648070549503L,
362+
617683648131366911L,
363+
617683648070811647L,
364+
617683648066093055L,
365+
617683648067141631L,
366+
617683642944585727L,
367+
617683642947469311L,
368+
617683642940915711L,
369+
617683642940129279L,
370+
617683642953498623L,
371+
617683642945372159L,
372+
617683642944323583L,
373+
617683642944585727L,
374+
617683648065568767L,
375+
617683648066093055L,
376+
617683648065830911L,
377+
617683648065830911L,
378+
617683648068190207L,
379+
617683648067665919L,
380+
617683648028344319L,
381+
617683648065830911L,
382+
617683648067665919L,
383+
617683648068976639L,
384+
617683648028606463L);
385+
h3.cellsToMultiPolygon(cells, true);
386+
}
273387
}

0 commit comments

Comments
 (0)