Skip to content

Commit 438c79c

Browse files
committed
fix a buf of null pointer exception about redis driver.
1 parent 284d311 commit 438c79c

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/tmongoquery.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ TMongoDriver *TMongoQuery::driver()
282282
#ifdef TF_NO_DEBUG
283283
return (TMongoDriver *)database.driver();
284284
#else
285+
if (!database.driver()) {
286+
return nullptr;
287+
}
288+
285289
TMongoDriver *driver = dynamic_cast<TMongoDriver *>(database.driver());
286290
if (!driver) {
287291
throw RuntimeException("cast error", __FILE__, __LINE__);
@@ -298,6 +302,10 @@ const TMongoDriver *TMongoQuery::driver() const
298302
#ifdef TF_NO_DEBUG
299303
return (const TMongoDriver *)database.driver();
300304
#else
305+
if (!database.driver()) {
306+
return nullptr;
307+
}
308+
301309
const TMongoDriver *driver = dynamic_cast<const TMongoDriver *>(database.driver());
302310
if (!driver) {
303311
throw RuntimeException("cast error", __FILE__, __LINE__);

src/tredis.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ TRedisDriver *TRedis::driver()
3737
#ifdef TF_NO_DEBUG
3838
return (TRedisDriver *)database.driver();
3939
#else
40+
if (!database.driver()) {
41+
return nullptr;
42+
}
43+
4044
TRedisDriver *driver = dynamic_cast<TRedisDriver *>(database.driver());
4145
if (!driver) {
4246
throw RuntimeException("cast error", __FILE__, __LINE__);
@@ -53,6 +57,10 @@ const TRedisDriver *TRedis::driver() const
5357
#ifdef TF_NO_DEBUG
5458
return (const TRedisDriver *)database.driver();
5559
#else
60+
if (!database.driver()) {
61+
return nullptr;
62+
}
63+
5664
const TRedisDriver *driver = dynamic_cast<const TRedisDriver *>(database.driver());
5765
if (!driver) {
5866
throw RuntimeException("cast error", __FILE__, __LINE__);
@@ -62,8 +70,18 @@ const TRedisDriver *TRedis::driver() const
6270
}
6371

6472

73+
bool TRedis::isOpen() const
74+
{
75+
return (driver()) ? driver()->isOpen() : false;
76+
}
77+
78+
6579
QByteArray TRedis::get(const QByteArray &key)
6680
{
81+
if (!driver()) {
82+
return QByteArray();
83+
}
84+
6785
QVariantList reply;
6886
QList<QByteArray> command = { "GET", key };
6987
bool res = driver()->request(command, reply);
@@ -73,6 +91,10 @@ QByteArray TRedis::get(const QByteArray &key)
7391

7492
bool TRedis::set(const QByteArray &key, const QByteArray &value)
7593
{
94+
if (!driver()) {
95+
return false;
96+
}
97+
7698
QVariantList reply;
7799
QList<QByteArray> command = { "SET", key, value };
78100
return driver()->request(command, reply);
@@ -81,6 +103,10 @@ bool TRedis::set(const QByteArray &key, const QByteArray &value)
81103

82104
bool TRedis::setEx(const QByteArray &key, const QByteArray &value, int seconds)
83105
{
106+
if (!driver()) {
107+
return false;
108+
}
109+
84110
QVariantList reply;
85111
QList<QByteArray> command = { "SETEX", key, QByteArray::number(seconds), value };
86112
return driver()->request(command, reply);
@@ -89,6 +115,10 @@ bool TRedis::setEx(const QByteArray &key, const QByteArray &value, int seconds)
89115

90116
QByteArray TRedis::getSet(const QByteArray &key, const QByteArray &value)
91117
{
118+
if (!driver()) {
119+
return QByteArray();
120+
}
121+
92122
QVariantList reply;
93123
QList<QByteArray> command = { "GETSET", key, value };
94124
bool res = driver()->request(command, reply);
@@ -106,6 +136,10 @@ bool TRedis::del(const QByteArray &key)
106136

107137
int TRedis::del(const QList<QByteArray> &keys)
108138
{
139+
if (!driver()) {
140+
return 0;
141+
}
142+
109143
QVariantList reply;
110144
QList<QByteArray> command = { "DEL" };
111145
command << keys;

src/tredis.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class T_CORE_EXPORT TRedis
1515
TRedis(const TRedis &other);
1616
virtual ~TRedis() { }
1717

18-
bool exists(const QByteArray &key);
18+
bool isOpen() const;
19+
//bool exists(const QByteArray &key);
1920
QByteArray get(const QByteArray &key);
2021
bool set(const QByteArray &key, const QByteArray &value);
2122
bool setEx(const QByteArray &key, const QByteArray &value, int seconds);

src/tredisdriver.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ TRedisDriver::~TRedisDriver()
3535

3636
bool TRedisDriver::isOpen() const
3737
{
38-
return (client) ? client->isOpen() : false;
38+
return (client) ? (client->state() == QAbstractSocket::ConnectedState) : false;
3939
}
4040

4141

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

52+
if (client->state() != QAbstractSocket::UnconnectedState) {
53+
return false;
54+
}
55+
5256
QString hst = (host.isEmpty()) ? "localhost" : host;
5357

5458
if (port <= 0) {
@@ -63,6 +67,7 @@ bool TRedisDriver::open(const QString &, const QString &, const QString &, const
6367
tSystemDebug("Redis open successfully");
6468
} else {
6569
tSystemError("Redis open failed");
70+
close();
6671
}
6772
return ret;
6873
}
@@ -72,6 +77,8 @@ void TRedisDriver::close()
7277
{
7378
if (client) {
7479
client->close();
80+
delete client;
81+
client = nullptr;
7582
}
7683
}
7784

0 commit comments

Comments
 (0)