Files
Thomas Van Lenten 3c8b0109b1 Use the fuzz options support to control encoding options. (#2049)
In looking at the code coverage on oss-fuzz, I realized the encoding
support never goes down paths related to options being not the default,
so go ahead and allow the existing options extraction to set those also,
so we're running everything possible to help look for bugs.

All but one of the options defaulted to "false", so since the fuzz
options parsing was ensuring the extra bits were zero, it is safe to
just add the option. The one that wasn't zero, we add a new "inverted"
bool so all saved cases remain valid.

This also gave us the change to control "partial" on binary decoding, so
wire that up also.

Lastly, while at it, increase the options logging so it is a little more
complete.
2026-05-05 10:16:24 -04:00

33 lines
1.1 KiB
Swift

// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See LICENSE.txt for license information:
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
//
// -----------------------------------------------------------------------------
import Foundation
import FuzzCommon
import SwiftProtobuf
@_cdecl("LLVMFuzzerTestOneInput")
public func FuzzJSON(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
guard let (options, bytes) = JSONFuzzingOptions.extractOptions(start, count) else {
return 1
}
var msg: SwiftProtoTesting_Fuzz_Message?
do {
msg = try SwiftProtoTesting_Fuzz_Message(
jsonUTF8Data: Data(bytes),
extensions: SwiftProtoTesting_Fuzz_FuzzTesting_Extensions,
options: options.decoding
)
} catch {
// Error parsing are to be expected since not all input will be well formed.
}
// Test serialization for completeness.
// If a message was parsed, it should not fail to serialize, so assert as such.
let _ = try! msg?.jsonString(options: options.encoding)
return 0
}