[vlc-commits] vlc_arrays: split array_append in checked and w/abort

Francois Cartegnie git at videolan.org
Wed Oct 25 12:12:36 CEST 2017


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Wed Oct 25 12:00:05 2017 +0200| [b01a32e73c74e60409f88a69d6e57563ed4f69be] | committer: Francois Cartegnie

vlc_arrays: split array_append in checked and w/abort

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b01a32e73c74e60409f88a69d6e57563ed4f69be
---

 include/vlc_arrays.h                    | 11 +++++++++--
 include/vlc_fingerprinter.h             |  2 +-
 lib/event.c                             | 11 +++++++++--
 lib/media_list.c                        |  2 +-
 modules/access/bluray.c                 |  2 +-
 modules/access/dsm/sd.c                 |  2 +-
 modules/access_output/livehttp.c        |  2 +-
 modules/control/dbus/dbus.c             |  8 ++++----
 modules/gui/qt/adapters/chromaprint.cpp |  6 +++++-
 modules/misc/fingerprinter.c            | 21 +++++++++++++--------
 modules/mux/asf.c                       |  8 +++++++-
 modules/services_discovery/microdns.c   |  4 ++--
 modules/stream_filter/hds/hds.c         |  2 +-
 src/input/mrl_helpers.h                 |  6 +++++-
 src/interface/dialog.c                  |  8 +++++++-
 src/misc/background_worker.c            |  7 ++++++-
 src/misc/httpcookies.c                  |  7 +++++--
 src/stream_output/stream_output.c       |  6 +++---
 src/video_output/video_output.c         |  6 +++---
 19 files changed, 84 insertions(+), 37 deletions(-)

diff --git a/include/vlc_arrays.h b/include/vlc_arrays.h
index 8c4419cb61..fb967caedf 100644
--- a/include/vlc_arrays.h
+++ b/include/vlc_arrays.h
@@ -307,15 +307,22 @@ static inline void vlc_array_insert( vlc_array_t *ar, void *elem, int idx )
     ar->pp_elems = pp;
 }
 
-static inline void vlc_array_append( vlc_array_t *ar, void *elem )
+static inline bool vlc_array_append( vlc_array_t *ar, void *elem )
 {
     void **pp = (void **)realloc( ar->pp_elems,
                                   sizeof( void * ) * (ar->i_count + 1) );
     if( unlikely(pp == NULL) )
-        abort();
+        return false;
 
     pp[ar->i_count++] = elem;
     ar->pp_elems = pp;
+    return true;
+}
+
+static inline void vlc_array_append_or_abort( vlc_array_t *ar, void *elem )
+{
+    if( !vlc_array_append( ar, elem ) )
+        abort();
 }
 
 static inline void vlc_array_remove( vlc_array_t *ar, size_t idx )
diff --git a/include/vlc_fingerprinter.h b/include/vlc_fingerprinter.h
index a958ef7692..e4b22f94bb 100644
--- a/include/vlc_fingerprinter.h
+++ b/include/vlc_fingerprinter.h
@@ -75,7 +75,7 @@ struct fingerprinter_thread_t
 
     module_t *   p_module;
 
-    void ( *pf_enqueue ) ( struct fingerprinter_thread_t *f, fingerprint_request_t *r );
+    bool ( *pf_enqueue ) ( struct fingerprinter_thread_t *f, fingerprint_request_t *r );
     fingerprint_request_t * ( *pf_getresults ) ( struct fingerprinter_thread_t *f );
     void ( *pf_apply ) ( fingerprint_request_t *, size_t i_resultid );
 };
diff --git a/lib/event.c b/lib/event.c
index 0bd5510a62..a34ed40505 100644
--- a/lib/event.c
+++ b/lib/event.c
@@ -239,10 +239,17 @@ int libvlc_event_attach(libvlc_event_manager_t *em, libvlc_event_type_t type,
     listener->p_user_data = opaque;
     listener->pf_callback = callback;
 
+    int i_ret;
     vlc_mutex_lock(&em->lock);
-    vlc_array_append(&em->listeners, listener);
+    if(!vlc_array_append(&em->listeners, listener))
+    {
+        i_ret = VLC_EGENERIC;
+        free(listener);
+    }
+    else
+        i_ret = VLC_SUCCESS;
     vlc_mutex_unlock(&em->lock);
-    return 0;
+    return i_ret;
 }
 
 /**************************************************************************
diff --git a/lib/media_list.c b/lib/media_list.c
index 5567567b07..463d039966 100644
--- a/lib/media_list.c
+++ b/lib/media_list.c
@@ -335,7 +335,7 @@ void libvlc_media_list_internal_add_media( libvlc_media_list_t * p_mlist,
 
     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
                           EventWillHappen );
-    vlc_array_append( &p_mlist->items, p_md );
+    vlc_array_append_or_abort( &p_mlist->items, p_md );
     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
                           EventDidHappen );
 }
diff --git a/modules/access/bluray.c b/modules/access/bluray.c
index b1408e4fb6..1f2f952f34 100644
--- a/modules/access/bluray.c
+++ b/modules/access/bluray.c
@@ -1028,7 +1028,7 @@ static es_out_id_t *esOutAdd(es_out_t *p_out, const es_format_t *p_fmt)
                 p_pair->i_id = p_fmt->i_id;
                 p_pair->p_es = p_es;
                 msg_Info(p_demux, "Adding ES %d", p_fmt->i_id);
-                vlc_array_append(&p_sys->es, p_pair);
+                vlc_array_append_or_abort(&p_sys->es, p_pair);
 
                 if (b_select) {
                     if (fmt.i_cat == AUDIO_ES) {
diff --git a/modules/access/dsm/sd.c b/modules/access/dsm/sd.c
index 94129dc504..ce157d2e56 100644
--- a/modules/access/dsm/sd.c
+++ b/modules/access/dsm/sd.c
@@ -68,7 +68,7 @@ static void entry_item_append( services_discovery_t *p_sd,
     p_entry_item->p_entry = p_entry;
     p_entry_item->p_item = p_item;
     input_item_Hold( p_item );
-    vlc_array_append( &p_sys->entry_item_list, p_entry_item );
+    vlc_array_append_or_abort( &p_sys->entry_item_list, p_entry_item );
     services_discovery_AddItem( p_sd, p_item );
 }
 
diff --git a/modules/access_output/livehttp.c b/modules/access_output/livehttp.c
index 235b26b819..d8d1ace11a 100644
--- a/modules/access_output/livehttp.c
+++ b/modules/access_output/livehttp.c
@@ -880,7 +880,7 @@ static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t
         return -1;
     }
 
-    vlc_array_append( &p_sys->segments_t, segment );
+    vlc_array_append_or_abort( &p_sys->segments_t, segment );
 
     if( p_sys->psz_keyfile )
     {
diff --git a/modules/control/dbus/dbus.c b/modules/control/dbus/dbus.c
index 5afe8bb782..484195a7c6 100644
--- a/modules/control/dbus/dbus.c
+++ b/modules/control/dbus/dbus.c
@@ -342,7 +342,7 @@ static dbus_bool_t add_timeout(DBusTimeout *to, void *data)
     dbus_timeout_set_data(to, expiry, free);
 
     vlc_mutex_lock(&sys->lock);
-    vlc_array_append(&sys->timeouts, to);
+    vlc_array_append_or_abort(&sys->timeouts, to);
     vlc_mutex_unlock(&sys->lock);
 
     return TRUE;
@@ -447,7 +447,7 @@ static dbus_bool_t add_watch( DBusWatch *p_watch, void *p_data )
     intf_sys_t    *p_sys  = (intf_sys_t*) p_intf->p_sys;
 
     vlc_mutex_lock( &p_sys->lock );
-    vlc_array_append( &p_sys->watches, p_watch );
+    vlc_array_append_or_abort( &p_sys->watches, p_watch );
     vlc_mutex_unlock( &p_sys->lock );
 
     return TRUE;
@@ -942,7 +942,7 @@ static int InputCallback( vlc_object_t *p_this, const char *psz_var,
         p_info->signal = SIGNAL_STATE;
     }
     if( p_info->signal )
-        vlc_array_append( &p_sys->events, p_info );
+        vlc_array_append_or_abort( &p_sys->events, p_info );
     else
         free( p_info );
     vlc_mutex_unlock( &p_intf->p_sys->lock );
@@ -1003,7 +1003,7 @@ static int AllCallback( vlc_object_t *p_this, const char *psz_var,
     // Append the event
     *p_info = info;
     vlc_mutex_lock( &p_intf->p_sys->lock );
-    vlc_array_append( &p_intf->p_sys->events, p_info );
+    vlc_array_append_or_abort( &p_intf->p_sys->events, p_info );
     vlc_mutex_unlock( &p_intf->p_sys->lock );
 
     wakeup_main_loop( p_intf );
diff --git a/modules/gui/qt/adapters/chromaprint.cpp b/modules/gui/qt/adapters/chromaprint.cpp
index e08d968cac..e5fb604202 100644
--- a/modules/gui/qt/adapters/chromaprint.cpp
+++ b/modules/gui/qt/adapters/chromaprint.cpp
@@ -58,7 +58,11 @@ bool Chromaprint::enqueue( input_item_t *p_item )
     if ( ! p_r ) return false;
     mtime_t t = input_item_GetDuration( p_item );
     if ( t ) p_r->i_duration = (unsigned int) ( t / 1000000 );
-    p_fingerprinter->pf_enqueue( p_fingerprinter, p_r );
+    if( !p_fingerprinter->pf_enqueue( p_fingerprinter, p_r ) )
+    {
+        fingerprint_request_Delete( p_r );
+        return false;
+    }
     return true;
 }
 
diff --git a/modules/misc/fingerprinter.c b/modules/misc/fingerprinter.c
index 593b2ff8cc..985674074c 100644
--- a/modules/misc/fingerprinter.c
+++ b/modules/misc/fingerprinter.c
@@ -80,22 +80,25 @@ vlc_module_end ()
  * Requests lifecycle
  *****************************************************************************/
 
-static void EnqueueRequest( fingerprinter_thread_t *f, fingerprint_request_t *r )
+static bool EnqueueRequest( fingerprinter_thread_t *f, fingerprint_request_t *r )
 {
     fingerprinter_sys_t *p_sys = f->p_sys;
     vlc_mutex_lock( &p_sys->incoming.lock );
-    vlc_array_append( &p_sys->incoming.queue, r );
+    bool b_ret = vlc_array_append( &p_sys->incoming.queue, r );
     vlc_mutex_unlock( &p_sys->incoming.lock );
+    return b_ret;
 }
 
 static void QueueIncomingRequests( fingerprinter_sys_t *p_sys )
 {
     vlc_mutex_lock( &p_sys->incoming.lock );
-    size_t i = vlc_array_count( &p_sys->incoming.queue );
 
-    while( i )
-        vlc_array_append( &p_sys->processing.queue,
-                          vlc_array_item_at_index( &p_sys->incoming.queue, --i ) );
+    for( size_t i = vlc_array_count( &p_sys->incoming.queue ); i > 0 ; i-- )
+    {
+        fingerprint_request_t *r = vlc_array_item_at_index( &p_sys->incoming.queue, i - 1 );
+        if( !vlc_array_append( &p_sys->processing.queue, r ) )
+            fingerprint_request_Delete( r );
+    }
     vlc_array_clear( &p_sys->incoming.queue );
     vlc_mutex_unlock(&p_sys->incoming.lock);
 }
@@ -313,7 +316,8 @@ static void fill_metas_with_results( fingerprint_request_t *p_r, acoustid_finger
                 vlc_meta_Set( p_meta, vlc_meta_Title, p_record->psz_title );
                 vlc_meta_Set( p_meta, vlc_meta_Artist, p_record->psz_artist );
                 vlc_meta_AddExtra( p_meta, "musicbrainz-id", p_record->s_musicbrainz_id );
-                vlc_array_append( & p_r->results.metas_array, p_meta );
+                if( !vlc_array_append( & p_r->results.metas_array, p_meta ) )
+                    vlc_meta_Delete( p_meta );
             }
         }
     }
@@ -370,7 +374,8 @@ static void *Run( void *opaque )
 
             /* copy results */
             vlc_mutex_lock( &p_sys->results.lock );
-            vlc_array_append( &p_sys->results.queue, p_data );
+            if( !vlc_array_append( &p_sys->results.queue, p_data ) )
+                fingerprint_request_Delete( p_data );
             vlc_mutex_unlock( &p_sys->results.lock );
 
             vlc_testcancel();
diff --git a/modules/mux/asf.c b/modules/mux/asf.c
index 4514cdf274..1b2f996612 100644
--- a/modules/mux/asf.c
+++ b/modules/mux/asf.c
@@ -620,9 +620,15 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             return VLC_EGENERIC;
     }
 
+    if( !vlc_array_append( &p_sys->tracks, tk ) )
+    {
+        free( tk->p_extra );
+        free( tk );
+        return VLC_EGENERIC;
+    }
+
     es_format_Copy( &tk->fmt, p_input->p_fmt );
 
-    vlc_array_append( &p_sys->tracks, tk );
     tk->i_id = vlc_array_index_of_item( &p_sys->tracks, tk ) + 1;
 
     if( p_sys->b_asf_http )
diff --git a/modules/services_discovery/microdns.c b/modules/services_discovery/microdns.c
index 7fe5feb0bb..e9b7ecc567 100644
--- a/modules/services_discovery/microdns.c
+++ b/modules/services_discovery/microdns.c
@@ -174,7 +174,7 @@ items_add_input( struct discovery_sys *p_sys, services_discovery_t *p_sd,
     p_item->p_input_item = p_input_item;
     p_item->p_renderer_item = NULL;
     p_item->i_last_seen = mdate();
-    vlc_array_append( &p_sys->items, p_item );
+    vlc_array_append_or_abort( &p_sys->items, p_item );
     services_discovery_AddItem( p_sd, p_input_item );
 
     return VLC_SUCCESS;
@@ -206,7 +206,7 @@ items_add_renderer( struct discovery_sys *p_sys, vlc_renderer_discovery_t *p_rd,
     p_item->p_input_item = NULL;
     p_item->p_renderer_item = p_renderer_item;
     p_item->i_last_seen = mdate();
-    vlc_array_append( &p_sys->items, p_item );
+    vlc_array_append_or_abort( &p_sys->items, p_item );
     vlc_rd_add_item( p_rd, p_renderer_item );
 
     return VLC_SUCCESS;
diff --git a/modules/stream_filter/hds/hds.c b/modules/stream_filter/hds/hds.c
index 7112bad08c..0add2b50d3 100644
--- a/modules/stream_filter/hds/hds.c
+++ b/modules/stream_filter/hds/hds.c
@@ -1565,7 +1565,7 @@ static int parse_Manifest( stream_t *s, manifest_t *m )
 
                 new_stream->bitrate = medias[i].bitrate;
 
-                vlc_array_append( &sys->hds_streams, new_stream );
+                vlc_array_append_or_abort( &sys->hds_streams, new_stream );
 
                 msg_Info( (vlc_object_t*)s, "New track with quality_segment(%s), bitrate(%u), timescale(%u), movie_id(%s), segment_run_count(%d), fragment_run_count(%u)",
                           new_stream->quality_segment_modifier?new_stream->quality_segment_modifier:"", new_stream->bitrate, new_stream->timescale,
diff --git a/src/input/mrl_helpers.h b/src/input/mrl_helpers.h
index 09916c6070..466b1ff204 100644
--- a/src/input/mrl_helpers.h
+++ b/src/input/mrl_helpers.h
@@ -130,7 +130,11 @@ mrl_FragmentSplit( vlc_array_t* out_items,
         if( unlikely( !decoded ) || !vlc_uri_decode( decoded ) )
             goto error;
 
-        vlc_array_append( out_items, decoded );
+        if( !vlc_array_append( out_items, decoded ) )
+        {
+            free( decoded );
+            goto error;
+        }
         payload += len;
     }
 
diff --git a/src/interface/dialog.c b/src/interface/dialog.c
index 9d9f4a7704..23496ed360 100644
--- a/src/interface/dialog.c
+++ b/src/interface/dialog.c
@@ -188,13 +188,19 @@ dialog_add_locked(vlc_dialog_provider *p_provider, enum dialog_type i_type)
 
     if (p_id == NULL)
         return NULL;
+
+    if(!vlc_array_append(&p_provider->dialog_array, p_id))
+    {
+        free(p_id);
+        return NULL;
+    }
+
     vlc_mutex_init(&p_id->lock);
     vlc_cond_init(&p_id->wait);
 
     p_id->i_type = i_type;
     p_id->i_refcount = 2; /* provider and callbacks */
 
-    vlc_array_append(&p_provider->dialog_array, p_id);
     return p_id;
 }
 
diff --git a/src/misc/background_worker.c b/src/misc/background_worker.c
index 95ff7dcc44..1a9581b39d 100644
--- a/src/misc/background_worker.c
+++ b/src/misc/background_worker.c
@@ -218,8 +218,13 @@ int background_worker_Push( struct background_worker* worker, void* entity,
     item->timeout = timeout < 0 ? worker->conf.default_timeout : timeout;
 
     vlc_mutex_lock( &worker->lock );
-    vlc_array_append( &worker->tail.data, item );
+    bool b_appended = vlc_array_append( &worker->tail.data, item );
     vlc_cond_signal( &worker->tail.wait );
+    if( !b_appended )
+    {
+        free( item );
+        return VLC_EGENERIC;
+    }
 
     if( worker->head.active == false )
     {
diff --git a/src/misc/httpcookies.c b/src/misc/httpcookies.c
index 2e4d3a8663..814de8b14b 100644
--- a/src/misc/httpcookies.c
+++ b/src/misc/httpcookies.c
@@ -340,11 +340,14 @@ bool vlc_http_cookies_store(vlc_http_cookie_jar_t *p_jar, const char *cookies,
             break;
         }
     }
-    vlc_array_append( &p_jar->cookies, cookie );
+
+    bool b_ret = vlc_array_append( &p_jar->cookies, cookie );
+    if( !b_ret )
+        cookie_destroy( cookie );
 
     vlc_mutex_unlock( &p_jar->lock );
 
-    return true;
+    return b_ret;
 }
 
 char *vlc_http_cookies_fetch(vlc_http_cookie_jar_t *p_jar, bool secure,
diff --git a/src/stream_output/stream_output.c b/src/stream_output/stream_output.c
index a30e66852c..046ea2071f 100644
--- a/src/stream_output/stream_output.c
+++ b/src/stream_output/stream_output.c
@@ -852,8 +852,8 @@ sout_stream_t *sout_StreamChainNew(sout_instance_t *p_sout, const char *psz_chai
         free( psz_parser );
         psz_parser = psz_rest_chain;
 
-        vlc_array_append(&cfg, p_cfg);
-        vlc_array_append(&name, psz_name);
+        vlc_array_append_or_abort(&cfg, p_cfg);
+        vlc_array_append_or_abort(&name, psz_name);
     }
 
     size_t i = vlc_array_count(&name);
@@ -870,7 +870,7 @@ sout_stream_t *sout_StreamChainNew(sout_instance_t *p_sout, const char *psz_chai
         if(i == vlc_array_count(&name) - 1 && pp_last)
             *pp_last = p_next;   /* last module created in the chain */
 
-        vlc_array_append(&module, p_next);
+        vlc_array_append_or_abort(&module, p_next);
     }
 
     vlc_array_clear(&name);
diff --git a/src/video_output/video_output.c b/src/video_output/video_output.c
index 12539768db..0088ee9cca 100644
--- a/src/video_output/video_output.c
+++ b/src/video_output/video_output.c
@@ -747,7 +747,7 @@ static void ThreadChangeFilters(vout_thread_t *vout,
         if (likely(e))
         {
             free(config_ChainCreate(&e->name, &e->cfg, "deinterlace"));
-            vlc_array_append(&array_static, e);
+            vlc_array_append_or_abort(&array_static, e);
         }
     }
 
@@ -764,9 +764,9 @@ static void ThreadChangeFilters(vout_thread_t *vout,
                 e->name = name;
                 e->cfg  = cfg;
                 if (!strcmp(e->name, "postproc"))
-                    vlc_array_append(&array_static, e);
+                    vlc_array_append_or_abort(&array_static, e);
                 else
-                    vlc_array_append(&array_interactive, e);
+                    vlc_array_append_or_abort(&array_interactive, e);
             }
             else {
                 if (cfg)



More information about the vlc-commits mailing list