Skip to content

Commit 044834d

Browse files
committed
Change atoi to ParseInt
1 parent 070100f commit 044834d

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

src/gridcoin/staking/kernel.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int64_t GetRSAWeightByBlock(const std::string& bb)
140140
constexpr size_t rsa_weight_offset = 13;
141141
constexpr size_t magnitude_offset = 15;
142142

143-
int64_t rsa_weight = 0;
143+
int64_t rsa_weight_sum = 0;
144144

145145
// General-purpose deserialization of claim contexts in the hashBoinc field
146146
// no longer parses out the RSA weight field, so we handle the special case
@@ -154,18 +154,25 @@ int64_t GetRSAWeightByBlock(const std::string& bb)
154154
if (n == cpid_offset && end - offset != 32) {
155155
return 0;
156156
} else if (n == rsa_weight_offset || n == magnitude_offset) {
157-
rsa_weight += std::atoi(bb.substr(offset, end - offset).c_str());
157+
int64_t rsa_weight = 0;
158+
159+
if (!ParseInt64(bb.substr(offset, end - offset), &rsa_weight)) {
160+
error("%s: Unable to parse rsa weight from hashBoinc.", __func__);
161+
rsa_weight = 0;
162+
}
163+
164+
rsa_weight_sum += rsa_weight;
158165
}
159166

160167
offset = end + 3;
161168
end = bb.find("<|>", offset);
162169
}
163170

164-
if (rsa_weight < 0) {
171+
if (rsa_weight_sum < 0) {
165172
return 0;
166173
}
167174

168-
return rsa_weight;
175+
return rsa_weight_sum;
169176
}
170177
} // anonymous namespace
171178

src/rpc/protocol.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ int ReadHTTPHeaders(std::basic_istream<char>& stream, std::map<std::string, std:
114114
std::string strValue = str.substr(nColon+1);
115115
boost::trim(strValue);
116116
mapHeadersRet[strHeader] = strValue;
117-
if (strHeader == "content-length")
118-
nLen = atoi(strValue.c_str());
117+
if (strHeader == "content-length" && !ParseInt(strValue, &nLen)) {
118+
throw std::invalid_argument("Unable to parse content-length value.");
119+
}
119120
}
120121
}
121122
return nLen;
@@ -150,8 +151,9 @@ bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,
150151

151152
proto = 0;
152153
const char *ver = strstr(strProto.c_str(), "HTTP/1.");
153-
if (ver != nullptr)
154-
proto = atoi(ver+7);
154+
if (ver != nullptr && !ParseInt(std::string(ver+7), &proto)) {
155+
throw std::invalid_argument("Unable to parse proto.");
156+
}
155157

156158
return true;
157159
}
@@ -166,9 +168,15 @@ int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto)
166168
return HTTP_INTERNAL_SERVER_ERROR;
167169
proto = 0;
168170
const char *ver = strstr(str.c_str(), "HTTP/1.");
169-
if (ver != nullptr)
170-
proto = atoi(ver+7);
171-
return atoi(vWords[1].c_str());
171+
if (ver != nullptr && !ParseInt(std::string(ver+7), &proto)) {
172+
throw std::invalid_argument("Unable to parse proto.");
173+
}
174+
175+
int status = 0;
176+
if (!ParseInt(vWords[1], &status)) {
177+
throw std::invalid_argument("Unable to parse status.");
178+
}
179+
return status;
172180
}
173181

174182
int ReadHTTPMessage(std::basic_istream<char>& stream, std::map<std::string,

src/test/gridcoin/superblock_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ struct Legacy
7878
}
7979

8080
// Append zero magnitude researchers so the beacon count matches
81-
int num_zero_mag = atoi(ExtractXML(sBlock,"<ZERO>","</ZERO>"));
81+
int num_zero_mag = 0;
82+
if (!ParseInt(ExtractXML(sBlock,"<ZERO>","</ZERO>"), &num_zero_mag)) {
83+
error("%s: Unable to parse number of zero magnitude researchers from legary binary superblock data.",
84+
__func__);
85+
};
8286
const std::string zero_entry("0,15;");
8387
for(int i=0; i<num_zero_mag; ++i)
8488
stream << zero_entry;

src/util/system.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ static bool InterpretBool(const std::string& strValue)
7272
{
7373
if (strValue.empty())
7474
return true;
75-
return (atoi(strValue) != 0);
75+
76+
// Maintaining the behavior as described above, but replacing the atoi with ParseInt.
77+
int value = 0;
78+
if (!ParseInt(strValue, &value)) {
79+
value = 0;
80+
}
81+
82+
return (value);
7683
}
7784

7885
static std::string SettingName(const std::string& arg)

0 commit comments

Comments
 (0)