Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 64e3320

Browse files
committed
Fix bad usage of sizeOfBuffer parameter in sprintf_s and strcpy_s - was crashing debug builds
Revert to strcpy in GUIProperties::GetValue - strcpy_s nor strncpy/_s seem to work here
1 parent bda31f2 commit 64e3320

31 files changed

+173
-174
lines changed

Activities/GATutorial.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ int GATutorial::Start()
308308
{
309309
// See if there are specified landing zone areas defined in the scene
310310
char str[64];
311-
sprintf_s(str, c_PrintBufferSize, "LZ Team %d", team + 1);
311+
sprintf_s(str, sizeof(str), "LZ Team %d", team + 1);
312312
Scene::Area *pArea = g_SceneMan.GetScene()->GetArea(str);
313313
// pArea = pArea ? pArea : g_SceneMan.GetScene()->GetArea("Landing Zone");
314314
// If area is defined, save a copy so we can lock the LZ selection to within its boxes

Activities/GameActivity.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ int GameActivity::Start()
866866
/* This is taken care of by the individual Activity logic
867867
// See if there are specified landing zone areas defined in the scene
868868
char str[64];
869-
sprintf_s(str, c_PrintBufferSize, "LZ Team %d", team + 1);
869+
sprintf_s(str, sizeof(str), "LZ Team %d", team + 1);
870870
Scene::Area *pArea = g_SceneMan.GetScene()->GetArea(str);
871871
pArea = pArea ? pArea : g_SceneMan.GetScene()->GetArea("Landing Zone");
872872
// If area is defined, save a copy so we can lock the LZ selection to within its boxes
@@ -2071,7 +2071,7 @@ void GameActivity::Update()
20712071
if (m_MsgTimer[player].IsPastSimMS(1000))
20722072
{
20732073
char message[512];
2074-
sprintf_s(message, c_PrintBufferSize, "Next delivery in %i secs", ((int)m_Deliveries[team].front().delay - (int)m_Deliveries[team].front().timer.GetElapsedSimTimeMS()) / 1000);
2074+
sprintf_s(message, sizeof(message), "Next delivery in %i secs", ((int)m_Deliveries[team].front().delay - (int)m_Deliveries[team].front().timer.GetElapsedSimTimeMS()) / 1000);
20752075
g_FrameMan.SetScreenText(message, ScreenOfPlayer(player));
20762076
m_MsgTimer[player].Reset();
20772077
}
@@ -2214,9 +2214,9 @@ void GameActivity::DrawGUI(BITMAP *pTargetBitmap, const Vector &targetPos, int w
22142214
// Text
22152215
pSmallFont->DrawAligned(&pBitmapInt, landZone.m_X, landZone.m_Y - 38, "ETA:", GUIFont::Centre);
22162216
if (m_ActivityState == Activity::PREGAME)
2217-
sprintf_s(str, c_PrintBufferSize, "???s");
2217+
sprintf_s(str, sizeof(str), "???s");
22182218
else
2219-
sprintf_s(str, c_PrintBufferSize, "%is", ((int)itr->delay - (int)itr->timer.GetElapsedSimTimeMS()) / 1000);
2219+
sprintf_s(str, sizeof(str), "%is", ((int)itr->delay - (int)itr->timer.GetElapsedSimTimeMS()) / 1000);
22202220
pLargeFont->DrawAligned(&pBitmapInt, landZone.m_X, landZone.m_Y - 32, str, GUIFont::Centre);
22212221
// Draw wrap around the world if necessary, and only if this is being drawn directly to a scenewide target bitmap
22222222
if (targetPos.IsZero() && (landZone.m_X < halfWidth || landZone.m_X > g_SceneMan.GetSceneWidth() - halfWidth))
@@ -2336,7 +2336,7 @@ void GameActivity::DrawGUI(BITMAP *pTargetBitmap, const Vector &targetPos, int w
23362336
if (pIcon)
23372337
draw_sprite(pTargetBitmap, pIcon->GetBitmaps8()[0], MAX(2, g_SceneMan.GetScreenOcclusion(which).m_X + 2), 2);
23382338
// Gold
2339-
sprintf_s(str, c_PrintBufferSize, "%c Funds: %.0f oz", TeamFundsChanged(which) ? -57 : -58, GetTeamFunds(m_Team[PoS]));
2339+
sprintf_s(str, sizeof(str), "%c Funds: %.0f oz", TeamFundsChanged(which) ? -57 : -58, GetTeamFunds(m_Team[PoS]));
23402340
g_FrameMan.GetLargeFont()->DrawAligned(&pBitmapInt, MAX(16, g_SceneMan.GetScreenOcclusion(which).m_X + 16), yTextPos, str, GUIFont::Left);
23412341
/* Not applicable anymore to the 4-team games
23422342
// Body losses
@@ -2608,9 +2608,9 @@ void GameActivity::Draw(BITMAP *pTargetBitmap, const Vector &targetPos)
26082608
// Text
26092609
pSmallFont->DrawAligned(&pBitmapInt, landZone.m_X, landZone.m_Y - 38, "ETA:", GUIFont::Centre);
26102610
if (m_ActivityState == Activity::PREGAME)
2611-
sprintf_s(str, c_PrintBufferSize, "???s");
2611+
sprintf_s(str, sizeof(str), "???s");
26122612
else
2613-
sprintf_s(str, c_PrintBufferSize, "%is", ((int)itr->delay - (int)itr->timer.GetElapsedSimTimeMS()) / 1000);
2613+
sprintf_s(str, sizeof(str), "%is", ((int)itr->delay - (int)itr->timer.GetElapsedSimTimeMS()) / 1000);
26142614
pLargeFont->DrawAligned(&pBitmapInt, landZone.m_X, landZone.m_Y - 32, str, GUIFont::Centre);
26152615
// Draw wrap around the world if necessary, and only if this is being drawn directly to a scenewide target bitmap
26162616
if (targetPos.IsZero() && (landZone.m_X < halfWidth || landZone.m_X > g_SceneMan.GetSceneWidth() - halfWidth))

Activities/MultiplayerServerLobby.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ namespace RTE {
261261
for (int team = Activity::TEAM_1; team < TEAMROWCOUNT; ++team)
262262
{
263263
// +1 because the controls are indexed starting at 1, not 0
264-
sprintf_s(str, c_PrintBufferSize, "P%dT%dBox", player + 1, team + 1);
264+
sprintf_s(str, sizeof(str), "P%dT%dBox", player + 1, team + 1);
265265
m_aapPlayerBoxes[player][team] = dynamic_cast<GUICollectionBox *>(m_pGUIController->GetControl(str));
266266
}
267267
}
@@ -771,7 +771,7 @@ namespace RTE {
771771
if (!pIcon)
772772
{
773773
char str[128];
774-
sprintf_s(str, c_PrintBufferSize, "Team %d Default", team + 1);
774+
sprintf_s(str, sizeof(str), "Team %d Default", team + 1);
775775
pIcon = dynamic_cast<const Icon *>(g_PresetMan.GetEntityPreset("Icon", str));
776776
}
777777
m_apTeamNameLabels[team]->SetText(pActivity->GetTeamName(team) + ":");
@@ -834,7 +834,7 @@ namespace RTE {
834834
m_pStartScenarioButton->SetVisible(false);
835835
m_pStartErrorLabel->SetVisible(true);
836836
char str[256];
837-
sprintf_s(str, c_PrintBufferSize, "Too many players assigned! Max for this activity is %d", pGameActivity->GetMaxPlayerSupport());
837+
sprintf_s(str, sizeof(str), "Too many players assigned! Max for this activity is %d", pGameActivity->GetMaxPlayerSupport());
838838
m_pStartErrorLabel->SetText(str);
839839
}
840840
// If we are under the required number of teams with players assigned, disable the start button and show why
@@ -843,7 +843,7 @@ namespace RTE {
843843
m_pStartScenarioButton->SetVisible(false);
844844
m_pStartErrorLabel->SetVisible(true);
845845
char str[256];
846-
sprintf_s(str, c_PrintBufferSize, "Assign players to at\nleast %d of the teams!", pGameActivity->GetMinTeamsRequired());
846+
sprintf_s(str, sizeof(str), "Assign players to at\nleast %d of the teams!", pGameActivity->GetMinTeamsRequired());
847847
m_pStartErrorLabel->SetText(str);
848848
}
849849
// Assign at least one human player
@@ -866,9 +866,9 @@ namespace RTE {
866866
int startGold = m_pGoldSlider->GetValue();
867867
startGold = startGold - startGold % 500;
868868
if (m_pGoldSlider->GetValue() == m_pGoldSlider->GetMaximum())
869-
sprintf_s(str, c_PrintBufferSize, "Starting Gold: %c Infinite", -58);
869+
sprintf_s(str, sizeof(str), "Starting Gold: %c Infinite", -58);
870870
else
871-
sprintf_s(str, c_PrintBufferSize, "Starting Gold: %c %d oz", -58, startGold);
871+
sprintf_s(str, sizeof(str), "Starting Gold: %c %d oz", -58, startGold);
872872
m_pGoldLabel->SetText(str);
873873

874874

Constants.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace RTE {
77
constexpr static unsigned int c_MaxFileName = 256; ///! Maximum length of output file directory+name string.
88
constexpr static unsigned int c_FileBufferSize = 8192; ///! Buffer to hold data read from the zip file.
99
constexpr static unsigned int c_MaxUnzippedFileSize = 104857600; ///! Maximum size of single file being extracted from zip archive.
10-
constexpr static unsigned int c_PrintBufferSize = 512; ///! Maximum number of bytes to be used in the print buffer.
1110
#pragma endregion
1211

1312
#pragma region Graphics Constants

Entities/ACrab.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,19 +3269,19 @@ void ACrab::DrawHUD(BITMAP *pTargetBitmap, const Vector &targetPos, int whichScr
32693269
str[0] = -56; str[1] = 0;
32703270
pSymbolFont->DrawAligned(&allegroBitmap, drawPos.m_X - 10, drawPos.m_Y + m_HUDStack, str, GUIFont::Left);
32713271
if (pHeldFirearm->IsReloading())
3272-
sprintf_s(str, c_PrintBufferSize, "%s", "Reloading...");
3272+
sprintf_s(str, sizeof(str), "%s", "Reloading...");
32733273
else if (pHeldFirearm->GetRoundInMagCount() >= 0)
3274-
sprintf_s(str, c_PrintBufferSize, "%i", pHeldFirearm->GetRoundInMagCount());
3274+
sprintf_s(str, sizeof(str), "%i", pHeldFirearm->GetRoundInMagCount());
32753275
else
3276-
sprintf_s(str, c_PrintBufferSize, "%s", "INF");
3276+
sprintf_s(str, sizeof(str), "%s", "INF");
32773277
pSmallFont->DrawAligned(&allegroBitmap, drawPos.m_X - 0, drawPos.m_Y + m_HUDStack + 3, str, GUIFont::Left);
32783278

32793279
m_HUDStack += -10;
32803280
}
32813281
}
32823282
else
32833283
{
3284-
sprintf_s(str, c_PrintBufferSize, "NO TURRET!");
3284+
sprintf_s(str, sizeof(str), "NO TURRET!");
32853285
pSmallFont->DrawAligned(&allegroBitmap, drawPos.m_X + 2, drawPos.m_Y + m_HUDStack + 3, str, GUIFont::Centre);
32863286
m_HUDStack += -9;
32873287
}

Entities/AHuman.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,13 +4908,13 @@ void AHuman::DrawHUD(BITMAP *pTargetBitmap, const Vector &targetPos, int whichSc
49084908
str[0] = -56; str[1] = 0;
49094909
pSymbolFont->DrawAligned(&allegroBitmap, drawPos.m_X - 10, drawPos.m_Y + m_HUDStack, str, GUIFont::Left);
49104910
if (pHeldFirearm->IsReloading())
4911-
sprintf_s(str, c_PrintBufferSize, "%s", "Reloading...");
4911+
sprintf_s(str, sizeof(str), "%s", "Reloading...");
49124912
else
49134913
{
49144914
if (pHeldFirearm->GetRoundInMagCount() < 0)
4915-
sprintf_s(str, c_PrintBufferSize, "%s", "Infinite");
4915+
sprintf_s(str, sizeof(str), "%s", "Infinite");
49164916
else
4917-
sprintf_s(str, c_PrintBufferSize, "%i", pHeldFirearm->GetRoundInMagCount());
4917+
sprintf_s(str, sizeof(str), "%i", pHeldFirearm->GetRoundInMagCount());
49184918
}
49194919
pSmallFont->DrawAligned(&allegroBitmap, drawPos.m_X - 0, drawPos.m_Y + m_HUDStack + 3, str, GUIFont::Left);
49204920

@@ -4965,7 +4965,7 @@ void AHuman::DrawHUD(BITMAP *pTargetBitmap, const Vector &targetPos, int whichSc
49654965
}
49664966
else
49674967
{
4968-
sprintf_s(str, c_PrintBufferSize, "NO ARM!");
4968+
sprintf_s(str, sizeof(str), "NO ARM!");
49694969
pSmallFont->DrawAligned(&allegroBitmap, drawPos.m_X + 2, drawPos.m_Y + m_HUDStack + 3, str, GUIFont::Centre);
49704970
m_HUDStack += -9;
49714971
}
@@ -4976,7 +4976,7 @@ void AHuman::DrawHUD(BITMAP *pTargetBitmap, const Vector &targetPos, int whichSc
49764976
if (m_pItemInReach && g_MovableMan.IsDevice(m_pItemInReach) && m_pFGArm && m_pFGArm->IsAttached())
49774977
{
49784978
// sprintf_s(str, " œ Pick up %s", m_pItemInReach->GetPresetName().c_str());
4979-
sprintf_s(str, c_PrintBufferSize, " %c %s", -49, m_pItemInReach->GetPresetName().c_str());
4979+
sprintf_s(str, sizeof(str), " %c %s", -49, m_pItemInReach->GetPresetName().c_str());
49804980
pSmallFont->DrawAligned(&allegroBitmap, drawPos.m_X - 12, drawPos.m_Y + m_HUDStack + 3, str, GUIFont::Left);
49814981
}
49824982
else

Entities/Actor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,7 @@ void Actor::DrawHUD(BITMAP *pTargetBitmap, const Vector &targetPos, int whichScr
18991899
pSymbolFont->DrawAligned(&bitmapInt, drawPos.m_X - 11, drawPos.m_Y + m_HUDStack, str, GUIFont::Left);
19001900
}
19011901
*/
1902-
sprintf_s(str, c_PrintBufferSize, "%.0f", m_Health);
1902+
sprintf_s(str, sizeof(str), "%.0f", m_Health);
19031903
// pSmallFont->DrawAligned(&bitmapInt, drawPos.m_X - 0, drawPos.m_Y - 35, str, GUIFont::Left);
19041904
pSymbolFont->DrawAligned(&bitmapInt, drawPos.m_X - 0, drawPos.m_Y + m_HUDStack, str, GUIFont::Left);
19051905

@@ -1909,7 +1909,7 @@ void Actor::DrawHUD(BITMAP *pTargetBitmap, const Vector &targetPos, int whichScr
19091909
if (GetGoldCarried() > 0) {
19101910
str[0] = m_GoldPicked ? -57 : -58; str[1] = 0;
19111911
pSymbolFont->DrawAligned(&bitmapInt, drawPos.m_X - 11, drawPos.m_Y + m_HUDStack, str, GUIFont::Left);
1912-
sprintf_s(str, c_PrintBufferSize, "%.0f oz", GetGoldCarried());
1912+
sprintf_s(str, sizeof(str), "%.0f oz", GetGoldCarried());
19131913
pSmallFont->DrawAligned(&bitmapInt, drawPos.m_X - 0, drawPos.m_Y + m_HUDStack + 2, str, GUIFont::Left);
19141914

19151915
m_HUDStack += -11;

Entities/BunkerAssembly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ int BunkerAssembly::ReadProperty(std::string propName, Reader &reader)
260260
} else {
261261
// Do not allow to define assemblies prior to corresponding assembly scheme
262262
char s[256];
263-
sprintf_s(s, c_PrintBufferSize, "Required BunkerAssemblyScheme '%s%' not found when trying to load BunkerAssembly '%s'! BunkerAssemblySchemes MUST be defined before dependent BunkerAssmeblies.", parentScheme.c_str(), m_PresetName.c_str());
263+
sprintf_s(s, sizeof(s), "Required BunkerAssemblyScheme '%s%' not found when trying to load BunkerAssembly '%s'! BunkerAssemblySchemes MUST be defined before dependent BunkerAssmeblies.", parentScheme.c_str(), m_PresetName.c_str());
264264
DDTAbort(s);
265265
}
266266
} else

Entities/HeldDevice.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ void HeldDevice::DrawHUD(BITMAP *pTargetBitmap, const Vector &targetPos, int whi
544544

545545
pSymbolFont->DrawAligned(&pBitmapInt, drawPos.m_X - 1, drawPos.m_Y - 20, str, GUIFont::Centre);
546546
// sprintf_s(str, "%.0f", m_PresetName);
547-
sprintf_s(str, c_PrintBufferSize, "%s", m_PresetName.c_str());
547+
sprintf_s(str, sizeof(str), "%s", m_PresetName.c_str());
548548
// sprintf_s(str, "%s", m_sClass.GetName().c_str());
549549
pTextFont->DrawAligned(&pBitmapInt, drawPos.m_X + 0, drawPos.m_Y - 29, str, GUIFont::Centre);
550550
}

Entities/Scene.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ int Scene::SaveData(string pathBase)
10291029
{
10301030
if (m_apUnseenLayer[team])
10311031
{
1032-
sprintf_s(str, c_PrintBufferSize, "T%d", team);
1032+
sprintf_s(str, sizeof(str), "T%d", team);
10331033
// Save unseen layer data to disk
10341034
if (m_apUnseenLayer[team]->SaveData(pathBase + " US" + str + ".bmp") < 0)
10351035
{
@@ -1446,7 +1446,7 @@ int Scene::Save(Writer &writer) const
14461446
{
14471447
if (m_ResidentBrains[player])
14481448
{
1449-
sprintf_s(str, c_PrintBufferSize, "P%dResidentBrain", player + 1);
1449+
sprintf_s(str, sizeof(str), "P%dResidentBrain", player + 1);
14501450
writer.NewProperty(str);
14511451
writer.ObjectStart(m_ResidentBrains[player]->GetClassName());
14521452
writer.NewProperty("CopyOf");

0 commit comments

Comments
 (0)