Skip to content

Commit 3cef1b1

Browse files
author
sakirr
committed
fix: validate negative distance in pgr_drivingDistance
1 parent 28c581a commit 3cef1b1

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* :file: This file is part of the pgRouting project.
2+
:copyright: Copyright (c) 2025-2026 pgRouting developers
3+
:license: Creative Commons Attribution-Share Alike 3.0 https://creativecommons.org/licenses/by-sa/3.0 */
4+
5+
BEGIN;
6+
7+
UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
8+
SELECT CASE WHEN min_version('3.6.0') THEN plan(3) ELSE plan(1) END;
9+
10+
CREATE OR REPLACE FUNCTION test_function()
11+
RETURNS SETOF TEXT AS
12+
$BODY$
13+
BEGIN
14+
IF min_version('3.6.0') THEN
15+
-- Test negative distance throws error
16+
SELECT throws_ok(
17+
$$SELECT * FROM pgr_drivingDistance('SELECT id, source, target, cost FROM edges', 1, -1.0)$$,
18+
'XX000',
19+
'Invalid value of ''distance''',
20+
'SHOULD THROW because distance is negative'
21+
);
22+
23+
-- Test zero distance throws error
24+
SELECT throws_ok(
25+
$$SELECT * FROM pgr_drivingDistance('SELECT id, source, target, cost FROM edges', 1, 0.0)$$,
26+
'XX000',
27+
'Invalid value of ''distance''',
28+
'SHOULD THROW because distance is zero'
29+
);
30+
31+
-- Test positive distance works (regression test)
32+
SELECT lives_ok(
33+
$$SELECT * FROM pgr_drivingDistance('SELECT id, source, target, cost FROM edges', 1, 1.0)$$,
34+
'SHOULD NOT THROW because distance is positive'
35+
);
36+
ELSE
37+
SELECT skip(1, 'Function standardized on 3.6.0');
38+
END IF;
39+
END;
40+
$BODY$
41+
LANGUAGE plpgsql;
42+
43+
SELECT * FROM test_function();
44+
SELECT * FROM finish();
45+
ROLLBACK;

src/driving_distance/driving_distance.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ void process(
4949
bool equicost,
5050
MST_rt **result_tuples,
5151
size_t *result_count) {
52+
if (distance <= 0) {
53+
pgr_throw_error("Invalid value of 'distance'", "Valid values are greater than 0");
54+
}
55+
5256
pgr_SPI_connect();
5357
char* log_msg = NULL;
5458
char* notice_msg = NULL;

0 commit comments

Comments
 (0)