Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined behavior related to date parsing #3278

Merged
merged 6 commits into from
Sep 15, 2021
Merged

Conversation

SiarheiFedartsou
Copy link
Member

@SiarheiFedartsou SiarheiFedartsou commented Aug 19, 2021

Issue

We found this issue using undefined behavior sanitizer:

/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/chrono:166:38: runtime error: signed integer overflow: 18014398509480175 * 1000000000 cannot be represented in type 'long'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/chrono:166:38 in

image

Why it happened in our case:

  1. offset_date is called with in_dt == "current".
  2. DateTime::seconds_since_epoch is called with date_time == "current".
  3. We get here and return smth like UINT_MAX. That's why we run into UB during std::chrono::... related operations.

I'm absolutely not confident if it should be fixed this way, so want to hear opinion of @kevinkreiser first before covering it with tests etc.

Also related question: I see that Valhalla uses time zone information. Which features actually require it? Why I am asking, I see potential problem here in our use case: we use Valhalla on mobile phones and can download tiles generated with newer Valhalla version than we currently have on-board. Newer Valhalla can potentially have updated time zone database, i.e. NodeInfo::timezone can be wrong/out-of-bounds for our local time zone database. Is there easy way to simply turn off features which use this timezone information for our use case? Or probably easy way to bake this time zone info into tiles?

Tasklist

  • Add tests
  • Add #fixes with the issue number that this PR addresses
  • Update the docs with any new request parameters or changes to behavior described
  • Update the changelog
  • If you made changes to the lua files, update the taginfo too.

Requirements / Relations

Link any requirements here. Other pull requests this PR is based on?

@kevinkreiser
Copy link
Member

in_dt should have been converted to an actual time before this was called most likely. the algorithms should make a call to time_info::make, which takes the pbf location as a reference and then modifies the date_time string to be the correct date time based on the current time in the current timezone:

https://github.com/valhalla/valhalla/blob/master/src/thor/bidirectional_astar.cc#L512-L513
https://github.com/valhalla/valhalla/blob/master/valhalla/baldr/time_info.h#L129-L133

can you check why that isnt happening in your case?

@@ -455,7 +456,8 @@ void thor_worker_t::path_arrive_by(Api& api, const std::string& costing) {

for (auto& temp_path : temp_paths) {
// back propagate time information
if (destination->has_date_time() && options.date_time_type() != valhalla::Options::invariant) {
if (destination->has_date_time() && options.date_time_type() != valhalla::Options::invariant &&
options.date_time_type() != valhalla::Options::current) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mentioned this in another comment but the proper fix is to figure out why your case didnt change the date_time string from current into a proper date_time string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kevinkreiser! I have a proper fix for that, but do not have rights to push to this repository

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oleg-liatte you've been invited

@oleg-liatte
Copy link
Member

As @SiarheiFedartsou said, UB was caused by date_time string conversion failure: default zero time was adjusted by time zone to negative side, causing this way unsigned overflow. The problem was that TimeInfo::make patched date_time of local copy of valhalla::Location, and original Location left intact.

Turned creating a local copies into a references.

@oleg-liatte oleg-liatte marked this pull request as ready for review September 15, 2021 08:51
@kevinkreiser
Copy link
Member

Yep this is a result of a recent refractor. Sadly it was very hard to catch this mistake during review: https://github.com/valhalla/valhalla/pull/2987/files#diff-93d0749f9c1f6cd0bc29f8b7db15520349e887fc8aa7bbcbe26475b14fcc9b88R434

@oleg-liatte oleg-liatte merged commit 7be4a7b into master Sep 15, 2021
@oleg-liatte oleg-liatte deleted the sf-fix-date-time-ub branch September 15, 2021 14:14
@danpat
Copy link
Member

danpat commented Sep 15, 2021

:tableflip: that's it, I'm never trying to fix anything ever again. Goddamn output parameters....

@kevinkreiser
Copy link
Member

@danpat sadly its just a fact of complicated life/code, dont beat yourself up about it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants