From 5cf2802a00cec2a860a0b5bca3dda35420a709fa Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 9 Mar 2020 09:45:31 +0000 Subject: [PATCH] macOS: Fixed a data race in URLConnectionState --- modules/juce_core/native/juce_mac_Network.mm | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/modules/juce_core/native/juce_mac_Network.mm b/modules/juce_core/native/juce_mac_Network.mm index d86baf45b6..2512a23663 100644 --- a/modules/juce_core/native/juce_mac_Network.mm +++ b/modules/juce_core/native/juce_mac_Network.mm @@ -188,15 +188,21 @@ public: int read (char* dest, int numBytes) { - int numDone = 0; + int numDone = 0, dataLength = 0; + + { + const ScopedLock sl (dataLock); + dataLength = (int) [data length]; + } while (numBytes > 0) { - const int available = jmin (numBytes, (int) [data length]); + auto available = jmin (numBytes, dataLength); if (available > 0) { const ScopedLock sl (dataLock); + [data getBytes: dest length: (NSUInteger) available]; [data replaceBytesInRange: NSMakeRange (0, (NSUInteger) available) withBytes: nil length: 0]; @@ -743,15 +749,21 @@ public: int read (char* dest, int numBytes) { - int numDone = 0; + int numDone = 0, dataLength = 0; + + { + const ScopedLock sl (dataLock); + dataLength = (int) [data length]; + } while (numBytes > 0) { - const int available = jmin (numBytes, (int) [data length]); + auto available = jmin (numBytes, dataLength); if (available > 0) { const ScopedLock sl (dataLock); + [data getBytes: dest length: (NSUInteger) available]; [data replaceBytesInRange: NSMakeRange (0, (NSUInteger) available) withBytes: nil length: 0];