-
Notifications
You must be signed in to change notification settings - Fork 35
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 potential integer overflow at end of stream in media_foundation_helper #74
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #74 +/- ##
=======================================
Coverage 83.10% 83.10%
=======================================
Files 82 82
Lines 2024 2024
=======================================
Hits 1682 1682
Misses 342 342 ☔ View full report in Codecov by Sentry. |
Looks good to me. |
If you all are fine with the change, feel free to merge. We'd definitely appreciate a quick fix. 😁 |
Actually, are we sure that |
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.
lgtm!
@@ -86,7 +86,8 @@ class SyncronousByteStream : public IMFByteStream | |||
|
|||
try | |||
{ | |||
*read = static_cast<ULONG>( m_source.read( reinterpret_cast<char*>( buffer ), toRead ) ); | |||
const auto result = m_source.read( reinterpret_cast<char*>( buffer ), toRead ); | |||
*read = static_cast<ULONG>( result == -1 ? 0 : result ); |
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.
would it make sense to define -1 as "endOfSequence" for readability purposes?
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.
Probably better to avoid magic numbers indeed.
I was wondering about that as well. It might be a bit defensive but maybe we could also catch any negative and translate the
Alternatively, one could simply catch any "<0" cases with the following. S
|
Looks good to me too, if read() promises non-negative or -1 as return value then I think a check against a define with |
See fixup. So |
@@ -86,7 +86,8 @@ class SyncronousByteStream : public IMFByteStream | |||
|
|||
try | |||
{ | |||
*read = static_cast<ULONG>( m_source.read( reinterpret_cast<char*>( buffer ), toRead ) ); | |||
const auto result = m_source.read( reinterpret_cast<char*>( buffer ), toRead ); | |||
*read = static_cast<ULONG>( result == -1 ? 0 : result ); |
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.
Probably better to avoid magic numbers indeed.
If in SyncronousByteStream::Read, m_source.read() reaches the end of stream, boost::iostreams::file_descriptor returns -1 as an end of stream indicator. Previously, by casting the returned value to ULONG, an integer overflow would occur. To fix this, the end of stream indicator (-1) is caught and handled as "0 bytes read".
If in
SyncronousByteStream::Read
,m_source.read()
reaches the end of stream,boost::iostreams::file_descriptor
returns-1
as an end of stream indicator. Previously, by casting the returned value toULONG
, an integer overflow would occur.In the attached file, the observed behaviour then would be that the calling code in WMF would continue to attempt to read past the EOF indefinitely or until some unexpected state is reached.
To fix this, the end of stream indicator (
-1
) is caught and handled as "0 bytes read".See the attached TestMp3.zip for a mp3 which triggers this edge case.
See Issue #73