From d6c1f18c848649256bc9d5183b823de8cff0b492 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 17 Nov 2023 12:18:44 +0100 Subject: [PATCH 1/6] Add assertion to detect overflow --- src/structures/typedefs.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structures/typedefs.h b/src/structures/typedefs.h index 79ef1ec15..265787478 100644 --- a/src/structures/typedefs.h +++ b/src/structures/typedefs.h @@ -195,6 +195,7 @@ inline Duration scale_from_user_duration(UserDuration d) { } inline UserDuration scale_to_user_duration(Duration d) { + assert(d <= DURATION_FACTOR * static_cast(std::numeric_limits::max())); return static_cast(d / DURATION_FACTOR); } From 957dc62dec6ca99f6b8c99ecdf43e2c1883dc231 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 17 Nov 2023 12:23:33 +0100 Subject: [PATCH 2/6] Refactor code --- src/structures/typedefs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/typedefs.h b/src/structures/typedefs.h index 265787478..027a3673f 100644 --- a/src/structures/typedefs.h +++ b/src/structures/typedefs.h @@ -195,7 +195,7 @@ inline Duration scale_from_user_duration(UserDuration d) { } inline UserDuration scale_to_user_duration(Duration d) { - assert(d <= DURATION_FACTOR * static_cast(std::numeric_limits::max())); + assert(d <= scale_from_user_duration(std::numeric_limits::max())); return static_cast(d / DURATION_FACTOR); } From 78e14ff1ec95943f6d27f5474f94312558d204df Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 17 Nov 2023 12:40:30 +0100 Subject: [PATCH 3/6] Make scaling functions constexpr --- src/structures/typedefs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/typedefs.h b/src/structures/typedefs.h index 027a3673f..c2930d66e 100644 --- a/src/structures/typedefs.h +++ b/src/structures/typedefs.h @@ -190,11 +190,11 @@ struct StringHash { }; namespace utils { -inline Duration scale_from_user_duration(UserDuration d) { +constexpr inline Duration scale_from_user_duration(UserDuration d) { return DURATION_FACTOR * static_cast(d); } -inline UserDuration scale_to_user_duration(Duration d) { +constexpr inline UserDuration scale_to_user_duration(Duration d) { assert(d <= scale_from_user_duration(std::numeric_limits::max())); return static_cast(d / DURATION_FACTOR); } From ce715bcf4902980bc13efebbe178d43ef6cd047a Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 17 Nov 2023 12:41:18 +0100 Subject: [PATCH 4/6] Fix values of default time window end and length --- src/structures/vroom/time_window.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/vroom/time_window.cpp b/src/structures/vroom/time_window.cpp index 69832f78a..a223dd8b7 100644 --- a/src/structures/vroom/time_window.cpp +++ b/src/structures/vroom/time_window.cpp @@ -13,10 +13,10 @@ All rights reserved (see LICENSE). namespace vroom { constexpr Duration TimeWindow::default_length = - std::numeric_limits::max(); + utils::scale_from_user_duration(std::numeric_limits::max()); TimeWindow::TimeWindow() - : start(0), end(std::numeric_limits::max()), length(end - start) { + : start(0), end(utils::scale_from_user_duration(std::numeric_limits::max())), length(end - start) { } TimeWindow::TimeWindow(UserDuration start, UserDuration end) From 63db4f20f449e5dfd8764796dce3c2096d041064 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 17 Nov 2023 12:44:34 +0100 Subject: [PATCH 5/6] Apply code formatting --- src/structures/typedefs.h | 3 ++- src/structures/vroom/time_window.cpp | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/structures/typedefs.h b/src/structures/typedefs.h index c2930d66e..f9bbfa0ba 100644 --- a/src/structures/typedefs.h +++ b/src/structures/typedefs.h @@ -195,7 +195,8 @@ constexpr inline Duration scale_from_user_duration(UserDuration d) { } constexpr inline UserDuration scale_to_user_duration(Duration d) { - assert(d <= scale_from_user_duration(std::numeric_limits::max())); + assert(d <= + scale_from_user_duration(std::numeric_limits::max())); return static_cast(d / DURATION_FACTOR); } diff --git a/src/structures/vroom/time_window.cpp b/src/structures/vroom/time_window.cpp index a223dd8b7..487526c68 100644 --- a/src/structures/vroom/time_window.cpp +++ b/src/structures/vroom/time_window.cpp @@ -16,7 +16,10 @@ constexpr Duration TimeWindow::default_length = utils::scale_from_user_duration(std::numeric_limits::max()); TimeWindow::TimeWindow() - : start(0), end(utils::scale_from_user_duration(std::numeric_limits::max())), length(end - start) { + : start(0), + end(utils::scale_from_user_duration( + std::numeric_limits::max())), + length(end - start) { } TimeWindow::TimeWindow(UserDuration start, UserDuration end) From b545622b73c20e1d0ef9f21082d633feea352d85 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 17 Nov 2023 12:47:57 +0100 Subject: [PATCH 6/6] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e8671045..8ae7223e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,7 @@ - `max_travel_time` not accounted for with vehicle steps in solving mode (#954) - `max_travel_time` not accounted for in `RouteSplit` (#941) - Wrong capacity checks in `RouteSplit` (#981) +- Overflow related to scaling default time windows (#1020) #### Internals