-
Notifications
You must be signed in to change notification settings - Fork 51
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
MediaTime: fix negative value into Duration bug #400
MediaTime: fix negative value into Duration bug #400
Conversation
Actually, I'm not sure that |
I don't feel strongly one way or the other. Have you experienced a bug due to this conversion? |
Yes. I had two I will prepare a proposed |
Sounds good! |
3aaf33b
to
1495560
Compare
@algesten I've revised the patch. While I was there, I also fixed some defects regarding handling of negative and zero denominators. Those cases are now statically disallowed. This should eliminate a possible DoS vulnerability resulting from SDP parsing of streams with 0 frequency resulting in division by zero panics. You can, of course, cherry-pick the commit(s) you want or ask me to prepare a reduced changeset but I would very much like this basic data type to be as safe as possible. |
Thanks! I'm in two minds about this For now we could potentially keep it internal and decide later if we want to have it public. |
56bc26c
to
0659282
Compare
Unfortunately, it is indeed rather heavyweight compared to just plugging in literals but I have tried to make it as straightforward as possible to use with frequency-specific constructors and constants. As far as I can see, the design space for this division by zero restriction is:
Perhaps you'd be happier with 4? I have added |
Another option would be to do both the The |
@david-flok I'm coming around to this being a newtype. Having it in |
@xnorpx you had thoughts on MediaTime (passing it to My idea for API ergonomics here is that
Since you need to know the negotiated |
It feels a little clunky to need to create a MediaTime everytime I call write. Pass in Pt and RtpTimestamp and MediaTime could be created inside. But it could just be the way I use it. We probably need to add 8000 and 16000 at some point to the frequency struct, but I like having the new type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let's land this. I want some documentation is all.
@@ -1,36 +1,127 @@ | |||
#![allow(missing_docs)] | |||
|
|||
use std::cmp::Ordering; | |||
use std::ops::{Add, Sub}; | |||
use std::fmt::Display; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not your problem, really, but it's expanding with the increased amount of code in this file.
Could you remove the #![allow(missing_docs)]
at the top and document the new and old code?
Also, In the Frequency
doc on struct
, and MediaTime
struct, I'd like to see some code examples how to get the Frequency
for a certain Format
from the chain Rtc -> CodecConfig -> PayloadParam -> CodecSpec -> Format -> Frequency
and how to subsequenty use that to construct a MediaTime
. You can duplicate the code example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you have in mind for this example so I've not guessed, sorry.
@david-flok also, note #338 which I want to land before this, which will clean up in |
Hi @david-flok, I know I've created some more work in this PR. Would you prefer if I took the PR to completion, or do you want to finish it? |
These each indicate a defect of some severity.
Hi @algesten, no worries. I'll polish this now, sorry for the delay. |
This enforces the signedness invariant in the type so that negative denominators are unrepresentable. This saves complexity in managing improper ratios with cancelling signs.
MediaTime supports negative durations but Duration does not. Expose the partiality of the conversion in the signature.
Ensure that we can never have a delayed divide by zero panic due to carrying a zero denominator around. This removes a denial of service vulnerability from SDP parsing and makes the MediaTime type safer for API users. The change from 64-bit to 32-bit is justified by the observation that it is unlikely applications will need to represent times with precision smaller than quarters of nanoseconds.
c09474e
to
342a384
Compare
Ok, I've rebased and resolved the conflicts with #338. I also did some documentation but I haven't provided the example(s) you wanted, sorry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's ship it!
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my private user
@david-flok I added some code example: a319da9 |
as u64
ofi64
is dangerous.Duration
is non-negative butMediaTime
is not necessarily. This fixes what is effectively an integer underflow.