From 27b663900dbd1974f6b22d0372ece1a12050a5da Mon Sep 17 00:00:00 2001 From: wro Date: Tue, 27 Feb 2024 12:00:34 +0100 Subject: [PATCH] Fix potential integer overflow at end of stream 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". --- .../src/ni/media/audio/os/win/media_foundation_helper.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/audiostream/src/ni/media/audio/os/win/media_foundation_helper.h b/audiostream/src/ni/media/audio/os/win/media_foundation_helper.h index 07e1f206..c84309ac 100644 --- a/audiostream/src/ni/media/audio/os/win/media_foundation_helper.h +++ b/audiostream/src/ni/media/audio/os/win/media_foundation_helper.h @@ -65,6 +65,8 @@ template class SyncronousByteStream : public IMFByteStream { + static constexpr auto endOfSequence = -1; + auto tell() -> std::streamsize { auto pos = m_source.seek( boost::iostreams::stream_offset( 0 ), BOOST_IOS::cur ); @@ -86,7 +88,8 @@ class SyncronousByteStream : public IMFByteStream try { - *read = static_cast( m_source.read( reinterpret_cast( buffer ), toRead ) ); + const auto result = m_source.read( reinterpret_cast( buffer ), toRead ); + *read = static_cast( result == endOfSequence ? 0 : result ); return S_OK; } catch ( const std::system_error& )