Skip to content

Commit

Permalink
Merge pull request #8 from metaplex-foundation/fix/deserialization
Browse files Browse the repository at this point in the history
Fix crashes related to deserialization
  • Loading branch information
ajamaica authored Oct 25, 2022
2 parents 315ce19 + 44398f9 commit 05657fa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Sources/Beet/Beets/Composites.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class coptionNone: ScalarFixedSizeBeet {
}

public func read<T>(buf: Data, offset: Int) -> T {
debugPrint("read \(description): \(NONE)")
return Optional<Any>.none as! T
}
}
Expand Down Expand Up @@ -97,8 +98,10 @@ class coptionSome: ScalarFixedSizeBeet {
func read<T>(buf: Data, offset: Int) -> T {
switch inner.value {
case .scalar(let type):
debugPrint("read \(description): \(type)")
return type.read(buf: buf, offset: offset + 1)
case .collection(let type):
debugPrint("read \(description): \(type)")
return type.read(buf: buf, offset: offset + 1)
}
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/Beet/Beets/Numbers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ public class u64: ScalarFixedSizeBeet {
}

public func read<T>(buf: Data, offset: Int) -> T {

let x = buf.withUnsafeBytes({ (rawPtr: UnsafeRawBufferPointer) in
return rawPtr.load(fromByteOffset: offset, as: UInt64.self)
let x = Data(buf.bytes[offset..<(offset + Int(byteSize))]).withUnsafeBytes({ (rawPtr: UnsafeRawBufferPointer) in
return rawPtr.load(fromByteOffset: 0, as: UInt64.self)
})
debugPrint("read \(description): \(x)")
return x as! T
Expand Down Expand Up @@ -333,7 +332,9 @@ public class bool: ScalarFixedSizeBeet {
} else {
data = 0.data()
}
advanced.replaceSubrange(offset..<offset+data.count, with: data)
if offset > offset+data.count {
advanced.replaceSubrange(offset..<offset+data.count, with: data)
}
buf = advanced
}

Expand Down
3 changes: 1 addition & 2 deletions Sources/Beet/Struct.Fixable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public class FixableBeetArgsStruct<Class>: FixableBeetStruct<Args> {
}

override public func toFixedFromValue(val: Any) -> FixedSizeBeet {
let value = val as! Class
let mirror = mirrored(value: value)
let mirror = mirrored(value: val)

var dictionary: [AnyHashable: Any] = [:]
for param in mirror.params {
Expand Down

0 comments on commit 05657fa

Please sign in to comment.