Skip to content

Commit 3b78b63

Browse files
authored
fix: Replace wcsncpy with wcslcpy for robustness (#1676)
1 parent 9ba92fa commit 3b78b63

File tree

12 files changed

+46
-94
lines changed

12 files changed

+46
-94
lines changed

Generals/Code/GameEngine/Include/Common/Language.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ typedef enum
7676
} LanguageID;
7777

7878
#define GameStrcpy wcscpy
79-
#define GameStrncpy wcsncpy
8079
#define GameStrlen wcslen
8180
#define GameStrcat wcscat
8281
#define GameStrcmp wcscmp

Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ScoreScreen.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,7 @@ WindowMsgHandledType ScoreScreenSystem( GameWindow *window, UnsignedInt msg,
540540
req.arg.addbuddy.id = playerID;
541541
UnicodeString buddyAddstr;
542542
buddyAddstr = TheGameText->fetch("GUI:BuddyAddReq");
543-
wcsncpy(req.arg.addbuddy.text, buddyAddstr.str(), MAX_BUDDY_CHAT_LEN);
544-
req.arg.addbuddy.text[MAX_BUDDY_CHAT_LEN-1] = 0;
543+
wcslcpy(req.arg.addbuddy.text, buddyAddstr.str(), MAX_BUDDY_CHAT_LEN);
545544
TheGameSpyBuddyMessageQueue->addRequest(req);
546545
}
547546
}

Generals/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLBuddyOverlay.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ WindowMsgHandledType BuddyControlSystem( GameWindow *window, UnsignedInt msg,
304304
// Send the message
305305
BuddyRequest req;
306306
req.buddyRequestType = BuddyRequest::BUDDYREQUEST_MESSAGE;
307-
wcsncpy(req.arg.message.text, txtInput.str(), MAX_BUDDY_CHAT_LEN);
308-
req.arg.message.text[MAX_BUDDY_CHAT_LEN-1] = 0;
307+
wcslcpy(req.arg.message.text, txtInput.str(), MAX_BUDDY_CHAT_LEN);
309308
req.arg.message.recipient = selectedProfile;
310309
TheGameSpyBuddyMessageQueue->addRequest(req);
311310

@@ -1184,8 +1183,7 @@ void RequestBuddyAdd(Int profileID, AsciiString nick)
11841183
req.arg.addbuddy.id = profileID;
11851184
UnicodeString buddyAddstr;
11861185
buddyAddstr = TheGameText->fetch("GUI:BuddyAddReq");
1187-
wcsncpy(req.arg.addbuddy.text, buddyAddstr.str(), MAX_BUDDY_CHAT_LEN);
1188-
req.arg.addbuddy.text[MAX_BUDDY_CHAT_LEN-1] = 0;
1186+
wcslcpy(req.arg.addbuddy.text, buddyAddstr.str(), MAX_BUDDY_CHAT_LEN);
11891187
TheGameSpyBuddyMessageQueue->addRequest(req);
11901188

11911189
UnicodeString s;

Generals/Code/GameEngine/Source/GameNetwork/GameSpy/Thread/BuddyThread.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,7 @@ void BuddyThreadClass::messageCallback( GPConnection *con, GPRecvBuddyMessageArg
512512
gpGetInfo( con, arg->profile, GP_CHECK_CACHE, GP_BLOCKING, (GPCallback)getNickForMessage, &messageResponse);
513513

514514
std::wstring s = MultiByteToWideCharSingleLine( arg->message );
515-
wcsncpy(messageResponse.arg.message.text, s.c_str(), MAX_BUDDY_CHAT_LEN);
516-
messageResponse.arg.message.text[MAX_BUDDY_CHAT_LEN-1] = 0;
515+
wcslcpy(messageResponse.arg.message.text, s.c_str(), MAX_BUDDY_CHAT_LEN);
517516
messageResponse.arg.message.date = arg->date;
518517
DEBUG_LOG(("Got a buddy message from %d [%ls]", arg->profile, s.c_str()));
519518
TheGameSpyBuddyMessageQueue->addResponse( messageResponse );
@@ -629,8 +628,7 @@ void BuddyThreadClass::requestCallback( GPConnection *con, GPRecvBuddyRequestArg
629628
gpGetInfo( con, arg->profile, GP_CHECK_CACHE, GP_BLOCKING, (GPCallback)getInfoResponseForRequest, &response);
630629

631630
std::wstring s = MultiByteToWideCharSingleLine( arg->reason );
632-
wcsncpy(response.arg.request.text, s.c_str(), GP_REASON_LEN);
633-
response.arg.request.text[GP_REASON_LEN-1] = 0;
631+
wcslcpy(response.arg.request.text, s.c_str(), GP_REASON_LEN);
634632

635633
TheGameSpyBuddyMessageQueue->addResponse( response );
636634
}

Generals/Code/GameEngine/Source/GameNetwork/LANAPI.cpp

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,7 @@ void LANAPI::update( void )
519519
LANMessage msg;
520520
fillInLANMessage( &msg );
521521
msg.LANMessageType = LANMessage::MSG_REQUEST_GAME_LEAVE;
522-
wcsncpy(msg.name, m_currentGame->getPlayerName(0).str(), g_lanPlayerNameLength);
523-
msg.name[g_lanPlayerNameLength] = 0;
522+
wcslcpy(msg.name, m_currentGame->getPlayerName(0).str(), ARRAY_SIZE(msg.name));
524523
handleRequestGameLeave(&msg, m_currentGame->getIP(0));
525524
UnicodeString text;
526525
text = TheGameText->fetch("LAN:HostNotResponding");
@@ -538,8 +537,7 @@ void LANAPI::update( void )
538537
UnicodeString theStr;
539538
theStr.format(TheGameText->fetch("LAN:PlayerDropped"), m_currentGame->getPlayerName(p).str());
540539
msg.LANMessageType = LANMessage::MSG_REQUEST_GAME_LEAVE;
541-
wcsncpy(msg.name, m_currentGame->getPlayerName(p).str(), g_lanPlayerNameLength);
542-
msg.name[g_lanPlayerNameLength] = 0;
540+
wcslcpy(msg.name, m_currentGame->getPlayerName(p).str(), ARRAY_SIZE(msg.name));
543541
handleRequestGameLeave(&msg, m_currentGame->getIP(p));
544542
OnChat(UnicodeString::TheEmptyString, m_localIP, theStr, LANCHAT_SYSTEM);
545543
}
@@ -670,8 +668,7 @@ void LANAPI::RequestGameJoinDirectConnect(UnsignedInt ipaddress)
670668
msg.LANMessageType = LANMessage::MSG_REQUEST_GAME_INFO;
671669
fillInLANMessage(&msg);
672670
msg.PlayerInfo.ip = GetLocalIP();
673-
wcsncpy(msg.PlayerInfo.playerName, m_name.str(), m_name.getLength());
674-
msg.PlayerInfo.playerName[m_name.getLength()] = 0;
671+
wcslcpy(msg.PlayerInfo.playerName, m_name.str(), ARRAY_SIZE(msg.PlayerInfo.playerName));
675672

676673
sendMessage(&msg, ipaddress);
677674

@@ -684,8 +681,7 @@ void LANAPI::RequestGameLeave( void )
684681
LANMessage msg;
685682
msg.LANMessageType = LANMessage::MSG_REQUEST_GAME_LEAVE;
686683
fillInLANMessage( &msg );
687-
wcsncpy(msg.GameToLeave.gameName, (m_currentGame)?m_currentGame->getName().str():L"", g_lanGameNameLength);
688-
msg.GameToLeave.gameName[g_lanGameNameLength] = 0;
684+
wcslcpy(msg.PlayerInfo.playerName, m_name.str(), ARRAY_SIZE(msg.PlayerInfo.playerName));
689685
sendMessage(&msg);
690686
m_transport->update(); // Send immediately, before OnPlayerLeave below resets everything.
691687

@@ -717,8 +713,7 @@ void LANAPI::RequestGameAnnounce( void )
717713

718714
AsciiString gameOpts = GameInfoToAsciiString(m_currentGame);
719715
strlcpy(reply.GameInfo.options,gameOpts.str(), ARRAY_SIZE(reply.GameInfo.options));
720-
wcsncpy(reply.GameInfo.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
721-
reply.GameInfo.gameName[g_lanGameNameLength] = 0;
716+
wcslcpy(reply.GameInfo.gameName, m_currentGame->getName().str(), ARRAY_SIZE(reply.GameInfo.gameName));
722717
reply.GameInfo.inProgress = m_currentGame->isGameInProgress();
723718
reply.GameInfo.isDirectConnect = m_currentGame->getIsDirectConnect();
724719

@@ -736,8 +731,7 @@ void LANAPI::RequestAccept( void )
736731
fillInLANMessage( &msg );
737732
msg.LANMessageType = LANMessage::MSG_SET_ACCEPT;
738733
msg.Accept.isAccepted = true;
739-
wcsncpy(msg.Accept.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
740-
msg.Accept.gameName[g_lanGameNameLength] = 0;
734+
wcslcpy(msg.Accept.gameName, m_currentGame->getName().str(), ARRAY_SIZE(msg.Accept.gameName));
741735
sendMessage(&msg);
742736
}
743737

@@ -750,8 +744,7 @@ void LANAPI::RequestHasMap( void )
750744
fillInLANMessage( &msg );
751745
msg.LANMessageType = LANMessage::MSG_MAP_AVAILABILITY;
752746
msg.MapStatus.hasMap = m_currentGame->getSlot(m_currentGame->getLocalSlotNum())->hasMap();
753-
wcsncpy(msg.MapStatus.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
754-
msg.MapStatus.gameName[g_lanGameNameLength] = 0;
747+
wcslcpy(msg.MapStatus.gameName, m_currentGame->getName().str(), ARRAY_SIZE(msg.MapStatus.gameName));
755748
CRC mapNameCRC;
756749
//mapNameCRC.computeCRC(m_currentGame->getMap().str(), m_currentGame->getMap().getLength());
757750
AsciiString portableMapName = TheGameState->realMapPathToPortableMapPath(m_currentGame->getMap());
@@ -788,12 +781,10 @@ void LANAPI::RequestChat( UnicodeString message, ChatType format )
788781
{
789782
LANMessage msg;
790783
fillInLANMessage( &msg );
791-
wcsncpy(msg.Chat.gameName, (m_currentGame)?m_currentGame->getName().str():L"", g_lanGameNameLength);
792-
msg.Chat.gameName[g_lanGameNameLength] = 0;
784+
wcslcpy(msg.Chat.gameName, (m_currentGame) ? m_currentGame->getName().str() : L"", ARRAY_SIZE(msg.Chat.gameName));
793785
msg.LANMessageType = LANMessage::MSG_CHAT;
794786
msg.Chat.chatType = format;
795-
wcsncpy(msg.Chat.message, message.str(), g_lanMaxChatLength);
796-
msg.Chat.message[g_lanMaxChatLength] = 0;
787+
wcslcpy(msg.Chat.message, message.str(), ARRAY_SIZE(msg.Chat.message));
797788
sendMessage(&msg);
798789

799790
OnChat(m_name, m_localIP, message, format);
@@ -943,8 +934,7 @@ void LANAPI::RequestGameCreate( UnicodeString gameName, Bool isDirectConnect )
943934
//RequestSlotList();
944935
/*
945936
LANMessage msg;
946-
wcsncpy(msg.name, m_name.str(), g_lanPlayerNameLength);
947-
msg.name[g_lanPlayerNameLength] = 0;
937+
wcslcpy(msg.name, m_name.str(), ARRAY_SIZE(msg.name));
948938
wcscpy(msg.GameInfo.gameName, myGame->getName().str());
949939
for (player=0; player<MAX_SLOTS; ++player)
950940
{
@@ -1015,18 +1005,15 @@ void LANAPI::RequestSlotList( void )
10151005
10161006
LANMessage reply;
10171007
reply.LANMessageType = LANMessage::MSG_GAME_ANNOUNCE;
1018-
wcsncpy(reply.name, m_name.str(), g_lanPlayerNameLength);
1019-
reply.name[g_lanPlayerNameLength] = 0;
1008+
wcslcpy(reply.name, m_name.str(), ARRAY_SIZE(reply.name));
10201009
int player;
10211010
for (player = 0; player < MAX_SLOTS; ++player)
10221011
{
1023-
wcsncpy(reply.GameInfo.name[player], m_currentGame->getPlayerName(player).str(), g_lanPlayerNameLength);
1024-
reply.GameInfo.name[player][g_lanPlayerNameLength] = 0;
1012+
wcslcpy(reply.GameInfo.name[player], m_currentGame->getPlayerName(player).str(), ARRAY_SIZE(reply.GameInfo.name[player]));
10251013
reply.GameInfo.ip[player] = m_currentGame->getIP(player);
10261014
reply.GameInfo.playerAccepted[player] = m_currentGame->getSlot(player)->isAccepted();
10271015
}
1028-
wcsncpy(reply.GameInfo.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
1029-
reply.GameInfo.gameName[g_lanGameNameLength] = 0;
1016+
wcslcpy(reply.GameInfo.gameName, m_currentGame->getName().str(), ARRAY_SIZE(reply.GameInfo.gameName));
10301017
reply.GameInfo.inProgress = m_currentGame->isGameInProgress();
10311018
10321019
sendMessage(&reply);
@@ -1082,8 +1069,7 @@ void LANAPI::fillInLANMessage( LANMessage *msg )
10821069
if (!msg)
10831070
return;
10841071

1085-
wcsncpy(msg->name, m_name.str(), g_lanPlayerNameLength);
1086-
msg->name[g_lanPlayerNameLength] = 0;
1072+
wcslcpy(msg->name, m_name.str(), ARRAY_SIZE(msg->name));
10871073
strlcpy(msg->userName, m_userName.str(), ARRAY_SIZE(msg->userName));
10881074
strlcpy(msg->hostName, m_hostName.str(), ARRAY_SIZE(msg->hostName));
10891075
}

Generals/Code/GameEngine/Source/GameNetwork/LANAPIhandlers.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ void LANAPI::handleRequestLocations( LANMessage *msg, UnsignedInt senderIP )
6363
reply.LANMessageType = LANMessage::MSG_GAME_ANNOUNCE;
6464
AsciiString gameOpts = GenerateGameOptionsString();
6565
strlcpy(reply.GameInfo.options, gameOpts.str(), ARRAY_SIZE(reply.GameInfo.options));
66-
wcsncpy(reply.GameInfo.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
67-
reply.GameInfo.gameName[g_lanGameNameLength] = 0;
66+
wcslcpy(reply.GameInfo.gameName, m_currentGame->getName().str(), ARRAY_SIZE(reply.GameInfo.gameName));
6867
reply.GameInfo.inProgress = m_currentGame->isGameInProgress();
6968

7069
sendMessage(&reply);
@@ -194,8 +193,7 @@ void LANAPI::handleRequestGameInfo( LANMessage *msg, UnsignedInt senderIP )
194193

195194
AsciiString gameOpts = GameInfoToAsciiString(m_currentGame);
196195
strlcpy(reply.GameInfo.options,gameOpts.str(), ARRAY_SIZE(reply.GameInfo.options));
197-
wcsncpy(reply.GameInfo.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
198-
reply.GameInfo.gameName[g_lanGameNameLength] = 0;
196+
wcslcpy(reply.GameInfo.gameName, m_currentGame->getName().str(), ARRAY_SIZE(reply.GameInfo.gameName));
199197
reply.GameInfo.inProgress = m_currentGame->isGameInProgress();
200198
reply.GameInfo.isDirectConnect = m_currentGame->getIsDirectConnect();
201199

@@ -314,8 +312,7 @@ void LANAPI::handleRequestJoin( LANMessage *msg, UnsignedInt senderIP )
314312
{
315313
// OK, add him in.
316314
reply.LANMessageType = LANMessage::MSG_JOIN_ACCEPT;
317-
wcsncpy(reply.GameJoined.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
318-
reply.GameJoined.gameName[g_lanGameNameLength] = 0;
315+
wcslcpy(reply.GameJoined.gameName, m_currentGame->getName().str(), ARRAY_SIZE(reply.GameJoined.gameName));
319316
reply.GameJoined.slotPosition = player;
320317
reply.GameJoined.gameIP = m_localIP;
321318
reply.GameJoined.playerIP = senderIP;
@@ -339,8 +336,7 @@ void LANAPI::handleRequestJoin( LANMessage *msg, UnsignedInt senderIP )
339336
if (canJoin && player == MAX_SLOTS)
340337
{
341338
reply.LANMessageType = LANMessage::MSG_JOIN_DENY;
342-
wcsncpy(reply.GameNotJoined.gameName, m_currentGame->getName().str(), g_lanGameNameLength);
343-
reply.GameNotJoined.gameName[g_lanGameNameLength] = 0;
339+
wcslcpy(reply.GameNotJoined.gameName, m_currentGame->getName().str(), ARRAY_SIZE(reply.GameNotJoined.gameName));
344340
reply.GameNotJoined.reason = LANAPIInterface::RET_GAME_FULL;
345341
reply.GameNotJoined.gameIP = m_localIP;
346342
reply.GameNotJoined.playerIP = senderIP;

GeneralsMD/Code/GameEngine/Include/Common/Language.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ typedef enum
7676
} LanguageID;
7777

7878
#define GameStrcpy wcscpy
79-
#define GameStrncpy wcsncpy
8079
#define GameStrlen wcslen
8180
#define GameStrcat wcscat
8281
#define GameStrcmp wcscmp

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ScoreScreen.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,7 @@ WindowMsgHandledType ScoreScreenSystem( GameWindow *window, UnsignedInt msg,
632632
req.arg.addbuddy.id = playerID;
633633
UnicodeString buddyAddstr;
634634
buddyAddstr = TheGameText->fetch("GUI:BuddyAddReq");
635-
wcsncpy(req.arg.addbuddy.text, buddyAddstr.str(), MAX_BUDDY_CHAT_LEN);
636-
req.arg.addbuddy.text[MAX_BUDDY_CHAT_LEN-1] = 0;
635+
wcslcpy(req.arg.addbuddy.text, buddyAddstr.str(), MAX_BUDDY_CHAT_LEN);
637636
TheGameSpyBuddyMessageQueue->addRequest(req);
638637
}
639638
}

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLBuddyOverlay.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ WindowMsgHandledType BuddyControlSystem( GameWindow *window, UnsignedInt msg,
304304
// Send the message
305305
BuddyRequest req;
306306
req.buddyRequestType = BuddyRequest::BUDDYREQUEST_MESSAGE;
307-
wcsncpy(req.arg.message.text, txtInput.str(), MAX_BUDDY_CHAT_LEN);
308-
req.arg.message.text[MAX_BUDDY_CHAT_LEN-1] = 0;
307+
wcslcpy(req.arg.message.text, txtInput.str(), MAX_BUDDY_CHAT_LEN);
309308
req.arg.message.recipient = selectedProfile;
310309
TheGameSpyBuddyMessageQueue->addRequest(req);
311310

@@ -1185,8 +1184,7 @@ void RequestBuddyAdd(Int profileID, AsciiString nick)
11851184
req.arg.addbuddy.id = profileID;
11861185
UnicodeString buddyAddstr;
11871186
buddyAddstr = TheGameText->fetch("GUI:BuddyAddReq");
1188-
wcsncpy(req.arg.addbuddy.text, buddyAddstr.str(), MAX_BUDDY_CHAT_LEN);
1189-
req.arg.addbuddy.text[MAX_BUDDY_CHAT_LEN-1] = 0;
1187+
wcslcpy(req.arg.addbuddy.text, buddyAddstr.str(), MAX_BUDDY_CHAT_LEN);
11901188
TheGameSpyBuddyMessageQueue->addRequest(req);
11911189

11921190
UnicodeString s;

GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/Thread/BuddyThread.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,7 @@ void BuddyThreadClass::messageCallback( GPConnection *con, GPRecvBuddyMessageArg
512512
gpGetInfo( con, arg->profile, GP_CHECK_CACHE, GP_BLOCKING, (GPCallback)getNickForMessage, &messageResponse);
513513

514514
std::wstring s = MultiByteToWideCharSingleLine( arg->message );
515-
wcsncpy(messageResponse.arg.message.text, s.c_str(), MAX_BUDDY_CHAT_LEN);
516-
messageResponse.arg.message.text[MAX_BUDDY_CHAT_LEN-1] = 0;
515+
wcslcpy(messageResponse.arg.message.text, s.c_str(), MAX_BUDDY_CHAT_LEN);
517516
messageResponse.arg.message.date = arg->date;
518517
DEBUG_LOG(("Got a buddy message from %d [%ls]", arg->profile, s.c_str()));
519518
TheGameSpyBuddyMessageQueue->addResponse( messageResponse );
@@ -629,8 +628,7 @@ void BuddyThreadClass::requestCallback( GPConnection *con, GPRecvBuddyRequestArg
629628
gpGetInfo( con, arg->profile, GP_CHECK_CACHE, GP_BLOCKING, (GPCallback)getInfoResponseForRequest, &response);
630629

631630
std::wstring s = MultiByteToWideCharSingleLine( arg->reason );
632-
wcsncpy(response.arg.request.text, s.c_str(), GP_REASON_LEN);
633-
response.arg.request.text[GP_REASON_LEN-1] = 0;
631+
wcslcpy(response.arg.request.text, s.c_str(), GP_REASON_LEN);
634632

635633
TheGameSpyBuddyMessageQueue->addResponse( response );
636634
}

0 commit comments

Comments
 (0)