A fast, safe, and efficient CBOR serialization library for Swift on any platform. swiftpackageindex.com/thecoolwinter/CBOR/1.1.1/documentation/cbor
atproto swift cbor
at 1.0.0 71 lines 1.7 kB view raw
1// 2// fuzz.swift 3// CBOR 4// 5// Created by Khan Winter on 8/29/25. 6// 7 8import CBOR 9#if canImport(FoundationEssentials) 10import FoundationEssentials 11#else 12import Foundation 13#endif 14 15@_optimize(none) 16func blackhole(_ val: some Any) { } 17 18/// Fuzzing entry point 19@_cdecl("LLVMFuzzerTestOneInput") 20public func fuzz(_ start: UnsafeRawPointer, _ count: Int) -> CInt { 21 let bytes = UnsafeRawBufferPointer(start: start, count: count) 22 let data = Data(bytes) 23 24 // Try decoding against a bunch of standard types. 25 // Each attempt is independent errors are ignored, crashes are what fuzzing is for. 26 func tryDecode<T: Decodable>(_ type: T.Type) { 27 do { 28 blackhole(try CBORDecoder(rejectIndeterminateLengths: false).decode(T.self, from: data)) 29 } catch { 30 // ignore decode errors 31 } 32 } 33 34 // Scalars 35 tryDecode(Bool.self) 36 tryDecode(Int.self) 37 tryDecode(Int8.self) 38 tryDecode(Int16.self) 39 tryDecode(Int32.self) 40 tryDecode(Int64.self) 41 tryDecode(UInt.self) 42 tryDecode(UInt8.self) 43 tryDecode(UInt16.self) 44 tryDecode(UInt32.self) 45 tryDecode(UInt64.self) 46 tryDecode(Float.self) 47 tryDecode(Double.self) 48 tryDecode(String.self) 49 tryDecode(Data.self) 50 51 // Optionals 52 tryDecode(Optional<Int>.self) 53 tryDecode(Optional<String>.self) 54 tryDecode(Optional<Data>.self) 55 56 // Collections 57 tryDecode([Int].self) 58 tryDecode([String].self) 59 tryDecode([Data].self) 60 tryDecode([String: Int].self) 61 tryDecode([String: String].self) 62 63 // Nested combinations 64 tryDecode([[Int]].self) 65 tryDecode([String: [String: Int]].self) 66 67 // Any decodable 68 tryDecode(AnyDecodable.self) 69 70 return 0 71}