Skip to content

Commit da907fb

Browse files
committed
Variadic Generic decoding: Ensure PostgresRow length check
1 parent 0b176b0 commit da907fb

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Sources/PostgresNIO/New/VariadicGenerics.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ extension PostgresRow {
5252
file: String = #fileID,
5353
line: Int = #line
5454
) throws -> (repeat each Column) {
55+
let packCount = ComputeParameterPackLength.count(ofPack: repeat (each Column).self)
56+
precondition(self.columns.count >= packCount)
57+
5558
var columnIndex = 0
5659
var cellIterator = self.data.makeIterator()
5760
var columnIterator = self.columns.makeIterator()
@@ -153,4 +156,19 @@ extension AsyncSequence where Element == PostgresRow {
153156
self.decode(columnType, context: .default, file: file, line: line)
154157
}
155158
}
159+
160+
@usableFromInline
161+
enum ComputeParameterPackLength {
162+
@usableFromInline
163+
enum BoolConverter<T> {
164+
@usableFromInline
165+
typealias Bool = Swift.Bool
166+
}
167+
168+
@inlinable
169+
static func count<each T>(ofPack t: repeat each T) -> Int {
170+
MemoryLayout<(repeat BoolConverter<each T>.Bool)>.size / MemoryLayout<Bool>.stride
171+
}
172+
}
156173
#endif // compiler(>=5.9)
174+

0 commit comments

Comments
 (0)