Skip to content

Enable more checks #17

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 4 commits into from
Jul 12, 2025
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
9 changes: 4 additions & 5 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ jobs:
name: Soundness
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
with:
format_check_enabled: false # bug: https://github.com/swiftlang/swift-format/issues/1028
shell_check_enabled: false
yamllint_check_enabled: false
format_check_enabled: false # bug: https://github.com/swiftlang/swift-format/issues/1028
shell_check_enabled: true
yamllint_check_enabled: true
api_breakage_check_enabled: false
docs_check_enabled: false

docs_check_enabled: false # doc check runs using 6.0.3 compiler
2 changes: 0 additions & 2 deletions Scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ echo "Formatting Swift sources in $(pwd)"
# Run the format / lint commands
git ls-files -z '*.swift' | xargs -0 swift format format --parallel --in-place
git ls-files -z '*.swift' | xargs -0 swift format lint --strict --parallel

sed -i '' 's/borrowbuffer/borrow buffer/g' "Sources/BinaryParsing/Parser Types/ParserSpan.swift"
13 changes: 8 additions & 5 deletions Sources/BinaryParsing/Operations/OptionalOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Collection {
}
return self[i]
}

/// Returns the subsequence at the given range, or `nil` if the range is out
/// of bounds.
@inlinable
Expand All @@ -40,14 +40,17 @@ extension Collection where Index == Int {
}
return self[i]
}

/// Returns the subsequence at the given range after converting the bounds
/// to `Int`, or `nil` if the range is out of bounds.
@_alwaysEmitIntoClient
public subscript(ifInBounds bounds: Range<some FixedWidthInteger>) -> SubSequence? {
public subscript(ifInBounds bounds: Range<some FixedWidthInteger>)
-> SubSequence?
{
guard let low = Int(exactly: bounds.lowerBound),
let high = Int(exactly: bounds.upperBound),
low >= startIndex, high <= endIndex else {
let high = Int(exactly: bounds.upperBound),
low >= startIndex, high <= endIndex
else {
return nil
}
return self[low..<high]
Expand Down
2 changes: 1 addition & 1 deletion Sources/BinaryParsing/Operations/Optionators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extension Optional where Wrapped: FixedWidthInteger {
public static func -?= (a: inout Self, b: Self) {
a = a -? b
}

/// Multiplies two values and stores the result in the left-hand-side variable,
/// if the values are non-`nil` and the result is representable.
@inlinable @inline(__always)
Expand Down
2 changes: 1 addition & 1 deletion Sources/BinaryParsing/Operations/ThrowingOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension Collection {
return self[i]
}
}

/// Returns the subsequence in the given range, throwing an error if the range
/// is not in bounds.
@inlinable
Expand Down
2 changes: 1 addition & 1 deletion Sources/BinaryParsing/Parser Types/Endianness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension Endianness {
public var isBigEndian: Bool {
_isBigEndian
}

/// A Boolean value inidicating whether the endianness is little-endian.
public var isLittleEndian: Bool {
!_isBigEndian
Expand Down
23 changes: 12 additions & 11 deletions Sources/BinaryParsing/Parsers/Integer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ extension FixedWidthInteger where Self: BitwiseCopyable {

// 2) Load and store value in `result`.
let result =
unsafe endianness.isBigEndian
unsafe endianness.isBigEndian
? Self(_unchecked: (), _parsingBigEndian: &input)
: Self(_unchecked: (), _parsingLittleEndian: &input)

Expand Down Expand Up @@ -207,7 +207,8 @@ extension FixedWidthInteger where Self: BitwiseCopyable {
func consumeZeroPadding() throws(ParsingError) {
var paddingBuffer = input.divide(atOffset: paddingCount)
for _ in 0..<paddingCount {
guard unsafe 0 == paddingBuffer.consumeUnchecked(type: UInt8.self) else {
guard unsafe 0 == paddingBuffer.consumeUnchecked(type: UInt8.self)
else {
throw ParsingError(
status: .invalidValue, location: paddingBuffer.startPosition)
}
Expand All @@ -217,12 +218,12 @@ extension FixedWidthInteger where Self: BitwiseCopyable {
if endianness.isBigEndian {
try consumeZeroPadding()
self =
unsafe endianness.isBigEndian
unsafe endianness.isBigEndian
? Self(_unchecked: (), _parsingBigEndian: &input)
: Self(_unchecked: (), _parsingLittleEndian: &input)
} else {
self =
unsafe endianness.isBigEndian
unsafe endianness.isBigEndian
? Self(_unchecked: (), _parsingBigEndian: &input)
: Self(_unchecked: (), _parsingLittleEndian: &input)
try consumeZeroPadding()
Expand All @@ -241,7 +242,7 @@ extension FixedWidthInteger where Self: BitwiseCopyable {
let paddingCount = byteCount - MemoryLayout<Self>.size
if paddingCount < 0 {
self =
unsafe Self.isSigned
unsafe Self.isSigned
? Self(
_unchecked: (), _parsingSigned: &input, endianness: endianness,
byteCount: byteCount)
Expand All @@ -250,7 +251,7 @@ extension FixedWidthInteger where Self: BitwiseCopyable {
byteCount: byteCount)
} else {
self =
try unsafe Self.isSigned
try unsafe Self.isSigned
? Self(
_unchecked: (), _parsingSigned: &input, endianness: endianness,
paddingCount: paddingCount)
Expand Down Expand Up @@ -330,7 +331,7 @@ extension MultiByteInteger {
_unchecked _: Void, parsing input: inout ParserSpan, endianness: Endianness
) {
self =
unsafe endianness.isBigEndian
unsafe endianness.isBigEndian
? Self(_unchecked: (), _parsingBigEndian: &input)
: Self(_unchecked: (), _parsingLittleEndian: &input)
}
Expand Down Expand Up @@ -485,8 +486,8 @@ extension FixedWidthInteger where Self: BitwiseCopyable {
/// - Parameters:
/// - input: The `ParserSpan` to parse from. If parsing succeeds, the start
/// position of `input` is moved forward by `byteCount`.
/// - byteCount: The number of bytes to read the value from.
/// - endianness: The endianness to use when interpreting the parsed value.
/// - byteCount: The number of bytes to read the value from.
/// - Throws: A `ParsingError` if `input` contains fewer than `byteCount`
/// bytes, if the parsed value overflows this integer type, or if the
/// padding bytes are invalid.
Expand Down Expand Up @@ -601,7 +602,7 @@ extension FixedWidthInteger where Self: BitwiseCopyable {
endianness: Endianness
) throws(ParsingError) {
let result =
unsafe endianness.isBigEndian
unsafe endianness.isBigEndian
? T(_unchecked: (), _parsingBigEndian: &input)
: T(_unchecked: (), _parsingLittleEndian: &input)
self = try Self(_throwing: result)
Expand Down Expand Up @@ -654,7 +655,8 @@ extension FixedWidthInteger where Self: BitwiseCopyable {
parsing input: inout ParserSpan,
storedAs: T.Type
) throws(ParsingError) {
self = try unsafe Self(_throwing: T(truncatingIfNeeded: input.consumeUnchecked()))
self = try unsafe Self(
_throwing: T(truncatingIfNeeded: input.consumeUnchecked()))
}

/// Creates an integer by parsing and converting a value of the given
Expand All @@ -678,7 +680,6 @@ extension FixedWidthInteger where Self: BitwiseCopyable {
/// position of `input` is moved forward by the size of this integer.
/// - storageType: The integer type to parse from `input` before conversion
/// to the destination type.
/// - endianness: The endianness to use when interpreting the parsed value.
/// - Throws: A `ParsingError` if `input` does not have enough bytes to store
/// `storageType`, or if converting the parsed value to this integer type
/// overflows.
Expand Down
2 changes: 1 addition & 1 deletion Tests/BinaryParsingTests/EndiannessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct EndiannessTests {
let endianness = Endianness(isBigEndian: isBigEndian)
#expect(endianness.isBigEndian == isBigEndian)
#expect(endianness.isLittleEndian == !isBigEndian)

let endianness2: Endianness = isBigEndian ? .big : .little
#expect(endianness == endianness2)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/BinaryParsingTests/IntegerParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ struct IntegerParsingTests {
let number = T.random(in: .min ... .max, using: &rng)
try runTest(for: number)
}

let empty: [UInt8] = []
empty.withParserSpan { span in
#expect(throws: ParsingError.self) {
Expand Down
8 changes: 4 additions & 4 deletions Tests/BinaryParsingTests/OptionalOperationsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ struct OptionalOperationsTests {
let allIndices = str.indices + [str.endIndex]
let validIndices = substr.startIndex..<substr.endIndex
let validBounds = substr.startIndex...substr.endIndex

for low in allIndices.indices {
let i = allIndices[low]
if validIndices.contains(i) {
#expect(substr[ifInBounds: i] == substr[i])
} else {
#expect(substr[ifInBounds: i] == nil)
}

for high in allIndices[low...].indices {
let j = allIndices[high]
if validBounds.contains(i) && validBounds.contains(j) {
Expand All @@ -167,14 +167,14 @@ struct OptionalOperationsTests {
let numbers = Array(1...100)
let slice = numbers.dropFirst(14).dropLast(20)
let validBounds = UInt8(slice.startIndex)...UInt8(slice.endIndex)

for i in 0...UInt8.max {
if slice.indices.contains(Int(i)) {
#expect(slice[ifInBounds: i] == slice[Int(i)])
} else {
#expect(slice[ifInBounds: i] == nil)
}

for j in i...UInt8.max {
if validBounds.contains(i) && validBounds.contains(j) {
#expect(slice[ifInBounds: i..<j] == slice[Int(i)..<Int(j)])
Expand Down
2 changes: 1 addition & 1 deletion Tests/BinaryParsingTests/ThrowingOperationsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ struct ThrowingOperationsTests {
try Self.numbers[throwing: i]
}
}

let validBounds = Self.numbers.startIndex...Self.numbers.endIndex
for j in i...10 {
if validBounds.contains(i), validBounds.contains(j) {
Expand Down