Skip to content

Commit e17c89c

Browse files
committed
fix: UBs
1 parent 85a3b84 commit e17c89c

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

include/boost/graph/relax.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ template < class T > struct closed_plus
2929

3030
T operator()(const T& a, const T& b) const
3131
{
32-
using namespace std;
3332
if (a == inf)
3433
return inf;
3534
if (b == inf)
3635
return inf;
36+
// saturate to avoid signed overflow (e.g. negative cycles)
37+
if (b > 0 && a > (std::numeric_limits< T >::max)() - b)
38+
return inf;
39+
if (b < 0 && a < (std::numeric_limits< T >::lowest)() - b)
40+
return (std::numeric_limits< T >::lowest)();
3741
return a + b;
3842
}
3943
};

test/bundled_properties.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ struct Highway
7676
}
7777

7878
string name;
79-
double miles;
80-
int speed_limit;
81-
int lanes;
82-
bool divided;
79+
double miles = 0;
80+
int speed_limit = 65;
81+
int lanes = 4;
82+
bool divided = true;
8383
};
8484

8585
std::ostream& operator<<(std::ostream& out, const Highway& highway)

0 commit comments

Comments
 (0)