Skip to content

Commit 81c16b4

Browse files
committed
Fixed issue #16: Segfault on rdp_simplify for GeoJSON
1 parent 821928e commit 81c16b4

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

geospatial.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "ext/standard/info.h"
2929
#include "php_geospatial.h"
3030
#include "geo_array.h"
31+
#include "Zend/zend_exceptions.h"
32+
#include "ext/spl/spl_exceptions.h"
3133

3234
ZEND_BEGIN_ARG_INFO_EX(haversine_args, 0, 0, 2)
3335
ZEND_ARG_INFO(0, geoJsonPointFrom)
@@ -225,6 +227,11 @@ static int parse_point_pair(zval *coordinates, double *lon, double *lat TSRMLS_D
225227
zval **z_lon, **z_lat;
226228
#endif
227229

230+
if (Z_TYPE_P(coordinates) != IS_ARRAY) {
231+
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "Expected a coordinate pair as an array, but %s given", zend_zval_type_name(coordinates));
232+
return 0;
233+
}
234+
228235
coords = HASH_OF(coordinates);
229236
if (coords->nNumOfElements != 2) {
230237
return 0;
@@ -921,6 +928,9 @@ PHP_FUNCTION(rdp_simplify)
921928
array_init(return_value);
922929

923930
points = geo_hashtable_to_array(points_array TSRMLS_CC);
931+
if (!points) {
932+
return;
933+
}
924934
rdp_simplify(points, epsilon, 0, points->count - 1);
925935
for (i = 0; i < points->count; i++) {
926936
if (points->status[i]) {

tests/bug0016.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Test for issue #16: Segfault on rdp_simplify for GeoJSON
3+
--FILE--
4+
<?php
5+
$geojson = json_decode('{"type":"LineString","coordinates":[[0,0],[1,0],[2,0],[2,1],[2,2],[1,2],[0,2],[0,1],[0,0]]}', true);
6+
7+
try {
8+
var_dump(rdp_simplify($geojson, 1));
9+
} catch ( InvalidArgumentException $e ) {
10+
echo $e->getMessage(), "\n";
11+
}
12+
?>
13+
--EXPECT--
14+
Expected a coordinate pair as an array, but string given

0 commit comments

Comments
 (0)