Skip to content

Commit 87c34e6

Browse files
committed
Improve OK packet parsing. Fixes #842
Be more robust when a server sets a flag that implies the existence of extra data, but doesn't actually send the data.
1 parent 282ba13 commit 87c34e6

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/MySqlConnector/Protocol/Payloads/OkPayload.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,38 @@ public static OkPayload Create(ReadOnlySpan<byte> span, bool deprecateEof, bool
3737
var warningCount = (int) reader.ReadUInt16();
3838
string? newSchema = null;
3939

40-
if (clientSessionTrack && (serverStatus & ServerStatus.SessionStateChanged) == ServerStatus.SessionStateChanged)
40+
if (clientSessionTrack)
4141
{
42-
reader.ReadLengthEncodedByteString(); // human-readable info
43-
44-
// implies ProtocolCapabilities.SessionTrack
45-
var sessionStateChangeDataLength = checked((int) reader.ReadLengthEncodedInteger());
46-
var endOffset = reader.Offset + sessionStateChangeDataLength;
47-
while (reader.Offset < endOffset)
42+
if (reader.BytesRemaining > 0)
4843
{
49-
var kind = (SessionTrackKind) reader.ReadByte();
50-
var dataLength = (int) reader.ReadLengthEncodedInteger();
51-
switch (kind)
44+
reader.ReadLengthEncodedByteString(); // human-readable info
45+
46+
if ((serverStatus & ServerStatus.SessionStateChanged) == ServerStatus.SessionStateChanged && reader.BytesRemaining > 0)
5247
{
53-
case SessionTrackKind.Schema:
54-
newSchema = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
55-
break;
48+
// implies ProtocolCapabilities.SessionTrack
49+
var sessionStateChangeDataLength = checked((int) reader.ReadLengthEncodedInteger());
50+
var endOffset = reader.Offset + sessionStateChangeDataLength;
51+
while (reader.Offset < endOffset)
52+
{
53+
var kind = (SessionTrackKind) reader.ReadByte();
54+
var dataLength = (int) reader.ReadLengthEncodedInteger();
55+
switch (kind)
56+
{
57+
case SessionTrackKind.Schema:
58+
newSchema = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
59+
break;
5660

57-
default:
58-
reader.Offset += dataLength;
59-
break;
61+
default:
62+
reader.Offset += dataLength;
63+
break;
64+
}
65+
}
6066
}
6167
}
6268
}
6369
else
6470
{
65-
// either "string<EOF> info" or "string<lenenc> info" (followed by no session change info)
66-
// ignore human-readable string in both cases
71+
// ignore "string<EOF> info" human-readable string
6772
}
6873

6974
if (affectedRowCount == 0 && lastInsertId == 0 && warningCount == 0 && newSchema is null)

0 commit comments

Comments
 (0)