Skip to content

Fix a crash if an empty JSON configuration file is specified. #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Sources/Testing/Support/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ enum JSON {
static func decode<T>(_ type: T.Type, from jsonRepresentation: UnsafeRawBufferPointer) throws -> T where T: Decodable {
#if canImport(Foundation)
try withExtendedLifetime(jsonRepresentation) {
let data = Data(
bytesNoCopy: .init(mutating: jsonRepresentation.baseAddress!),
count: jsonRepresentation.count,
deallocator: .none
)
let byteCount = jsonRepresentation.count
let data = if byteCount > 0 {
Data(
bytesNoCopy: .init(mutating: jsonRepresentation.baseAddress!),
count: byteCount,
deallocator: .none
)
} else {
Data()
}
return try JSONDecoder().decode(type, from: data)
}
#else
Expand Down
12 changes: 12 additions & 0 deletions Tests/TestingTests/ABIEntryPointTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#if canImport(Foundation) && !SWT_NO_ABI_ENTRY_POINT
@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing

#if canImport(Foundation)
private import Foundation
#endif
private import _TestingInternals

@Suite("ABI entry point tests")
Expand Down Expand Up @@ -150,5 +153,14 @@ struct ABIEntryPointTests {
// Call the entry point function.
return try await abiEntryPoint(.init(argumentsJSON), recordHandler)
}

#if canImport(Foundation)
@Test func decodeEmptyConfiguration() throws {
let emptyBuffer = UnsafeRawBufferPointer(start: nil, count: 0)
#expect(throws: DecodingError.self) {
_ = try JSON.decode(__CommandLineArguments_v0.self, from: emptyBuffer)
}
}
#endif
}
#endif