Skip to content

Commit c58c45f

Browse files
committed
Add various preconditions to ByteReader
These preconditions prevent unexpected behaviour and replace general out-of-bounds errors. Also bytes(count:) now returns empty array earlier if count equals to 0.
1 parent 861ce64 commit c58c45f

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

Sources/ByteReader.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,25 @@ public class ByteReader {
3737
/**
3838
Reads byte and returns it, advancing by one position.
3939

40-
- Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
41-
to check if the end is reached.
40+
- Precondition: There MUST be enough data left (`offset < size`).
4241
*/
4342
public func byte() -> UInt8 {
43+
precondition(self.offset < self.size)
4444
self.offset += 1
4545
return self.data[self.offset - 1]
4646
}
4747

4848
/**
4949
Reads `count` bytes and returns them as an array of `UInt8`, advancing by `count` positions.
5050

51-
- Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
52-
to check if the end is reached.
51+
- Precondition: There MUST be enough data left (`offset + count <= size`).
52+
- Precondition: Parameter `count` MUST not be less than 0.
5353
*/
5454
public func bytes(count: Int) -> [UInt8] {
55+
precondition(count >= 0)
56+
guard count > 0
57+
else { return [] }
58+
precondition(self.offset + count <= self.size)
5559
let result = self.data[self.offset..<self.offset + count].toArray(type: UInt8.self, count: count)
5660
self.offset += count
5761
return result
@@ -60,10 +64,10 @@ public class ByteReader {
6064
/**
6165
Reads 8 bytes and returns them as a `UInt64` number, advancing by 8 positions.
6266

63-
- Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
64-
to check if the end is reached.
67+
- Precondition: There MUST be enough data left to read (`offset + 8 <= size`).
6568
*/
6669
public func uint64() -> UInt64 {
70+
precondition(self.offset + 8 <= self.size)
6771
let result = self.data[self.offset..<self.offset + 8].to(type: UInt64.self)
6872
self.offset += 8
6973
return result
@@ -72,10 +76,10 @@ public class ByteReader {
7276
/**
7377
Reads 4 bytes and returns them as a `UInt32` number, advancing by 4 positions.
7478

75-
- Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
76-
to check if the end is reached.
79+
- Precondition: There MUST be enough data left to read (`offset + 4 <= size`).
7780
*/
7881
public func uint32() -> UInt32 {
82+
precondition(self.offset + 4 <= self.size)
7983
let result = self.data[self.offset..<self.offset + 4].to(type: UInt32.self)
8084
self.offset += 4
8185
return result
@@ -84,10 +88,10 @@ public class ByteReader {
8488
/**
8589
Reads 2 bytes and returns them as a `UInt16` number, advancing by 2 positions.
8690

87-
- Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
88-
to check if the end is reached.
91+
- Precondition: There MUST be enough data left to read (`offset + 2 <= size`).
8992
*/
9093
public func uint16() -> UInt16 {
94+
precondition(self.offset + 2 <= self.size)
9195
let result = self.data[self.offset..<self.offset + 2].to(type: UInt16.self)
9296
self.offset += 2
9397
return result

0 commit comments

Comments
 (0)