From 83d7f675a7e49cea371561b74cd0d3672b64e9f9 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 12 Feb 2019 22:24:18 -0800 Subject: [PATCH] Fix sign extension bug in connection termination error code --- src/ControlStream.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/ControlStream.c b/src/ControlStream.c index 5b462a4..d61f31f 100644 --- a/src/ControlStream.c +++ b/src/ControlStream.c @@ -422,7 +422,7 @@ static void controlReceiveThreadFunc(void* context) { return; } - unsigned short terminationReason = -1; + long terminationErrorCode = -1; while (!PltIsThreadInterrupted(&controlReceiveThread)) { ENetEvent event; @@ -508,10 +508,22 @@ static void controlReceiveThreadFunc(void* context) { BbInitializeWrappedBuffer(&bb, event.packet->data, sizeof(*ctlHdr), event.packet->dataLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE); + unsigned short terminationReason; + BbGetShort(&bb, &terminationReason); Limelog("Server notified termination reason: 0x%04x\n", terminationReason); + // SERVER_TERMINATED_INTENDED + if (terminationReason == 0x0100) { + // Pass error code 0 to notify the client that this was not an error + terminationErrorCode = 0; + } + else { + // Otherwise pass the reason unmodified + terminationErrorCode = terminationReason; + } + // We don't actually notify the connection listener until we receive // the disconnect event from the server that confirms the termination. } @@ -520,16 +532,7 @@ static void controlReceiveThreadFunc(void* context) { } else if (event.type == ENET_EVENT_TYPE_DISCONNECT) { Limelog("Control stream received disconnect event\n"); - - // SERVER_TERMINATED_INTENDED - if (terminationReason == 0x0100) { - // Pass error code 0 to notify the client that this was not an error - ListenerCallbacks.connectionTerminated(0); - } - else { - ListenerCallbacks.connectionTerminated(terminationReason); - } - + ListenerCallbacks.connectionTerminated(terminationErrorCode); return; } }