Skip to content

Commit 1c24b0b

Browse files
committed
Fix possible integer overflow in memory allocation with negative precision in geohash_encode
1 parent e373186 commit 1c24b0b

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

geohash.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ char *php_geo_geohash_encode(double latitude, double longitude, int precision)
4848
interval_struct lng_interval = { MAX_LONG, MIN_LONG };
4949
interval_struct *interval;
5050

51+
if (precision < 0) {
52+
precision = 0;
53+
}
54+
5155
hash = (char*)safe_emalloc(precision, sizeof(char), 1);
5256

5357
hash[precision] = '\0';

tests/geohash_encode.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ echo geohash_encode(array('type' => 'Point', 'coordinates' => [95, 95]), 6).PHP_
1111
echo geohash_encode(array('type' => 'Point', 'coordinates' => [185, 185]), 12).PHP_EOL;
1212
echo geohash_encode(array('type' => 'Point', 'coordinates' => [-90, -185]), 12).PHP_EOL;
1313
echo var_dump(geohash_encode(array('type' => 'Point', 'coordinates' => [30, 30]), 0));
14+
echo var_dump(geohash_encode(array('type' => 'Point', 'coordinates' => [30, 30]), -1));
1415
?>
1516
--EXPECT--
1617
u2edjnw17enr
@@ -20,3 +21,4 @@ ypgxcz
2021
zzzzzzzzzzzz
2122
1bpbpbpbpbpb
2223
string(0) ""
24+
string(0) ""

0 commit comments

Comments
 (0)