-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Time.kt uses Duration.toMillis in unsafe manner #868
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
Comments
I simple workaround is to avoid time check at all if the duration exceed a large number. So if the duration is greater than a century (example) then the code is executed without any timeout (this avoid some system calls and related work). |
The library should not do that. There are a lot of valid use-cases such as calling
|
@qwwdfsad Do you consider coerce at most 292 million years each |
Why not just use Long instead of this custom Duration? It breaks from migration to common code in the future. |
|
Yes, but I didn't meet developer who doesn't know how to make minutes, hours, etc with Long. |
Duration is actually the standard and long is the legacy mechanism. |
|
Obviously. But I tried to migrate some java only code to common from ktor websockets (it has only java part now). One of the blockers was java.time package. That's why I think about migration from platform-specific to universal code in every project |
Using
Duration.ofSeconds(Long.MAX_VALUE)
as the timeout argument forkotlinx.coroutines.withTimeout
will cause an ArithmaticOverflowExceptionThis is because, in pure java
Durations.ofSeconds(Long.MAX_VALUE).toMillis()
will cause an Arithmatic overflow.I've got a fix that basically involves adding an extension function
toMillisOrMax
that returnsLong.MAX_VALUE
whentoMillis
would otherwise overflow.Use case:
I'm a big fan of null objects, and the way I've structured things is such that the "default" timeout is
Duration.of(seconds = Long.MAX_VALUE, nanos = 999__999_999)
(which is effectivelyDuration.MAX_VALUE
), such that our down-stack code need not consider the case where no timeout is specified.If you guys are interested in this fix I'll submit a pull request to that involves simply:
as a function in
TimeKt
, invoked instead ofDuration.toMillis()
The text was updated successfully, but these errors were encountered: