From ebc47d9618b36c2f6c28a67fa52407ad8d72eb15 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 6 Oct 2015 16:53:30 +0100 Subject: [PATCH] Avoided possible arithmetic overflow in MemoryInputStream::read() for very large streams. --- .../juce_core/streams/juce_MemoryInputStream.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/juce_core/streams/juce_MemoryInputStream.cpp b/modules/juce_core/streams/juce_MemoryInputStream.cpp index da94088bc4..e8639dc8a1 100644 --- a/modules/juce_core/streams/juce_MemoryInputStream.cpp +++ b/modules/juce_core/streams/juce_MemoryInputStream.cpp @@ -67,13 +67,18 @@ int MemoryInputStream::read (void* const buffer, const int howMany) { jassert (buffer != nullptr && howMany >= 0); - const int num = jmin (howMany, (int) (dataSize - position)); - if (num <= 0) + if (howMany <= 0 || position >= dataSize) return 0; - memcpy (buffer, addBytesToPointer (data, position), (size_t) num); - position += (unsigned int) num; - return num; + const size_t num = jmin ((size_t) howMany, dataSize - position); + + if (num > 0) + { + memcpy (buffer, addBytesToPointer (data, position), num); + position += num; + } + + return (int) num; } bool MemoryInputStream::isExhausted()