[Asterisk-code-review] astobj2: Header macro's should deal with error's from ao2 ref. (asterisk[master])

Corey Farrell asteriskteam at digium.com
Fri Sep 9 13:21:47 CDT 2016


Corey Farrell has uploaded a new change for review.

  https://gerrit.asterisk.org/3876

Change subject: astobj2: Header macro's should deal with error's from ao2_ref.
......................................................................

astobj2: Header macro's should deal with error's from ao2_ref.

* ao2_t_bump - return NULL.
* ao2_t_replace - set the destination to NULL.
* ao2_ref_and_lock - return 0 to indicate failure.

ASTERISK-26351 #close

Change-Id: I5e06d27fb6667f54c960023aa3b93d95b07147de
---
M include/asterisk/astobj2.h
1 file changed, 12 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/76/3876/1

diff --git a/include/asterisk/astobj2.h b/include/asterisk/astobj2.h
index 28ae73e..1136df5 100644
--- a/include/asterisk/astobj2.h
+++ b/include/asterisk/astobj2.h
@@ -479,14 +479,13 @@
  *
  * \param obj AO2 object to bump the refcount on.
  * \retval The given \a obj pointer.
+ * \retval NULL if \a obj is not a valid AO2 object.
  */
 #define ao2_t_bump(obj, tag)						\
 	({							\
 		typeof(obj) __obj_ ## __LINE__ = (obj);		\
-		if (__obj_ ## __LINE__) {			\
-			ao2_t_ref(__obj_ ## __LINE__, +1, (tag));	\
-		}						\
-		__obj_ ## __LINE__;				\
+		(__obj_ ## __LINE__ && ao2_t_ref(__obj_ ## __LINE__, +1, (tag)) > 0) ? \
+			__obj_ ## __LINE__ : NULL;		\
 	})
 #define ao2_bump(obj) \
 	ao2_t_bump((obj), "")
@@ -504,14 +503,18 @@
 	{\
 		typeof(dst) *__dst_ ## __LINE__ = &dst; \
 		typeof(src) __src_ ## __LINE__ = src; \
+		int __had_error_ ## __LINE__ = 0; \
 		if (__src_ ## __LINE__ != *__dst_ ## __LINE__) { \
 			if (__src_ ## __LINE__) {\
-				ao2_t_ref(__src_ ## __LINE__, +1, (tag)); \
+				if (ao2_t_ref(__src_ ## __LINE__, +1, (tag)) < 1) { \
+					__had_error_ ## __LINE__ = 1; \
+				} \
 			} \
 			if (*__dst_ ## __LINE__) {\
 				ao2_t_ref(*__dst_ ## __LINE__, -1, (tag)); \
 			} \
-			*__dst_ ## __LINE__ = __src_ ## __LINE__; \
+			*__dst_ ## __LINE__ = __had_error_ ## __LINE__ ? \
+				NULL : __src_ ## __LINE__; \
 		} \
 	}
 #define ao2_replace(dst, src) \
@@ -764,7 +767,9 @@
 AST_INLINE_API(
 int ao2_ref_and_lock(void *obj),
 {
-	ao2_ref(obj, +1);
+	if (ao2_ref(obj, +1) < 1) {
+		return 0;
+	}
 	if (ao2_lock(obj)) {
 		ao2_ref(obj, -1);
 		return 0;

-- 
To view, visit https://gerrit.asterisk.org/3876
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5e06d27fb6667f54c960023aa3b93d95b07147de
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Corey Farrell <git at cfware.com>



More information about the asterisk-code-review mailing list