Skip to content
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

Fix crash when parsing timestamps with microsecond text #101

Open
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions Sources/MySQLNIO/MySQLTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ public struct MySQLTime: Equatable, MySQLDataConvertible {
let day = UInt16(parts[2]),
let hour = UInt16(parts[3]),
let minute = UInt16(parts[4]),
let second = UInt16(parts[5])
let second = Double(parts[5])
else {
return nil
}
self.init(year: year, month: month, day: day, hour: hour, minute: minute, second: second)

let fullSecond = UInt32((second * 1_000_000).rounded())
let integer = UInt16(fullSecond / 1_000_000)
let real = fullSecond % 1_000_000

self.init(year: year, month: month, day: day, hour: hour, minute: minute, second: integer, microsecond: real)
}

/// See ``MySQLDataConvertible/init(mysqlData:)``.
Expand Down
15 changes: 15 additions & 0 deletions Tests/MySQLNIOTests/MySQLNIOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,21 @@ final class MySQLNIOTests: XCTestCase {
XCTAssertEqual(rows[0].column("qux")?.int, 3)
}

func testTextWithMicrosecondsMySQLTimeParse() throws {
let dateString = "2024-04-15 22:38:12.392812"

let time = MySQLTime(dateString)

XCTAssertNotNil(time)
XCTAssertEqual(time?.year, 2024)
XCTAssertEqual(time?.month, 4)
XCTAssertEqual(time?.day, 15)
XCTAssertEqual(time?.hour, 22)
XCTAssertEqual(time?.minute, 38)
XCTAssertEqual(time?.second, 12)
XCTAssertEqual(time?.microsecond, 392812)
}

// https://github.com/vapor/mysql-nio/issues/87
func testUnexpectedPacketHandling() async throws {
struct PingCommand: MySQLCommand {
Expand Down