Skip to content

Commit 763f624

Browse files
sakirrcursoragent
andcommitted
fix: validate negative distance in pgr_drivingDistance
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 4912a28 commit 763f624

2 files changed

Lines changed: 49 additions & 4 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/spanningTree/spanningTree_process.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ void pgr_process_spanningTree(
7979

8080
switch (val) {
8181
case 1:
82-
if (distance < 0) {
83-
pgr_throw_error("Negative value found on 'distance'", "Must be non negative");
82+
if (distance <= 0) {
83+
pgr_throw_error("Invalid value of 'distance'", "Valid values are greater than 0");
8484
}
8585
break;
8686
case 2:
@@ -102,8 +102,8 @@ void pgr_process_spanningTree(
102102
break;
103103

104104
case DIJKSTRADD:
105-
if (distance < 0) {
106-
pgr_throw_error("Negative value found on 'distance'", "Must be positive");
105+
if (distance <= 0) {
106+
pgr_throw_error("Invalid value of 'distance'", "Valid values are greater than 0");
107107
}
108108
break;
109109

0 commit comments

Comments
 (0)