Skip to content

Commit f8d2c2e

Browse files
committed
Fix possible null pointer access
1 parent 30d3c25 commit f8d2c2e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib/ClientImpl.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ typedef std::unique_lock<std::mutex> Lock;
7676

7777
typedef std::vector<std::string> StringList;
7878

79+
template <typename T>
80+
static std::string getName(std::weak_ptr<T> ptr) {
81+
auto value = ptr.lock();
82+
return value ? value->getName() : "(null)";
83+
}
84+
7985
ClientImpl::ClientImpl(const std::string& serviceUrl, const ClientConfiguration& clientConfiguration)
8086
: mutex_(),
8187
state_(Open),
@@ -226,7 +232,7 @@ void ClientImpl::handleProducerCreated(Result result, ProducerImplBaseWeakPtr pr
226232
auto existingProducer = producers_.create(address, producer);
227233
if (existingProducer) {
228234
LOG_ERROR("Unexpected existing producer at the same address: "
229-
<< address << ", producer: " << existingProducer.value().lock()->getProducerName());
235+
<< address << ", producer: " << getName(existingProducer.value()));
230236
callback(ResultUnknownError, {});
231237
return;
232238
}
@@ -314,7 +320,7 @@ void ClientImpl::handleReaderMetadataLookup(const Result result, const LookupDat
314320
auto existingConsumer = consumers_.create(address, consumer);
315321
if (existingConsumer) {
316322
LOG_ERROR("Unexpected existing consumer at the same address: "
317-
<< address << ", consumer: " << existingConsumer.value().lock()->getName());
323+
<< address << ", consumer: " << getName(existingConsumer.value()));
318324
}
319325
} else {
320326
LOG_ERROR("Unexpected case: the consumer is somehow expired");
@@ -514,7 +520,7 @@ void ClientImpl::handleConsumerCreated(Result result, ConsumerImplBaseWeakPtr co
514520
auto existingConsumer = consumers_.create(address, consumer);
515521
if (existingConsumer) {
516522
LOG_ERROR("Unexpected existing consumer at the same address: "
517-
<< address << ", consumer: " << existingConsumer.value().lock()->getName());
523+
<< address << ", consumer: " << getName(existingConsumer.value()));
518524
callback(ResultUnknownError, {});
519525
return;
520526
}

0 commit comments

Comments
 (0)