Bug 144908 - [GTK] Avoid std::chrono::microseconds overflows in setSharedTimerFireInterval
Summary: [GTK] Avoid std::chrono::microseconds overflows in setSharedTimerFireInterval
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Zan Dobersek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-05-12 06:14 PDT by Zan Dobersek
Modified: 2015-12-07 10:01 PST (History)
6 users (show)

See Also:


Attachments
Patch (2.67 KB, patch)
2015-05-12 06:31 PDT, Zan Dobersek
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Zan Dobersek 2015-05-12 06:14:25 PDT
[GTK] Avoid std::chrono::microseconds overflows in setSharedTimerFireInterval
Comment 1 Zan Dobersek 2015-05-12 06:31:25 PDT
Created attachment 252960 [details]
Patch
Comment 2 Carlos Garcia Campos 2015-05-12 06:44:50 PDT
Comment on attachment 252960 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=252960&action=review

> Source/WebCore/platform/gtk/SharedTimerGtk.cpp:53
> +    // Passed-in value is in seconds, which might overflow when casting to std::chrono::microseconds.
> +    auto interval = std::chrono::duration<double>(intervalValue);
> +    auto delay = std::chrono::microseconds::max();
> +    if (interval < delay)
> +        delay = std::chrono::duration_cast<std::chrono::microseconds>(interval);

Could we do something like this in GMainLoopSource instead? To fix any other cases where we are passing a double casted to microseconds.
Comment 3 Zan Dobersek 2015-05-12 06:59:22 PDT
Comment on attachment 252960 [details]
Patch

Needs a bit more thought.
Comment 4 Darin Adler 2015-05-17 10:26:16 PDT
Comment on attachment 252960 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=252960&action=review

> Source/WebCore/platform/gtk/SharedTimerGtk.cpp:49
> +    // Passed-in value is in seconds, which might overflow when casting to std::chrono::microseconds.

This seems like an issue that might happen cross-platform as we move from the traditional “doubles in seconds” to std::chrono across the board. I wonder what the good idiom to avoid this is.
Comment 5 Zan Dobersek 2015-12-07 02:18:44 PST
A version of this patch landed in r192058.
https://trac.webkit.org/changeset/192058
Comment 6 Zan Dobersek 2015-12-07 02:20:47 PST
(In reply to comment #4)
> Comment on attachment 252960 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=252960&action=review
> 
> > Source/WebCore/platform/gtk/SharedTimerGtk.cpp:49
> > +    // Passed-in value is in seconds, which might overflow when casting to std::chrono::microseconds.
> 
> This seems like an issue that might happen cross-platform as we move from
> the traditional “doubles in seconds” to std::chrono across the board. I
> wonder what the good idiom to avoid this is.

If possible, we could try specializing the std::chrono::duration<> template for the WTF::Checked class.
Comment 7 Darin Adler 2015-12-07 10:01:35 PST
For most uses we might need clamping casts that turn out-of-range values into min/max.