Skip to content

Commit 469bc1c

Browse files
committed
Change atoi64 to ParseInt64
1 parent e6a98f5 commit 469bc1c

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

src/gridcoin/scraper/scraper.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,20 +1914,18 @@ bool ProcessProjectTeamFile(const std::string& project, const fs::path& file, co
19141914

19151915
int64_t nTeamID = 0;
19161916

1917-
try
1918-
{
1919-
nTeamID = atoi64(sTeamID);
1920-
}
1921-
catch (const std::exception&)
1917+
if (!ParseInt64(sTeamID, &nTeamID))
19221918
{
1923-
_log(logattribute::ERR, "ProccessProjectTeamFile", tfm::format("Ignoring bad team id for team %s.", sTeamName));
1919+
_log(logattribute::ERR, __func__, tfm::format("Ignoring bad team id for team %s.", sTeamName));
19241920
continue;
19251921
}
19261922

19271923
mTeamIdsForProject[sTeamName] = nTeamID;
19281924
}
19291925
else
1926+
{
19301927
builder.append(line);
1928+
}
19311929
}
19321930

19331931
if (mTeamIdsForProject.empty())
@@ -2319,13 +2317,9 @@ bool ProcessProjectRacFileByCPID(const std::string& project, const fs::path& fil
23192317
const std::string& sTeamID = ExtractXML(data, "<teamid>", "</teamid>");
23202318
int64_t nTeamID = 0;
23212319

2322-
try
2320+
if (!ParseInt64(sTeamID, &nTeamID))
23232321
{
2324-
nTeamID = atoi64(sTeamID);
2325-
}
2326-
catch (const std::exception&)
2327-
{
2328-
_log(logattribute::ERR, "ProcessProjectRacFileByCPID", "Bad team id in user stats file data.");
2322+
_log(logattribute::ERR, __func__, "Bad team id in user stats file data.");
23292323
continue;
23302324
}
23312325

@@ -2585,13 +2579,9 @@ bool LoadTeamIDList(const fs::path& file)
25852579
sProject = iter;
25862580
else
25872581
{
2588-
try
2589-
{
2590-
nTeamID = atoi64(iter);
2591-
}
2592-
catch (std::exception&)
2582+
if (!ParseInt64(iter, &nTeamID))
25932583
{
2594-
_log(logattribute::ERR, "LoadTeamIDList", "Ignoring invalid team id found in team id file.");
2584+
_log(logattribute::ERR, __func__, "Ignoring invalid team id found in team id file.");
25952585
continue;
25962586
}
25972587

src/gridcoin/staking/reward.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ CAmount GRC::GetConstantBlockReward(const CBlockIndex* index)
4646
//TODO: refactor the expire checking to subroutine
4747
//Note: time constant is same as GetBeaconPublicKey
4848
if ((index->nTime - oCBReward.timestamp) <= (60 * 24 * 30 * 6 * 60)) {
49-
reward = atoi64(oCBReward.value);
49+
// This is a little slippery, because if we ever change CAmount from a int64_t, this will
50+
// break. It is unlikely to ever change, however, and avoids an extra copy/implicit cast.
51+
if (!ParseInt64(oCBReward.value, &reward)) {
52+
error("%s: Cannot parse constant block reward from protocol entry: %s",
53+
__func__, oCBReward.value);
54+
}
5055
}
5156

5257
reward = std::clamp(reward, MIN_CBR, MAX_CBR);

src/util.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,12 @@ bool ParseMoney(const char* pszIn, int64_t& nRet)
245245
return false;
246246
if (nUnits < 0 || nUnits > COIN)
247247
return false;
248-
int64_t nWhole = atoi64(strWhole);
248+
249+
int64_t nWhole = 0;
250+
251+
// Because of the protection above, this assert should never fail.
252+
assert(!ParseInt64(strWhole, &nWhole));
253+
249254
int64_t nValue = nWhole*COIN + nUnits;
250255

251256
nRet = nValue;

src/util/system.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,24 @@ std::string ArgsManager::GetArg(const std::string& strArg, const std::string& st
496496
int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault) const
497497
{
498498
const util::SettingsValue value = GetSetting(strArg);
499-
return value.isNull() ? nDefault : value.isFalse() ? 0 : value.isTrue() ? 1 : value.isNum() ? value.get_int64() : atoi64(value.get_str());
499+
500+
int64_t arg_value = 0;
501+
502+
if (value.isNull()) {
503+
arg_value = nDefault;
504+
} else if (value.isFalse()) {
505+
arg_value = 0;
506+
} else if (value.isTrue()) {
507+
arg_value = 1;
508+
} else if (value.isNum()) {
509+
arg_value = value.get_int64();
510+
} else {
511+
if (!ParseInt64(value.get_str(), &arg_value)) {
512+
// Do nothing. If we get here and the string cannot be parsed, it should return zero, just like atoi64.
513+
}
514+
}
515+
516+
return arg_value;
500517
}
501518

502519
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const

src/wallet/wallet.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,12 @@ static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
451451
nOrderPos = -1; // TODO : calculate elsewhere
452452
return;
453453
}
454-
nOrderPos = atoi64(mapValue["n"].c_str());
454+
455+
nOrderPos = 0;
456+
if (!ParseInt64(mapValue["n"], &nOrderPos))
457+
{
458+
error("%s: nOrderPos cannot be parsed: %s.", __func__, mapValue["n"]);
459+
}
455460
}
456461

457462

@@ -601,9 +606,9 @@ class CWalletTx : public CMerkleTx
601606

602607
ReadOrderPos(nOrderPos, mapValue);
603608

604-
nTimeSmart = mapValue.count("timesmart")
605-
? (unsigned int)atoi64(mapValue["timesmart"])
606-
: 0;
609+
if (!mapValue.count("timesmart") || !ParseUInt(mapValue["timesmart"], &nTimeSmart)) {
610+
nTimeSmart = 0;
611+
}
607612
}
608613

609614
mapValue.erase("fromaccount");

0 commit comments

Comments
 (0)