Skip to content

Commit 257fa17

Browse files
committed
Fixing some of the code smells
1 parent e14a2fe commit 257fa17

4 files changed

Lines changed: 26 additions & 15 deletions

File tree

source/databaseConnection.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
#include <pqxx/pqxx>
1010
#include <cstdio>
1111
#include <charconv>
12+
#include <format>
1213
#include <stdexcept>
1314
#include <boost/decimal.hpp>
1415

16+
class InvalidTimestampFormatError : public std::runtime_error {
17+
public:
18+
using std::runtime_error::runtime_error;
19+
};
20+
1521
static std::chrono::system_clock::time_point fastParseTimestamp(const char* ts) {
1622
int year = 0;
1723
int month = 0;
@@ -23,14 +29,15 @@ static std::chrono::system_clock::time_point fastParseTimestamp(const char* ts)
2329
const int parsedFields =
2430
std::sscanf(ts, "%4d-%2d-%2d %2d:%2d:%2d.%d", &year, &month, &day, &hour, &min, &sec, &usec);
2531
if (parsedFields != 6 && parsedFields != 7) {
26-
throw std::runtime_error("Invalid timestamp format");
32+
throw InvalidTimestampFormatError("Invalid timestamp format: " + std::string(ts));
2733
}
2834

2935
// Cache timegm per date — tick data is time-ordered so date changes rarely
30-
static char cachedDate[11] = {};
36+
static std::string cachedDate;
3137
static time_t cachedEpoch = 0;
32-
if (std::memcmp(ts, cachedDate, 10) != 0) {
33-
std::memcpy(cachedDate, ts, 10);
38+
const std::string_view date(ts, 10);
39+
if (cachedDate != date) {
40+
cachedDate.assign(date);
3441
std::tm tm = {};
3542
tm.tm_year = year - 1900;
3643
tm.tm_mon = month - 1;
@@ -46,13 +53,9 @@ static std::chrono::system_clock::time_point fastParseTimestamp(const char* ts)
4653
DatabaseConnection::DatabaseConnection(const std::string& endpoint, int port,
4754
const std::string& dbname, const std::string& user,
4855
const std::string& password) {
49-
connection_string =
50-
"host=" + endpoint + " "
51-
"port=" + std::to_string(port) + " "
52-
"dbname=" + dbname + " "
53-
"user=" + user + " "
54-
"password=" + password + " "
55-
"connect_timeout=3";
56+
connection_string = std::format(
57+
"host={} port={} dbname={} user={} password={} connect_timeout=3",
58+
endpoint, port, dbname, user, password);
5659

5760
}
5861

source/elasticClient.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ int ElasticClient::putTradingResults(const TradingResults& results) {
5959
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
6060
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(body.size()));
6161

62+
// Require TLS 1.2 or newer and enforce certificate / hostname verification
63+
// for any HTTPS endpoint (Sonar cpp:S4423 / S5527).
64+
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
65+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
66+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
67+
6268
const CURLcode rc = curl_easy_perform(curl);
6369

6470
long httpStatus = 0;

source/redisLoader.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <boost/asio/use_awaitable.hpp>
2121
#include <boost/redis.hpp>
2222
#include <boost/redis/connection.hpp>
23+
#include <boost/system/system_error.hpp>
2324

2425
#include <nlohmann/json.hpp>
2526

@@ -92,7 +93,7 @@ int RedisLoader::load(const std::string& rawJson,
9293
if (pushError) {
9394
try {
9495
std::rethrow_exception(pushError);
95-
} catch (const std::exception& ex) {
96+
} catch (const boost::system::system_error& ex) {
9697
std::cerr << "Redis LPUSH failed: " << ex.what() << std::endl;
9798
}
9899
return 3;
@@ -145,15 +146,15 @@ int RedisLoader::loadPayload(const std::string& redisHost,
145146
if (pushError) {
146147
try {
147148
std::rethrow_exception(pushError);
148-
} catch (const std::exception& ex) {
149+
} catch (const boost::system::system_error& ex) {
149150
std::cerr << "Redis LPUSH failed: " << ex.what() << std::endl;
150151
}
151152
return 3;
152153
}
153154

154155
try {
155156
JsonParser::parseConfigurationFromBase64(encoded);
156-
} catch (const std::exception& ex) {
157+
} catch (const nlohmann::json::exception& ex) {
157158
std::cerr << "RedisLoader: failed to parse payload: " << ex.what()
158159
<< std::endl;
159160
return 2;

source/trading/tradeManager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
#include <atomic>
1010
#include <chrono>
1111
#include <ctime>
12+
#include <format>
1213
#include <iomanip>
1314
#include <iostream>
1415
#include <sstream>
1516

1617
namespace {
1718
std::string nextTradeId() {
1819
static std::atomic<uint64_t> counter{0};
19-
return "T" + std::to_string(counter.fetch_add(1));
20+
return std::format("T{}", counter.fetch_add(1));
2021
}
2122
}
2223

0 commit comments

Comments
 (0)