浏览代码

Fix undefined behavior in timeout checks and ensure safe channel access for outgoing commands

FluentCoding 5 月之前
父节点
当前提交
d6e759423f
共有 1 个文件被更改,包括 8 次插入2 次删除
  1. 8 2
      Source/Native/enet.h

+ 8 - 2
Source/Native/enet.h

@@ -2685,7 +2685,7 @@ extern "C" {
 			if (peer->earliestTimeout == 0 || ENET_TIME_LESS(outgoingCommand->sentTime, peer->earliestTimeout))
 				peer->earliestTimeout = outgoingCommand->sentTime;
 
-			if (peer->earliestTimeout != 0 && (ENET_TIME_DIFFERENCE(host->serviceTime, peer->earliestTimeout) >= peer->timeoutMaximum || ((uint32_t)(1u << (outgoingCommand->sendAttempts - 1)) >= peer->timeoutLimit && ENET_TIME_DIFFERENCE(host->serviceTime, peer->earliestTimeout) >= peer->timeoutMinimum))) {
+			if (peer->earliestTimeout != 0 && (ENET_TIME_DIFFERENCE(host->serviceTime, peer->earliestTimeout) >= peer->timeoutMaximum || ((uint32_t)(1u << ((outgoingCommand->sendAttempts - 1) % 32)) >= peer->timeoutLimit && ENET_TIME_DIFFERENCE(host->serviceTime, peer->earliestTimeout) >= peer->timeoutMinimum))) {
 				enet_protocol_notify_disconnect_timeout(host, peer, event);
 
 				return 1;
@@ -3523,7 +3523,13 @@ extern "C" {
 	}
 
 	void enet_peer_setup_outgoing_command(ENetPeer* peer, ENetOutgoingCommand* outgoingCommand) {
-		ENetChannel* channel = &peer->channels[outgoingCommand->command.header.channelID];
+		ENetChannel *channel = NULL;
+
+		if (outgoingCommand->command.header.channelID < peer->channelCount)
+		{
+			channel = &peer->channels[outgoingCommand->command.header.channelID];
+		}
+
 		peer->outgoingDataTotal += enet_protocol_command_size(outgoingCommand->command.header.command) + outgoingCommand->fragmentLength;
 
 		if (outgoingCommand->command.header.channelID == 0xFF) {