Skip to content

Commit 73d1d50

Browse files
authored
Replace SHA256_ZEROED uses with std::optional<SHA256_DIGEST>s (ddnet#11581)
2 parents c7dc609 + 4a33f66 commit 73d1d50

File tree

14 files changed

+86
-77
lines changed

14 files changed

+86
-77
lines changed

src/base/hash.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "hash_ctxt.h"
44
#include "system.h"
55

6-
const SHA256_DIGEST SHA256_ZEROED = {{0}};
7-
86
static void digest_str(const unsigned char *digest, size_t digest_len, char *str, size_t max_len)
97
{
108
if(max_len > digest_len * 2 + 1)

src/base/hash.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ void md5_str(MD5_DIGEST digest, char *str, size_t max_len);
3131
int md5_from_str(MD5_DIGEST *out, const char *str);
3232
int md5_comp(MD5_DIGEST digest1, MD5_DIGEST digest2);
3333

34-
extern const SHA256_DIGEST SHA256_ZEROED;
35-
3634
inline bool operator==(const SHA256_DIGEST &that, const SHA256_DIGEST &other)
3735
{
3836
return sha256_comp(that, other) == 0;

src/engine/client/client.cpp

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ void CClient::Render()
11481148
RenderGraphs();
11491149
}
11501150

1151-
const char *CClient::LoadMap(const char *pName, const char *pFilename, SHA256_DIGEST *pWantedSha256, unsigned WantedCrc)
1151+
const char *CClient::LoadMap(const char *pName, const char *pFilename, const std::optional<SHA256_DIGEST> &WantedSha256, unsigned WantedCrc)
11521152
{
11531153
static char s_aErrorMsg[128];
11541154

@@ -1163,11 +1163,11 @@ const char *CClient::LoadMap(const char *pName, const char *pFilename, SHA256_DI
11631163
return s_aErrorMsg;
11641164
}
11651165

1166-
if(pWantedSha256 && m_pMap->Sha256() != *pWantedSha256)
1166+
if(WantedSha256.has_value() && m_pMap->Sha256() != WantedSha256.value())
11671167
{
11681168
char aWanted[SHA256_MAXSTRSIZE];
11691169
char aGot[SHA256_MAXSTRSIZE];
1170-
sha256_str(*pWantedSha256, aWanted, sizeof(aWanted));
1170+
sha256_str(WantedSha256.value(), aWanted, sizeof(aWanted));
11711171
sha256_str(m_pMap->Sha256(), aGot, sizeof(aWanted));
11721172
str_format(s_aErrorMsg, sizeof(s_aErrorMsg), "map differs from the server. %s != %s", aGot, aWanted);
11731173
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client", s_aErrorMsg);
@@ -1176,7 +1176,7 @@ const char *CClient::LoadMap(const char *pName, const char *pFilename, SHA256_DI
11761176
}
11771177

11781178
// Only check CRC if we don't have the secure SHA256.
1179-
if(!pWantedSha256 && m_pMap->Crc() != WantedCrc)
1179+
if(!WantedSha256.has_value() && m_pMap->Crc() != WantedCrc)
11801180
{
11811181
str_format(s_aErrorMsg, sizeof(s_aErrorMsg), "map differs from the server. %08x != %08x", m_pMap->Crc(), WantedCrc);
11821182
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client", s_aErrorMsg);
@@ -1200,7 +1200,7 @@ const char *CClient::LoadMap(const char *pName, const char *pFilename, SHA256_DI
12001200
return nullptr;
12011201
}
12021202

1203-
static void FormatMapDownloadFilename(const char *pName, const SHA256_DIGEST *pSha256, int Crc, bool Temp, char *pBuffer, int BufferSize)
1203+
static void FormatMapDownloadFilename(const char *pName, const std::optional<SHA256_DIGEST> &Sha256, int Crc, bool Temp, char *pBuffer, int BufferSize)
12041204
{
12051205
char aSuffix[32];
12061206
if(Temp)
@@ -1212,10 +1212,10 @@ static void FormatMapDownloadFilename(const char *pName, const SHA256_DIGEST *pS
12121212
str_copy(aSuffix, ".map");
12131213
}
12141214

1215-
if(pSha256)
1215+
if(Sha256.has_value())
12161216
{
12171217
char aSha256[SHA256_MAXSTRSIZE];
1218-
sha256_str(*pSha256, aSha256, sizeof(aSha256));
1218+
sha256_str(Sha256.value(), aSha256, sizeof(aSha256));
12191219
str_format(pBuffer, BufferSize, "downloadedmaps/%s_%s%s", pName, aSha256, aSuffix);
12201220
}
12211221
else
@@ -1224,37 +1224,37 @@ static void FormatMapDownloadFilename(const char *pName, const SHA256_DIGEST *pS
12241224
}
12251225
}
12261226

1227-
const char *CClient::LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedSha256, int WantedCrc)
1227+
const char *CClient::LoadMapSearch(const char *pMapName, const std::optional<SHA256_DIGEST> &WantedSha256, int WantedCrc)
12281228
{
12291229
char aBuf[512];
12301230
char aWanted[SHA256_MAXSTRSIZE + 16];
12311231
aWanted[0] = 0;
1232-
if(pWantedSha256)
1232+
if(WantedSha256.has_value())
12331233
{
12341234
char aWantedSha256[SHA256_MAXSTRSIZE];
1235-
sha256_str(*pWantedSha256, aWantedSha256, sizeof(aWantedSha256));
1235+
sha256_str(WantedSha256.value(), aWantedSha256, sizeof(aWantedSha256));
12361236
str_format(aWanted, sizeof(aWanted), "sha256=%s ", aWantedSha256);
12371237
}
12381238
str_format(aBuf, sizeof(aBuf), "loading map, map=%s wanted %scrc=%08x", pMapName, aWanted, WantedCrc);
12391239
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client", aBuf);
12401240

12411241
// try the normal maps folder
12421242
str_format(aBuf, sizeof(aBuf), "maps/%s.map", pMapName);
1243-
const char *pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc);
1243+
const char *pError = LoadMap(pMapName, aBuf, WantedSha256, WantedCrc);
12441244
if(!pError)
12451245
return nullptr;
12461246

12471247
// try the downloaded maps
1248-
FormatMapDownloadFilename(pMapName, pWantedSha256, WantedCrc, false, aBuf, sizeof(aBuf));
1249-
pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc);
1248+
FormatMapDownloadFilename(pMapName, WantedSha256, WantedCrc, false, aBuf, sizeof(aBuf));
1249+
pError = LoadMap(pMapName, aBuf, WantedSha256, WantedCrc);
12501250
if(!pError)
12511251
return nullptr;
12521252

12531253
// backward compatibility with old names
1254-
if(pWantedSha256)
1254+
if(WantedSha256.has_value())
12551255
{
1256-
FormatMapDownloadFilename(pMapName, nullptr, WantedCrc, false, aBuf, sizeof(aBuf));
1257-
pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc);
1256+
FormatMapDownloadFilename(pMapName, std::nullopt, WantedCrc, false, aBuf, sizeof(aBuf));
1257+
pError = LoadMap(pMapName, aBuf, WantedSha256, WantedCrc);
12581258
if(!pError)
12591259
return nullptr;
12601260
}
@@ -1264,7 +1264,7 @@ const char *CClient::LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedS
12641264
str_format(aFilename, sizeof(aFilename), "%s.map", pMapName);
12651265
if(Storage()->FindFile(aFilename, "maps", IStorage::TYPE_ALL, aBuf, sizeof(aBuf)))
12661266
{
1267-
pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc);
1267+
pError = LoadMap(pMapName, aBuf, WantedSha256, WantedCrc);
12681268
if(!pError)
12691269
return nullptr;
12701270
}
@@ -1653,15 +1653,15 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket, int Conn, bool Dummy)
16531653

16541654
ResetMapDownload(true);
16551655

1656-
SHA256_DIGEST *pMapSha256 = nullptr;
1656+
std::optional<SHA256_DIGEST> MapSha256;
16571657
const char *pMapUrl = nullptr;
16581658
if(MapDetailsWerePresent && str_comp(m_aMapDetailsName, pMap) == 0 && m_MapDetailsCrc == MapCrc)
16591659
{
1660-
pMapSha256 = &m_MapDetailsSha256;
1660+
MapSha256 = m_MapDetailsSha256;
16611661
pMapUrl = m_aMapDetailsUrl[0] ? m_aMapDetailsUrl : nullptr;
16621662
}
16631663

1664-
if(LoadMapSearch(pMap, pMapSha256, MapCrc) == nullptr)
1664+
if(LoadMapSearch(pMap, MapSha256, MapCrc) == nullptr)
16651665
{
16661666
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client/network", "loading done");
16671667
SetLoadingStateDetail(IClient::LOADING_STATE_DETAIL_SENDING_READY);
@@ -1670,20 +1670,19 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket, int Conn, bool Dummy)
16701670
else
16711671
{
16721672
// start map download
1673-
FormatMapDownloadFilename(pMap, pMapSha256, MapCrc, false, m_aMapdownloadFilename, sizeof(m_aMapdownloadFilename));
1674-
FormatMapDownloadFilename(pMap, pMapSha256, MapCrc, true, m_aMapdownloadFilenameTemp, sizeof(m_aMapdownloadFilenameTemp));
1673+
FormatMapDownloadFilename(pMap, MapSha256, MapCrc, false, m_aMapdownloadFilename, sizeof(m_aMapdownloadFilename));
1674+
FormatMapDownloadFilename(pMap, MapSha256, MapCrc, true, m_aMapdownloadFilenameTemp, sizeof(m_aMapdownloadFilenameTemp));
16751675

16761676
char aBuf[256];
16771677
str_format(aBuf, sizeof(aBuf), "starting to download map to '%s'", m_aMapdownloadFilenameTemp);
16781678
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client/network", aBuf);
16791679

16801680
str_copy(m_aMapdownloadName, pMap);
1681-
m_MapdownloadSha256Present = (bool)pMapSha256;
1682-
m_MapdownloadSha256 = pMapSha256 ? *pMapSha256 : SHA256_ZEROED;
1681+
m_MapdownloadSha256 = MapSha256;
16831682
m_MapdownloadCrc = MapCrc;
16841683
m_MapdownloadTotalsize = MapSize;
16851684

1686-
if(pMapSha256)
1685+
if(MapSha256.has_value())
16871686
{
16881687
char aUrl[256];
16891688
char aEscaped[256];
@@ -1694,7 +1693,7 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket, int Conn, bool Dummy)
16941693
m_pMapdownloadTask = HttpGetFile(pMapUrl ? pMapUrl : aUrl, Storage(), m_aMapdownloadFilenameTemp, IStorage::TYPE_SAVE);
16951694
m_pMapdownloadTask->Timeout(CTimeout{g_Config.m_ClMapDownloadConnectTimeoutMs, 0, g_Config.m_ClMapDownloadLowSpeedLimit, g_Config.m_ClMapDownloadLowSpeedTime});
16961695
m_pMapdownloadTask->MaxResponseSize(MapSize);
1697-
m_pMapdownloadTask->ExpectSha256(*pMapSha256);
1696+
m_pMapdownloadTask->ExpectSha256(MapSha256.value());
16981697
Http()->Run(m_pMapdownloadTask);
16991698
}
17001699
else
@@ -2369,8 +2368,7 @@ void CClient::ResetMapDownload(bool ResetActive)
23692368
if(ResetActive)
23702369
{
23712370
m_MapdownloadChunk = 0;
2372-
m_MapdownloadSha256Present = false;
2373-
m_MapdownloadSha256 = SHA256_ZEROED;
2371+
m_MapdownloadSha256 = std::nullopt;
23742372
m_MapdownloadCrc = 0;
23752373
m_MapdownloadTotalsize = -1;
23762374
m_MapdownloadAmount = 0;
@@ -2384,8 +2382,6 @@ void CClient::FinishMapDownload()
23842382
{
23852383
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client/network", "download complete, loading map");
23862384

2387-
SHA256_DIGEST *pSha256 = m_MapdownloadSha256Present ? &m_MapdownloadSha256 : nullptr;
2388-
23892385
bool FileSuccess = true;
23902386
FileSuccess &= Storage()->RemoveFile(m_aMapdownloadFilename, IStorage::TYPE_SAVE);
23912387
FileSuccess &= Storage()->RenameFile(m_aMapdownloadFilenameTemp, m_aMapdownloadFilename, IStorage::TYPE_SAVE);
@@ -2397,7 +2393,7 @@ void CClient::FinishMapDownload()
23972393
return;
23982394
}
23992395

2400-
const char *pError = LoadMap(m_aMapdownloadName, m_aMapdownloadFilename, pSha256, m_MapdownloadCrc);
2396+
const char *pError = LoadMap(m_aMapdownloadName, m_aMapdownloadFilename, m_MapdownloadSha256, m_MapdownloadCrc);
24012397
if(!pError)
24022398
{
24032399
ResetMapDownload(true);
@@ -3955,9 +3951,7 @@ const char *CClient::DemoPlayer_Play(const char *pFilename, int StorageType)
39553951

39563952
// load map
39573953
const CMapInfo *pMapInfo = m_DemoPlayer.GetMapInfo();
3958-
int Crc = pMapInfo->m_Crc;
3959-
SHA256_DIGEST Sha = pMapInfo->m_Sha256;
3960-
const char *pError = LoadMapSearch(pMapInfo->m_aName, Sha != SHA256_ZEROED ? &Sha : nullptr, Crc);
3954+
const char *pError = LoadMapSearch(pMapInfo->m_aName, pMapInfo->m_Sha256, pMapInfo->m_Crc);
39613955
if(pError)
39623956
{
39633957
if(!m_DemoPlayer.ExtractMap(Storage()))
@@ -3966,8 +3960,7 @@ const char *CClient::DemoPlayer_Play(const char *pFilename, int StorageType)
39663960
return pError;
39673961
}
39683962

3969-
Sha = m_DemoPlayer.GetMapInfo()->m_Sha256;
3970-
pError = LoadMapSearch(pMapInfo->m_aName, &Sha, Crc);
3963+
pError = LoadMapSearch(pMapInfo->m_aName, pMapInfo->m_Sha256, pMapInfo->m_Crc);
39713964
if(pError)
39723965
{
39733966
DisconnectWithReason(pError);

src/engine/client/client.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,12 @@ class CClient : public IClient, public CDemoPlayer::IListener
154154
int m_MapdownloadCrc = 0;
155155
int m_MapdownloadAmount = -1;
156156
int m_MapdownloadTotalsize = -1;
157-
bool m_MapdownloadSha256Present = false;
158-
SHA256_DIGEST m_MapdownloadSha256 = SHA256_ZEROED;
157+
std::optional<SHA256_DIGEST> m_MapdownloadSha256;
159158

160159
bool m_MapDetailsPresent = false;
161160
char m_aMapDetailsName[256] = "";
162161
int m_MapDetailsCrc = 0;
163-
SHA256_DIGEST m_MapDetailsSha256 = SHA256_ZEROED;
162+
SHA256_DIGEST m_MapDetailsSha256;
164163
char m_aMapDetailsUrl[256] = "";
165164

166165
EInfoState m_InfoState = EInfoState::ERROR;
@@ -370,8 +369,8 @@ class CClient : public IClient, public CDemoPlayer::IListener
370369
const char *DummyName() override;
371370
const char *ErrorString() const override;
372371

373-
const char *LoadMap(const char *pName, const char *pFilename, SHA256_DIGEST *pWantedSha256, unsigned WantedCrc);
374-
const char *LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedSha256, int WantedCrc);
372+
const char *LoadMap(const char *pName, const char *pFilename, const std::optional<SHA256_DIGEST> &WantedSha256, unsigned WantedCrc);
373+
const char *LoadMapSearch(const char *pMapName, const std::optional<SHA256_DIGEST> &WantedSha256, int WantedCrc);
375374

376375
int TranslateSysMsg(int *pMsgId, bool System, CUnpacker *pUnpacker, CPacker *pPacker, CNetChunk *pPacket, bool *pIsExMsg);
377376

src/engine/client/serverbrowser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ void CServerBrowser::LoadDDNetInfoJson()
13581358
unsigned Length;
13591359
if(!m_pStorage->ReadFile(DDNET_INFO_FILE, IStorage::TYPE_SAVE, &pBuf, &Length))
13601360
{
1361-
m_DDNetInfoSha256 = SHA256_ZEROED;
1361+
// Keep old info if available
13621362
return;
13631363
}
13641364

@@ -1592,7 +1592,7 @@ void CServerBrowser::LoadDDNetServers()
15921592

15931593
// Add default none community
15941594
{
1595-
CCommunity NoneCommunity(COMMUNITY_NONE, "None", SHA256_ZEROED, "");
1595+
CCommunity NoneCommunity(COMMUNITY_NONE, "None", std::nullopt, "");
15961596
NoneCommunity.m_vCountries.emplace_back(COMMUNITY_COUNTRY_NONE, -1);
15971597
NoneCommunity.m_vTypes.emplace_back(COMMUNITY_TYPE_NONE);
15981598
m_vCommunities.push_back(std::move(NoneCommunity));

src/engine/client/serverbrowser.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <functional>
1414
#include <map>
15+
#include <optional>
1516
#include <set>
1617

1718
typedef struct _json_value json_value;
@@ -221,7 +222,7 @@ class CExcludedCommunityTypeFilterList : public IFilterList
221222
class CCommunityCache : public ICommunityCache
222223
{
223224
IServerBrowser *m_pServerBrowser;
224-
SHA256_DIGEST m_InfoSha256 = SHA256_ZEROED;
225+
std::optional<SHA256_DIGEST> m_InfoSha256;
225226
int m_LastType = IServerBrowser::NUM_TYPES; // initial value does not appear normally, marking uninitialized cache
226227
unsigned m_SelectedCommunitiesHash = 0;
227228
std::vector<const CCommunity *> m_vpSelectedCommunities;
@@ -286,7 +287,7 @@ class CServerBrowser : public IServerBrowser
286287
unsigned CurrentCommunitiesHash() const override;
287288

288289
bool DDNetInfoAvailable() const override { return m_pDDNetInfo != nullptr; }
289-
SHA256_DIGEST DDNetInfoSha256() const override { return m_DDNetInfoSha256; }
290+
std::optional<SHA256_DIGEST> DDNetInfoSha256() const override { return m_DDNetInfoSha256; }
290291

291292
ICommunityCache &CommunityCache() override { return m_CommunityCache; }
292293
const ICommunityCache &CommunityCache() const override { return m_CommunityCache; }
@@ -349,7 +350,7 @@ class CServerBrowser : public IServerBrowser
349350
CExcludedCommunityTypeFilterList m_TypesFilter;
350351

351352
json_value *m_pDDNetInfo = nullptr;
352-
SHA256_DIGEST m_DDNetInfoSha256 = SHA256_ZEROED;
353+
std::optional<SHA256_DIGEST> m_DDNetInfoSha256;
353354

354355
CServerEntry *m_pFirstReqServer; // request list
355356
CServerEntry *m_pLastReqServer;

src/engine/demo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <engine/shared/uuid_manager.h>
1212

1313
#include <cstdint>
14+
#include <optional>
1415

1516
enum
1617
{
@@ -54,7 +55,7 @@ struct CTimelineMarkers
5455
struct CMapInfo
5556
{
5657
char m_aName[MAX_MAP_LENGTH];
57-
SHA256_DIGEST m_Sha256;
58+
std::optional<SHA256_DIGEST> m_Sha256;
5859
unsigned m_Crc;
5960
unsigned m_Size;
6061
};

src/engine/serverbrowser.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <generated/protocol7.h>
1515

16+
#include <optional>
1617
#include <unordered_set>
1718
#include <vector>
1819

@@ -213,15 +214,15 @@ class CCommunity
213214

214215
char m_aId[CServerInfo::MAX_COMMUNITY_ID_LENGTH];
215216
char m_aName[64];
216-
SHA256_DIGEST m_IconSha256;
217+
std::optional<SHA256_DIGEST> m_IconSha256;
217218
char m_aIconUrl[128];
218219
std::vector<CCommunityCountry> m_vCountries;
219220
std::vector<CCommunityType> m_vTypes;
220221
bool m_HasFinishes = false;
221222
std::unordered_set<CCommunityMap, CCommunityMap::SHash> m_FinishedMaps;
222223

223224
public:
224-
CCommunity(const char *pId, const char *pName, SHA256_DIGEST IconSha256, const char *pIconUrl) :
225+
CCommunity(const char *pId, const char *pName, std::optional<SHA256_DIGEST> IconSha256, const char *pIconUrl) :
225226
m_IconSha256(IconSha256)
226227
{
227228
str_copy(m_aId, pId);
@@ -232,7 +233,7 @@ class CCommunity
232233
const char *Id() const { return m_aId; }
233234
const char *Name() const { return m_aName; }
234235
const char *IconUrl() const { return m_aIconUrl; }
235-
const SHA256_DIGEST &IconSha256() const { return m_IconSha256; }
236+
const std::optional<SHA256_DIGEST> &IconSha256() const { return m_IconSha256; }
236237
const std::vector<CCommunityCountry> &Countries() const { return m_vCountries; }
237238
const std::vector<CCommunityType> &Types() const { return m_vTypes; }
238239
bool HasCountry(const char *pCountryName) const;
@@ -361,7 +362,7 @@ class IServerBrowser : public IInterface
361362
virtual unsigned CurrentCommunitiesHash() const = 0;
362363

363364
virtual bool DDNetInfoAvailable() const = 0;
364-
virtual SHA256_DIGEST DDNetInfoSha256() const = 0;
365+
virtual std::optional<SHA256_DIGEST> DDNetInfoSha256() const = 0;
365366

366367
virtual ICommunityCache &CommunityCache() = 0;
367368
virtual const ICommunityCache &CommunityCache() const = 0;

0 commit comments

Comments
 (0)