U8toU16 now respects buffer sizes

This commit is contained in:
CPunch 2020-10-04 12:50:58 -05:00
parent 5015e2575d
commit 755bb75306
6 changed files with 11 additions and 8 deletions

View File

@ -135,7 +135,7 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
} }
} else { } else {
INITSTRUCT(sP_LS2CL_REP_LOGIN_FAIL, resp); INITSTRUCT(sP_LS2CL_REP_LOGIN_FAIL, resp);
U8toU16(userLogin, resp.szID); U8toU16(userLogin, resp.szID, sizeof(resp.szID));
resp.iErrorCode = errorCode; resp.iErrorCode = errorCode;
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_FAIL, sizeof(sP_LS2CL_REP_LOGIN_FAIL)); sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_FAIL, sizeof(sP_LS2CL_REP_LOGIN_FAIL));
} }

View File

@ -31,7 +31,7 @@
// TODO: rewrite U16toU8 & U8toU16 to not use codecvt // TODO: rewrite U16toU8 & U8toU16 to not use codecvt
std::string U16toU8(char16_t* src); std::string U16toU8(char16_t* src);
size_t U8toU16(std::string src, char16_t* des); // returns number of char16_t that was written at des size_t U8toU16(std::string src, char16_t* des, size_t max); // returns number of char16_t that was written at des
time_t getTime(); time_t getTime();
time_t getTimestamp(); time_t getTimestamp();

View File

@ -246,7 +246,7 @@ void ChatManager::sendServerMessage(CNSocket* sock, std::string msg) {
motd.iType = 1; motd.iType = 1;
// convert string to u16 and write it to the buffer (TODO: add sanity check to prevent buffer overflow) // convert string to u16 and write it to the buffer (TODO: add sanity check to prevent buffer overflow)
U8toU16(msg, (char16_t*)motd.szSystemMsg); U8toU16(msg, (char16_t*)motd.szSystemMsg, sizeof(motd.szSystemMsg));
// send the packet :) // send the packet :)
sock->sendPacket((void*)&motd, P_FE2CL_PC_MOTD_LOGIN, sizeof(sP_FE2CL_PC_MOTD_LOGIN)); sock->sendPacket((void*)&motd, P_FE2CL_PC_MOTD_LOGIN, sizeof(sP_FE2CL_PC_MOTD_LOGIN));

View File

@ -425,14 +425,14 @@ Player Database::DbToPlayer(DbPlayer player) {
result.PCStyle.iClass = player.Class; result.PCStyle.iClass = player.Class;
result.PCStyle.iEyeColor = player.EyeColor; result.PCStyle.iEyeColor = player.EyeColor;
result.PCStyle.iFaceStyle = player.FaceStyle; result.PCStyle.iFaceStyle = player.FaceStyle;
U8toU16(player.FirstName, result.PCStyle.szFirstName); U8toU16(player.FirstName, result.PCStyle.szFirstName, sizeof(result.PCStyle.szFirstName));
result.PCStyle.iGender = player.Gender; result.PCStyle.iGender = player.Gender;
result.PCStyle.iHairColor = player.HairColor; result.PCStyle.iHairColor = player.HairColor;
result.PCStyle.iHairStyle = player.HairStyle; result.PCStyle.iHairStyle = player.HairStyle;
result.PCStyle.iHeight = player.Height; result.PCStyle.iHeight = player.Height;
result.HP = player.HP; result.HP = player.HP;
result.accountLevel = player.AccountLevel; result.accountLevel = player.AccountLevel;
U8toU16(player.LastName, result.PCStyle.szLastName); U8toU16(player.LastName, result.PCStyle.szLastName, sizeof(result.PCStyle.szLastName));
result.level = player.Level; result.level = player.Level;
result.PCStyle.iNameCheck = player.NameCheck; result.PCStyle.iNameCheck = player.NameCheck;
result.PCStyle2.iPayzoneFlag = player.PayZoneFlag; result.PCStyle2.iPayzoneFlag = player.PayZoneFlag;

View File

@ -162,7 +162,7 @@ void TransportManager::transportWarpHandler(CNSocket* sock, CNPacketData* data)
INITSTRUCT(sP_FE2CL_ANNOUNCE_MSG, alert); INITSTRUCT(sP_FE2CL_ANNOUNCE_MSG, alert);
alert.iAnnounceType = 0; // don't think this lets us make a confirm dialog alert.iAnnounceType = 0; // don't think this lets us make a confirm dialog
alert.iDuringTime = 3; alert.iDuringTime = 3;
U8toU16("Skyway route " + std::to_string(route.mssRouteNum) + " isn't pathed yet. You will not be charged any taros.", (char16_t*)alert.szAnnounceMsg); U8toU16("Skyway route " + std::to_string(route.mssRouteNum) + " isn't pathed yet. You will not be charged any taros.", (char16_t*)alert.szAnnounceMsg, sizeof(alert.szAnnounceMsg));
sock->sendPacket((void*)&alert, P_FE2CL_ANNOUNCE_MSG, sizeof(sP_FE2CL_ANNOUNCE_MSG)); sock->sendPacket((void*)&alert, P_FE2CL_ANNOUNCE_MSG, sizeof(sP_FE2CL_ANNOUNCE_MSG));
std::cout << "[WARN] MSS route " << route.mssRouteNum << " not pathed" << std::endl; std::cout << "[WARN] MSS route " << route.mssRouteNum << " not pathed" << std::endl;

View File

@ -131,12 +131,15 @@ std::string U16toU8(char16_t* src) {
} }
// returns number of char16_t that was written at des // returns number of char16_t that was written at des
size_t U8toU16(std::string src, char16_t* des) { size_t U8toU16(std::string src, char16_t* des, size_t max) {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::u16string tmp = convert.from_bytes(src); std::u16string tmp = convert.from_bytes(src);
// copy utf16 string to buffer // copy utf16 string to buffer
memcpy(des, tmp.c_str(), sizeof(char16_t) * tmp.length()); if (sizeof(char16_t) * tmp.length() > max) // make sure we don't write outside the buffer
memcpy(des, tmp.c_str(), sizeof(char16_t) * max);
else
memcpy(des, tmp.c_str(), sizeof(char16_t) * tmp.length());
des[tmp.length()] = '\0'; des[tmp.length()] = '\0';
return tmp.length(); return tmp.length();