Skip to content

Fix PostgresNumeric.init crash at large number #187

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 16 additions & 26 deletions Sources/PostgresNIO/Data/PostgresData+Numeric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,36 +251,26 @@ extension PostgresData {
}

private extension Collection {
// splits the collection into chunks of the supplied size
// if the collection is not evenly divisible, the last chunk will be smaller
/// Split the collection into chunks of `maxSize` length each. The last chunk may contain anywhere between 1 and maxSize elements. `maxSize` must be greater than zero.
func chunked(by maxSize: Int) -> [SubSequence] {
return stride(from: 0, to: self.count, by: maxSize).map { current in
let chunkStartIndex = self.index(self.startIndex, offsetBy: current)
let chunkEndOffset = Swift.min(
self.distance(from: chunkStartIndex, to: self.endIndex),
maxSize
)
let chunkEndIndex = self.index(chunkStartIndex, offsetBy: chunkEndOffset)
return self[chunkStartIndex..<chunkEndIndex]
let terminatorIndex = self.index(self.endIndex, offsetBy: -1, limitedBy: self.startIndex) ?? self.endIndex
let strided = Array(sequence(first: self.startIndex) {
self.index($0, offsetBy: maxSize, limitedBy: terminatorIndex)
})
return zip(strided, strided.dropFirst() + CollectionOfOne(self.endIndex)).map {
self[$0..<$1]
}
}

// splits the collection into chunks of the supplied size
// if the collection is not evenly divisible, the first chunk will be smaller

/// Split the collection into chunks of `maxSize` length each. The **first** chunk may contain anywhere between 1 and maxSize elements. `maxSize` must be greater than zero.
func reverseChunked(by maxSize: Int) -> [SubSequence] {
var lastDistance = 0
var chunkStartIndex = self.startIndex
return stride(from: 0, to: self.count, by: maxSize).reversed().map { current in
let distance = (self.count - current) - lastDistance
lastDistance = distance
let chunkEndOffset = Swift.min(
self.distance(from: chunkStartIndex, to: self.endIndex),
distance
)
let chunkEndIndex = self.index(chunkStartIndex, offsetBy: chunkEndOffset)
defer { chunkStartIndex = chunkEndIndex }
return self[chunkStartIndex..<chunkEndIndex]
let terminatorIndex = self.index(self.startIndex, offsetBy: 1, limitedBy: self.endIndex) ?? self.startIndex
let strided = Array(sequence(first: self.endIndex) {
self.index($0, offsetBy: -maxSize, limitedBy: terminatorIndex)
})
return zip(strided, strided.dropFirst() + CollectionOfOne(self.startIndex)).map {
self[$1..<$0]
}
.reversed()
}
}

8 changes: 6 additions & 2 deletions Tests/IntegrationTests/PostgresNIOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -463,20 +463,24 @@ final class PostgresNIOTests: XCTestCase {
let a = PostgresNumeric(string: "123456.789123")!
let b = PostgresNumeric(string: "-123456.789123")!
let c = PostgresNumeric(string: "3.14159265358979")!
let d = PostgresNumeric(string: "1234567890123")!
var rows: PostgresQueryResult?
XCTAssertNoThrow(rows = try conn?.query("""
select
$1::numeric::text as a,
$2::numeric::text as b,
$3::numeric::text as c
$3::numeric::text as c,
$4::numeric::text as d
""", [
.init(numeric: a),
.init(numeric: b),
.init(numeric: c)
.init(numeric: c),
.init(numeric: d)
]).wait())
XCTAssertEqual(rows?.first?.column("a")?.string, "123456.789123")
XCTAssertEqual(rows?.first?.column("b")?.string, "-123456.789123")
XCTAssertEqual(rows?.first?.column("c")?.string, "3.14159265358979")
XCTAssertEqual(rows?.first?.column("d")?.string, "1234567890123")
}

func testMoney() {
Expand Down