Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upFixing packet sequenceId bug and incorrect incoming packet lengh calculations #122
Conversation
SandorDobi
added some commits
Feb 11, 2018
let byte0: UInt32 = numericCast(buffer[0]) | ||
let byte1: UInt32 = numericCast(buffer[1]) | ||
let byte2: UInt32 = numericCast(buffer[2]) | ||
let byte0: UInt32 = (numericCast(buffer[0]) as UInt32).littleEndian |
This comment has been minimized.
This comment has been minimized.
gwynne
Feb 11, 2018
Since you're immediately casting to a specific numeric type, it would be more concise here to write let byte0 = UInt32(buffer[0])
. Additionally, the .littleEndian
call is a no-op on all little-endian platforms (and thus all platforms supported by Swift).
@@ -46,7 +46,12 @@ extension Packet { | |||
|
|||
// Require decimal `10` to be the protocol version | |||
guard try parser.byte() == 10 else { | |||
throw MySQLError(.invalidHandshake) | |||
// if the first byte is 0xff then we got an error packet | |||
if(parser.payload[0] == 0xff){ |
This comment has been minimized.
This comment has been minimized.
gwynne
Feb 11, 2018
Code style: To match the style found elsewhere in this file, write this line as if parser.payload[0] == 0xff {
@@ -81,13 +85,14 @@ internal final class Packet: ExpressibleByArrayLiteral { | |||
convenience init(arrayLiteral elements: UInt8...) { | |||
let pointer = MutableBytesPointer.allocate(capacity: 4 &+ elements.count) | |||
|
|||
let packetSizeBytes = [ | |||
let packetSizeAndSequenceIdBytes = [ | |||
UInt8((elements.count) & 0xff), |
This comment has been minimized.
This comment has been minimized.
gwynne
Feb 11, 2018
Preferentially to adding the (fake) sequence ID to this array, I recommend keeping the original name and memcpy and adding
var sequenceId = UInt8(0)
memcpy(pointer.advanced(by: 3), &sequenceId, 1)
after the original line 90
This comment has been minimized.
This comment has been minimized.
Joannis
Feb 11, 2018
•
Member
@gwynne can't you just do pointer[3] = sequenceId
?
Or pointer.avanced(by: 3).pointee = sequenceId
This comment has been minimized.
This comment has been minimized.
gwynne
Feb 11, 2018
•
Actually, yes! Though the compiler will generate the same code either way in the end.
SandorDobi commentedFeb 11, 2018
Hi,
The outgoing packet sequenceId was broken, it got optimized out from the source, so when it allocates packet the sequenceid field (4. byte of the buffer) contains random data. This PR fix this and set it to zero. I don't touch the handshake sequenceid generation where it is used (yet).
Later on the packet fragmentation needs to be adressed thought because the servers are using this even when it has no practical usage.
The incoming packet length was incorrectly calculated, therefore all packets bigger than 255 byte likely causing memory errors
fixing dependencies to beta.1 tag