Skip to content

Commit ede524c

Browse files
tryfinallyJohnSully
authored andcommitted
fixes for robj_sharedptr
1. fix cases where null pointer might be accessed 2. make assignmnet op safe 3. make operator bool explicit (safe bool idiom) 4. make comparison operators symetric fix robj_sharedptr use in rdb.cpp
1 parent aeed09c commit ede524c

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

src/rdb.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,9 +2412,8 @@ int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
24122412
if (fStaleMvccKey && !fExpiredKey && rsi != nullptr && rsi->mi != nullptr && rsi->mi->staleKeyMap != nullptr && lookupKeyRead(db, &keyobj) == nullptr) {
24132413
// We have a key that we've already deleted and is not back in our database.
24142414
// We'll need to inform the sending master of the delete if it is also a replica of us
2415-
robj *objKeyDup = createStringObject(key, sdslen(key));
2416-
rsi->mi->staleKeyMap->operator[](db - g_pserver->db).push_back(objKeyDup);
2417-
decrRefCount(objKeyDup);
2415+
robj_sharedptr objKeyDup(createStringObject(key, sdslen(key)));
2416+
rsi->mi->staleKeyMap->operator[](db - g_pserver->db).push_back(objKeyDup);
24182417
}
24192418
fLastKeyExpired = true;
24202419
sdsfree(key);

src/server.h

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,24 @@ class robj_sharedptr
166166

167167
public:
168168
robj_sharedptr()
169-
: m_ptr(nullptr)
170-
{}
171-
robj_sharedptr(redisObject *ptr)
172-
: m_ptr(ptr)
173-
{
169+
: m_ptr(nullptr)
170+
{}
171+
explicit robj_sharedptr(redisObject *ptr)
172+
: m_ptr(ptr)
173+
{
174+
if(m_ptr)
174175
incrRefCount(ptr);
175-
}
176+
}
176177
~robj_sharedptr()
177178
{
178179
if (m_ptr)
179180
decrRefCount(m_ptr);
180181
}
181182
robj_sharedptr(const robj_sharedptr& other)
182-
{
183-
m_ptr = other.m_ptr;
184-
incrRefCount(m_ptr);
183+
: m_ptr(other.m_ptr)
184+
{
185+
if(m_ptr)
186+
incrRefCount(m_ptr);
185187
}
186188

187189
robj_sharedptr(robj_sharedptr&& other)
@@ -192,41 +194,19 @@ class robj_sharedptr
192194

193195
robj_sharedptr &operator=(const robj_sharedptr& other)
194196
{
195-
if (m_ptr)
196-
decrRefCount(m_ptr);
197-
m_ptr = other.m_ptr;
198-
incrRefCount(m_ptr);
197+
robj_sharedptr tmp(other);
198+
using std::swap;
199+
swap(m_ptr, tmp.m_ptr);
199200
return *this;
200201
}
201202
robj_sharedptr &operator=(redisObject *ptr)
202203
{
203-
if (m_ptr)
204-
decrRefCount(m_ptr);
205-
m_ptr = ptr;
206-
incrRefCount(m_ptr);
204+
robj_sharedptr tmp(ptr);
205+
using std::swap;
206+
swap(m_ptr, tmp.m_ptr);
207207
return *this;
208208
}
209-
210-
bool operator==(const robj_sharedptr &other) const
211-
{
212-
return m_ptr == other.m_ptr;
213-
}
214-
215-
bool operator==(const void *p) const
216-
{
217-
return m_ptr == p;
218-
}
219-
220-
bool operator!=(const robj_sharedptr &other) const
221-
{
222-
return m_ptr != other.m_ptr;
223-
}
224-
225-
bool operator!=(const void *p) const
226-
{
227-
return m_ptr != p;
228-
}
229-
209+
230210
redisObject* operator->() const
231211
{
232212
return m_ptr;
@@ -237,7 +217,7 @@ class robj_sharedptr
237217
return !m_ptr;
238218
}
239219

240-
operator bool() const{
220+
explicit operator bool() const{
241221
return !!m_ptr;
242222
}
243223

@@ -247,8 +227,39 @@ class robj_sharedptr
247227
}
248228

249229
redisObject *get() { return m_ptr; }
230+
const redisObject *get() const { return m_ptr; }
250231
};
251232

233+
inline bool operator==(const robj_sharedptr &lhs, const robj_sharedptr &rhs)
234+
{
235+
return lhs.get() == rhs.get();
236+
}
237+
238+
inline bool operator!=(const robj_sharedptr &lhs, const robj_sharedptr &rhs)
239+
{
240+
return !(lhs == rhs);
241+
}
242+
243+
inline bool operator==(const robj_sharedptr &lhs, const void *p)
244+
{
245+
return lhs.get() == p;
246+
}
247+
248+
inline bool operator==(const void *p, const robj_sharedptr &rhs)
249+
{
250+
return rhs == p;
251+
}
252+
253+
inline bool operator!=(const robj_sharedptr &lhs, const void *p)
254+
{
255+
return !(lhs == p);
256+
}
257+
258+
inline bool operator!=(const void *p, const robj_sharedptr &rhs)
259+
{
260+
return !(rhs == p);
261+
}
262+
252263
/* Error codes */
253264
#define C_OK 0
254265
#define C_ERR -1

0 commit comments

Comments
 (0)