Skip to content

Commit 22a71b7

Browse files
committed
Fix options needing multiple bytes, add tests.
1 parent 1bfc284 commit 22a71b7

File tree

9 files changed

+349
-11
lines changed

9 files changed

+349
-11
lines changed

FuzzTesting/Package.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ let package = Package(
3030
.target(
3131
name: "FuzzTextFormat",
3232
dependencies: ["SwiftProtobuf", "FuzzCommon"]),
33+
.testTarget(
34+
name: "FuzzCommonTests",
35+
dependencies: ["FuzzCommon"]),
3336
]
3437
)

FuzzTesting/Sources/FuzzAsyncMessageSequence/main.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
2+
// Licensed under Apache License v2.0 with Runtime Library Exception
3+
//
4+
// See LICENSE.txt for license information:
5+
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
6+
//
7+
// -----------------------------------------------------------------------------
8+
19
import Foundation
210

311
import FuzzCommon

FuzzTesting/Sources/FuzzBinary/main.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
2+
// Licensed under Apache License v2.0 with Runtime Library Exception
3+
//
4+
// See LICENSE.txt for license information:
5+
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
6+
//
7+
// -----------------------------------------------------------------------------
8+
19
import FuzzCommon
210

311
import SwiftProtobuf

FuzzTesting/Sources/FuzzBinaryDelimited/main.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
2+
// Licensed under Apache License v2.0 with Runtime Library Exception
3+
//
4+
// See LICENSE.txt for license information:
5+
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
6+
//
7+
// -----------------------------------------------------------------------------
8+
19
import Foundation
210

311
import FuzzCommon

FuzzTesting/Sources/FuzzCommon/Options.swift

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
2+
// Licensed under Apache License v2.0 with Runtime Library Exception
3+
//
4+
// See LICENSE.txt for license information:
5+
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
6+
//
7+
// -----------------------------------------------------------------------------
8+
19
import Foundation
210

311
import SwiftProtobuf
@@ -29,23 +37,33 @@ extension SupportsFuzzOptions {
2937
return (options, UnsafeRawBufferPointer(start: start, count: count))
3038
}
3139

32-
// Set over the zero
40+
// Step over the zero
3341
start += 1
3442
count -= 1
3543

36-
var optionsBits: UInt8? = nil
44+
var optionsBits = start.loadUnaligned(as: UInt8.self)
45+
start += 1
46+
count -= 1
3747
var bit = 0
3848
for opt in fuzzOptionsList {
39-
if optionsBits == nil {
49+
var isSet = optionsBits & (1 << bit) != 0
50+
if bit == 7 {
51+
// About the use the last bit of this byte, to allow more options in
52+
// the future, use this bit to indicate reading another byte.
53+
guard isSet else {
54+
// No continuation, just return whatever we got.
55+
return (options, UnsafeRawBufferPointer(start: start, count: count))
56+
}
4057
guard count >= 1 else {
4158
return nil // No data left to read bits
4259
}
4360
optionsBits = start.loadUnaligned(as: UInt8.self)
4461
start += 1
4562
count -= 1
63+
bit = 0
64+
isSet = optionsBits & (1 << bit) != 0
4665
}
4766

48-
let isSet = optionsBits! & (1 << bit) != 0
4967
switch opt {
5068
case .boolean(let keypath):
5169
options[keyPath: keypath] = isSet
@@ -61,16 +79,11 @@ extension SupportsFuzzOptions {
6179
options[keyPath: keypath] = Int(value % mod)
6280
}
6381
}
64-
6582
bit += 1
66-
if bit == 8 { // Rolled over, cause a new load next time through
67-
bit = 0
68-
optionsBits = nil
69-
}
7083
}
7184
// Ensure the any remaining bits are zero so they can be used in the future
72-
while optionsBits != nil && bit < 8 {
73-
if optionsBits! & (1 << bit) != 0 {
85+
while bit < 8 {
86+
if optionsBits & (1 << bit) != 0 {
7487
return nil
7588
}
7689
bit += 1

FuzzTesting/Sources/FuzzJSON/main.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
2+
// Licensed under Apache License v2.0 with Runtime Library Exception
3+
//
4+
// See LICENSE.txt for license information:
5+
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
6+
//
7+
// -----------------------------------------------------------------------------
8+
19
import Foundation
210

311
import FuzzCommon

FuzzTesting/Sources/FuzzTextFormat/main.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
2+
// Licensed under Apache License v2.0 with Runtime Library Exception
3+
//
4+
// See LICENSE.txt for license information:
5+
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
6+
//
7+
// -----------------------------------------------------------------------------
8+
19
import Foundation
210

311
import FuzzCommon

0 commit comments

Comments
 (0)