This is an automated email from the ASF dual-hosted git repository.

lostluck pushed a commit to branch swift-sdk
in repository https://gitbox.apache.org/repos/asf/beam.git

commit e155dc3e87d469d081f5d64dd85475e4630b0779
Author: Byron Ellis <[email protected]>
AuthorDate: Thu Aug 17 13:23:31 2023 -0700

    safeAdvance to try to address issues with zero-length data elements on 
non-mac platforms.
---
 .../ApacheBeam/Internal/Data+Decoding.swift        | 28 +++++++++++++++-------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/sdks/swift/Sources/ApacheBeam/Internal/Data+Decoding.swift 
b/sdks/swift/Sources/ApacheBeam/Internal/Data+Decoding.swift
index d8b3c7ef53a..978898fb51f 100644
--- a/sdks/swift/Sources/ApacheBeam/Internal/Data+Decoding.swift
+++ b/sdks/swift/Sources/ApacheBeam/Internal/Data+Decoding.swift
@@ -18,6 +18,23 @@
 import Foundation
 
 extension Data {
+    
+    /// advanced(by:) has issues on non-macOS Foundation implementations
+    @inlinable
+    func safeAdvance(by: Int) -> Data {
+        #if os(macOS)
+        return self.advanced(by: by)
+        #else
+        if(by == 0) {
+            return self
+        } else if(by >= self.count) {
+            return Data()
+        } else {
+            return self.advanced(by: by)
+        }
+        #endif
+    }
+    
     /// Read a variable length integer from the current data
     mutating func varint() throws -> Int {
         var advance: Int = 0
@@ -47,12 +64,7 @@ extension Data {
                 }
             }
         }
-        // On non-macOS platforms this crashes due to differences in 
Foundation implementations
-        #if os(macOS)
-        self = self.advanced(by: advance)
-        #else
-        self = advance == count ? Data() : self.advanced(by: advance)
-        #endif
+        self = self.safeAdvance(by: advance)
         return result
     }
     
@@ -60,7 +72,7 @@ extension Data {
     mutating func subdata() throws -> Data {
         let length = try self.varint()
         let result = self.subdata(in: 0..<length)
-        self = self.advanced(by: length)
+        self = self.safeAdvance(by: length)
         return result
     }
     
@@ -70,7 +82,7 @@ extension Data {
         let result = self.withUnsafeBytes {
             $0.baseAddress!.withMemoryRebound(to: T.self, capacity: 1) { 
$0.pointee }
         }
-        self = self.advanced(by: size)
+        self = self.safeAdvance(by: size)
         return result
     }
     

Reply via email to