@@ -4857,8 +4857,8 @@ typedef struct list_entry {
48574857
48584858typedef  struct  srtp_stream_list_ctx_t_  {
48594859    list_entry  * entries ;
4860+     size_t  capacity ;
48604861    size_t  size ;
4861-     size_t  available ;
48624862} srtp_stream_list_ctx_t_ ;
48634863
48644864srtp_err_status_t  srtp_stream_list_alloc (srtp_stream_list_t  * list_ptr )
@@ -4876,8 +4876,8 @@ srtp_err_status_t srtp_stream_list_alloc(srtp_stream_list_t *list_ptr)
48764876        return  srtp_err_status_alloc_fail ;
48774877    }
48784878
4879-     list -> size  =  INITIAL_STREAM_INDEX_SIZE ;
4880-     list -> available  =  INITIAL_STREAM_INDEX_SIZE ;
4879+     list -> capacity  =  INITIAL_STREAM_INDEX_SIZE ;
4880+     list -> size  =  0 ;
48814881
48824882    * list_ptr  =  list ;
48834883
@@ -4887,7 +4887,7 @@ srtp_err_status_t srtp_stream_list_alloc(srtp_stream_list_t *list_ptr)
48874887srtp_err_status_t  srtp_stream_list_dealloc (srtp_stream_list_t  list )
48884888{
48894889    /* list must be empty */ 
4890-     if  (list -> available  !=  list -> size ) {
4890+     if  (list -> size  !=  0 ) {
48914891        return  srtp_err_status_fail ;
48924892    }
48934893
@@ -4906,34 +4906,40 @@ srtp_err_status_t srtp_stream_list_insert(srtp_stream_list_t list,
49064906{
49074907    /* 
49084908     * there is no space to hold the new entry in the entries buffer, 
4909-      * double  the size of the buffer. 
4909+      * increase  the size of the buffer by a factor of 1.5 . 
49104910     */ 
4911-     if  (list -> available  ==  0 ) {
4912-         size_t  new_size  =  list -> size  *  2 ;
4911+     if  (list -> size  ==  list -> capacity ) {
4912+         size_t  new_capacity  =  list -> capacity  +  ((list -> capacity  +  1u ) / 2u );
4913+ 
4914+         // check for capacity overflow. 
4915+         if  ((sizeof (list_entry ) *  new_capacity ) <=
4916+             (sizeof (list_entry ) *  list -> capacity )) {
4917+             return  srtp_err_status_alloc_fail ;
4918+         }
4919+ 
49134920        list_entry  * new_entries  = 
4914-             srtp_crypto_alloc (sizeof (list_entry ) *  new_size );
4921+             srtp_crypto_alloc (sizeof (list_entry ) *  new_capacity );
49154922        if  (new_entries  ==  NULL ) {
49164923            return  srtp_err_status_alloc_fail ;
49174924        }
49184925
49194926        // copy previous entries into the new buffer 
4920-         memcpy (new_entries , list -> entries , sizeof (list_entry ) *  list -> size );
4927+         memcpy (new_entries , list -> entries , sizeof (list_entry ) *  list -> capacity );
49214928        // release previous entries 
49224929        srtp_crypto_free (list -> entries );
49234930        // assign new entries to the list 
49244931        list -> entries  =  new_entries ;
4925-         // update list info 
4926-         list -> size  =  new_size ;
4927-         list -> available  =  new_size  / 2 ;
4932+         // update list capacity 
4933+         list -> capacity  =  new_capacity ;
49284934    }
49294935
49304936    // fill the first available entry 
4931-     size_t  next_index  =  list -> size   -   list -> available ;
4937+     size_t  next_index  =  list -> size ;
49324938    list -> entries [next_index ].ssrc  =  stream -> ssrc ;
49334939    list -> entries [next_index ].stream  =  stream ;
49344940
4935-     // update available  value 
4936-     list -> available -- ;
4941+     // update size  value 
4942+     list -> size ++ ;
49374943
49384944    return  srtp_err_status_ok ;
49394945}
@@ -4946,14 +4952,14 @@ srtp_err_status_t srtp_stream_list_insert(srtp_stream_list_t list,
49464952void  srtp_stream_list_remove (srtp_stream_list_t  list ,
49474953                             srtp_stream_t  stream_to_remove )
49484954{
4949-     size_t  end  =  list -> size   -   list -> available ;
4955+     size_t  end  =  list -> size ;
49504956
49514957    for  (size_t  i  =  0 ; i  <  end ; i ++ ) {
49524958        if  (list -> entries [i ].ssrc  ==  stream_to_remove -> ssrc ) {
4953-             size_t  entries_to_move  =  list -> size  -  list -> available   -   i  -  1 ;
4959+             size_t  entries_to_move  =  list -> size  -  i  -  1 ;
49544960            memmove (& list -> entries [i ], & list -> entries [i  +  1 ],
49554961                    sizeof (list_entry ) *  entries_to_move );
4956-             list -> available ++ ;
4962+             list -> size -- ;
49574963
49584964            break ;
49594965        }
@@ -4962,7 +4968,7 @@ void srtp_stream_list_remove(srtp_stream_list_t list,
49624968
49634969srtp_stream_t  srtp_stream_list_get (srtp_stream_list_t  list , uint32_t  ssrc )
49644970{
4965-     size_t  end  =  list -> size   -   list -> available ;
4971+     size_t  end  =  list -> size ;
49664972
49674973    list_entry  * entries  =  list -> entries ;
49684974
@@ -4981,25 +4987,25 @@ void srtp_stream_list_for_each(srtp_stream_list_t list,
49814987{
49824988    list_entry  * entries  =  list -> entries ;
49834989
4984-     size_t  available  =  list -> available ;
4990+     size_t  size  =  list -> size ;
49854991
49864992    /* 
49874993     * the second statement of the expression needs to be recalculated on each 
49884994     * iteration as the available number of entries may change within the given 
49894995     * callback. 
49904996     * Ie: in case the callback calls srtp_stream_list_remove(). 
49914997     */ 
4992-     for  (size_t  i  =  0 ; i  <  list -> size   -   list -> available ;) {
4998+     for  (size_t  i  =  0 ; i  <  list -> size ;) {
49934999        if  (!callback (entries [i ].stream , data )) {
49945000            break ;
49955001        }
49965002
49975003        // the entry was not removed, increase the counter. 
4998-         if  (available  ==  list -> available ) {
5004+         if  (size  ==  list -> size ) {
49995005            ++ i ;
50005006        }
50015007
5002-         available  =  list -> available ;
5008+         size  =  list -> size ;
50035009    }
50045010}
50055011
0 commit comments