Fixed #1648: Timer heap new API cancel_if_active() should not assert if given bad entry

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@4449 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjlib/src/pj/timer.c b/pjlib/src/pj/timer.c
index 47209fb..07752f8 100644
--- a/pjlib/src/pj/timer.c
+++ b/pjlib/src/pj/timer.c
@@ -45,6 +45,13 @@
 
 #define DEFAULT_MAX_TIMED_OUT_PER_POLL  (64)
 
+enum
+{
+    F_DONT_CALL = 1,
+    F_DONT_ASSERT = 2,
+    F_SET_ID = 4
+};
+
 
 /**
  * The implementation of timer heap.
@@ -313,7 +320,7 @@
 
 static int cancel( pj_timer_heap_t *ht, 
 		   pj_timer_entry *entry, 
-		   int dont_call)
+		   unsigned flags)
 {
   long timer_node_slot;
 
@@ -330,14 +337,15 @@
 
   if (entry != ht->heap[timer_node_slot])
     {
-      pj_assert(entry == ht->heap[timer_node_slot]);
+      if ((flags & F_DONT_ASSERT) == 0)
+	  pj_assert(entry == ht->heap[timer_node_slot]);
       return 0;
     }
   else
     {
       remove_node( ht, timer_node_slot);
 
-      if (dont_call == 0)
+      if ((flags & F_DONT_CALL) == 0)
         // Call the close hook.
 	(*ht->callback)(ht, entry);
       return 1;
@@ -551,7 +559,7 @@
 
 static int cancel_timer(pj_timer_heap_t *ht,
 			pj_timer_entry *entry,
-			pj_bool_t set_id,
+			unsigned flags,
 			int id_val)
 {
     int count;
@@ -559,8 +567,8 @@
     PJ_ASSERT_RETURN(ht && entry, PJ_EINVAL);
 
     lock_timer_heap(ht);
-    count = cancel(ht, entry, 1);
-    if (set_id) {
+    count = cancel(ht, entry, flags | F_DONT_CALL);
+    if (flags & F_SET_ID) {
 	entry->id = id_val;
     }
     if (entry->_grp_lock) {
@@ -576,14 +584,14 @@
 PJ_DEF(int) pj_timer_heap_cancel( pj_timer_heap_t *ht,
 				  pj_timer_entry *entry)
 {
-    return cancel_timer(ht, entry, PJ_FALSE, 0);
+    return cancel_timer(ht, entry, 0, 0);
 }
 
 PJ_DEF(int) pj_timer_heap_cancel_if_active(pj_timer_heap_t *ht,
                                            pj_timer_entry *entry,
                                            int id_val)
 {
-    return cancel_timer(ht, entry, PJ_TRUE, id_val);
+    return cancel_timer(ht, entry, F_SET_ID | F_DONT_ASSERT, id_val);
 }
 
 PJ_DEF(unsigned) pj_timer_heap_poll( pj_timer_heap_t *ht,