Skip to content

Commit

Permalink
fix a buf of null pointer exception about redis driver.
Browse files Browse the repository at this point in the history
aoym committed Sep 1, 2015
1 parent 284d311 commit 438c79c
Showing 4 changed files with 52 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/tmongoquery.cpp
Original file line number Diff line number Diff line change
@@ -282,6 +282,10 @@ TMongoDriver *TMongoQuery::driver()
#ifdef TF_NO_DEBUG
return (TMongoDriver *)database.driver();
#else
if (!database.driver()) {
return nullptr;
}

TMongoDriver *driver = dynamic_cast<TMongoDriver *>(database.driver());
if (!driver) {
throw RuntimeException("cast error", __FILE__, __LINE__);
@@ -298,6 +302,10 @@ const TMongoDriver *TMongoQuery::driver() const
#ifdef TF_NO_DEBUG
return (const TMongoDriver *)database.driver();
#else
if (!database.driver()) {
return nullptr;
}

const TMongoDriver *driver = dynamic_cast<const TMongoDriver *>(database.driver());
if (!driver) {
throw RuntimeException("cast error", __FILE__, __LINE__);
34 changes: 34 additions & 0 deletions src/tredis.cpp
Original file line number Diff line number Diff line change
@@ -37,6 +37,10 @@ TRedisDriver *TRedis::driver()
#ifdef TF_NO_DEBUG
return (TRedisDriver *)database.driver();
#else
if (!database.driver()) {
return nullptr;
}

TRedisDriver *driver = dynamic_cast<TRedisDriver *>(database.driver());
if (!driver) {
throw RuntimeException("cast error", __FILE__, __LINE__);
@@ -53,6 +57,10 @@ const TRedisDriver *TRedis::driver() const
#ifdef TF_NO_DEBUG
return (const TRedisDriver *)database.driver();
#else
if (!database.driver()) {
return nullptr;
}

const TRedisDriver *driver = dynamic_cast<const TRedisDriver *>(database.driver());
if (!driver) {
throw RuntimeException("cast error", __FILE__, __LINE__);
@@ -62,8 +70,18 @@ const TRedisDriver *TRedis::driver() const
}


bool TRedis::isOpen() const
{
return (driver()) ? driver()->isOpen() : false;
}


QByteArray TRedis::get(const QByteArray &key)
{
if (!driver()) {
return QByteArray();
}

QVariantList reply;
QList<QByteArray> command = { "GET", key };
bool res = driver()->request(command, reply);
@@ -73,6 +91,10 @@ QByteArray TRedis::get(const QByteArray &key)

bool TRedis::set(const QByteArray &key, const QByteArray &value)
{
if (!driver()) {
return false;
}

QVariantList reply;
QList<QByteArray> command = { "SET", key, value };
return driver()->request(command, reply);
@@ -81,6 +103,10 @@ bool TRedis::set(const QByteArray &key, const QByteArray &value)

bool TRedis::setEx(const QByteArray &key, const QByteArray &value, int seconds)
{
if (!driver()) {
return false;
}

QVariantList reply;
QList<QByteArray> command = { "SETEX", key, QByteArray::number(seconds), value };
return driver()->request(command, reply);
@@ -89,6 +115,10 @@ bool TRedis::setEx(const QByteArray &key, const QByteArray &value, int seconds)

QByteArray TRedis::getSet(const QByteArray &key, const QByteArray &value)
{
if (!driver()) {
return QByteArray();
}

QVariantList reply;
QList<QByteArray> command = { "GETSET", key, value };
bool res = driver()->request(command, reply);
@@ -106,6 +136,10 @@ bool TRedis::del(const QByteArray &key)

int TRedis::del(const QList<QByteArray> &keys)
{
if (!driver()) {
return 0;
}

QVariantList reply;
QList<QByteArray> command = { "DEL" };
command << keys;
3 changes: 2 additions & 1 deletion src/tredis.h
Original file line number Diff line number Diff line change
@@ -15,7 +15,8 @@ class T_CORE_EXPORT TRedis
TRedis(const TRedis &other);
virtual ~TRedis() { }

bool exists(const QByteArray &key);
bool isOpen() const;
//bool exists(const QByteArray &key);
QByteArray get(const QByteArray &key);
bool set(const QByteArray &key, const QByteArray &value);
bool setEx(const QByteArray &key, const QByteArray &value, int seconds);
9 changes: 8 additions & 1 deletion src/tredisdriver.cpp
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ TRedisDriver::~TRedisDriver()

bool TRedisDriver::isOpen() const
{
return (client) ? client->isOpen() : false;
return (client) ? (client->state() == QAbstractSocket::ConnectedState) : false;
}


@@ -49,6 +49,10 @@ bool TRedisDriver::open(const QString &, const QString &, const QString &, const
return true;
}

if (client->state() != QAbstractSocket::UnconnectedState) {
return false;
}

QString hst = (host.isEmpty()) ? "localhost" : host;

if (port <= 0) {
@@ -63,6 +67,7 @@ bool TRedisDriver::open(const QString &, const QString &, const QString &, const
tSystemDebug("Redis open successfully");
} else {
tSystemError("Redis open failed");
close();
}
return ret;
}
@@ -72,6 +77,8 @@ void TRedisDriver::close()
{
if (client) {
client->close();
delete client;
client = nullptr;
}
}

0 comments on commit 438c79c

Please sign in to comment.