Problem
When connecting with empty or invalid credentials, the client can crash with a NullPointerException.
Issue 1: NPE in TestConnection() with nullptr client
File: timeplus/timeplus.cpp lines 74, 78, 96
Root Cause:
GuardedClient default constructs with client = nullptr
- If
GetGuardedClient() throws (e.g., TimeoutError when pool is exhausted), client remains nullptr
- The catch blocks call
guarded_client.TestConnection() with client == nullptr
TestConnection() calls client->Ping() causing NPE
void ExecuteWithRetries(std::function<void(Client&)> func, BaseResult& result) {
ClientPool::GuardedClient guarded_client; // client = nullptr
for (int retries = config_.max_retries; retries >= 0; --retries) {
try {
if (guarded_client.client == nullptr) {
guarded_client = client_pool_.GetGuardedClient(...); // May throw TimeoutError
}
...
} catch (const ProtocolError& ex) {
guarded_client.TestConnection(); // NPE if client is nullptr!
} catch (const ServerError& ex) {
guarded_client.TestConnection(); // NPE if client is nullptr!
} catch (const std::exception& ex) {
guarded_client.TestConnection(); // NPE if client is nullptr!
}
}
}
Fix for Issue 1:
Add null check in TestConnection() (timeplus/client_pool.cpp):
void ClientPool::GuardedClient::TestConnection() noexcept {
if (!client) {
valid = false;
return;
}
try {
client->Ping();
valid = true;
} catch (...) {
valid = false;
}
...
}
Issue 2: NPE in TRACE macro (debug builds only)
File: timeplus/client_pool.cpp line 40
GetCurrentEndpoint() returns std::optional<Endpoint>, but the TRACE macro dereferenced it without checking. This only affects debug builds with TRACE_TIMEPLUS_CPP defined.
Note: This was already fixed by adding a null check before the TRACE statement.
Files to Modify
timeplus/client_pool.cpp - Add null check for client in TestConnection()
Verification
- Build the project
- Test with invalid credentials or exhausted connection pool to verify no crash occurs
Problem
When connecting with empty or invalid credentials, the client can crash with a NullPointerException.
Issue 1: NPE in
TestConnection()with nullptr clientFile:
timeplus/timeplus.cpplines 74, 78, 96Root Cause:
GuardedClientdefault constructs withclient = nullptrGetGuardedClient()throws (e.g.,TimeoutErrorwhen pool is exhausted),clientremainsnullptrguarded_client.TestConnection()withclient == nullptrTestConnection()callsclient->Ping()causing NPEFix for Issue 1:
Add null check in
TestConnection()(timeplus/client_pool.cpp):Issue 2: NPE in TRACE macro (debug builds only)
File:
timeplus/client_pool.cppline 40GetCurrentEndpoint()returnsstd::optional<Endpoint>, but the TRACE macro dereferenced it without checking. This only affects debug builds withTRACE_TIMEPLUS_CPPdefined.Note: This was already fixed by adding a null check before the TRACE statement.
Files to Modify
timeplus/client_pool.cpp- Add null check forclientinTestConnection()Verification