[MPlayer-dev-eng] [PATCH] demux_mkv.c: fix crash with gcc 4.4 and optimizations

Reimar Döffinger Reimar.Doeffinger at gmx.de
Wed Sep 30 09:22:05 CEST 2009


On Tue, Sep 29, 2009 at 05:59:12PM -0700, Corey Hickey wrote:
> Index: libmpdemux/demux_mkv.c
> ===================================================================
> --- libmpdemux/demux_mkv.c	(revision 29731)
> +++ libmpdemux/demux_mkv.c	(working copy)
> @@ -203,16 +203,25 @@
>  extern int dvdsub_id;
>  
>  /**
> - * \brief ensures there is space for at least one additional element
> + * \brief ensures there is space for at least one additional uint64_t
>   * \param array array to grow
>   * \param nelem current number of elements in array
> - * \param elsize size of one array element
>   */
> -static void grow_array(void **array, int nelem, size_t elsize) {
> +static void grow_array_uint64(uint64_t **array, int nelem) {
>    if (!(nelem & 31))
> -    *array = realloc(*array, (nelem + 32) * elsize);
> +    *array = realloc(*array, (nelem + 32) * sizeof(uint64_t));
>  }
>  
> +/**
> + * \brief ensures there is space for at least one additional mkv_index_t
> + * \param array array to grow
> + * \param nelem current number of elements in array
> + */
> +static void grow_array_mkv_index(mkv_index_t **array, int nelem) {
> +  if (!(nelem & 31))
> +    *array = realloc(*array, (nelem + 32) * sizeof(mkv_index_t));
> +}
> +

Duplicating the code makes the whole thing rather pointless, the only
reason for the function was avoiding code duplication.
In case -Wstrict-aliasing=2 is reliable, this much simpler patch will
fix it too.
Index: libmpdemux/demux_mkv.c
===================================================================
--- libmpdemux/demux_mkv.c      (revision 29732)
+++ libmpdemux/demux_mkv.c      (working copy)
@@ -204,11 +204,12 @@
 
 /**
  * \brief ensures there is space for at least one additional element
- * \param array array to grow
+ * \param arrayp array to grow
  * \param nelem current number of elements in array
  * \param elsize size of one array element
  */
-static void grow_array(void **array, int nelem, size_t elsize) {
+static void grow_array(void *arrayp, int nelem, size_t elsize) {
+  void **array = arrayp;
   if (!(nelem & 31))
     *array = realloc(*array, (nelem + 32) * elsize);
 }
@@ -235,7 +236,7 @@
     if (mkv_d->cluster_positions[i] == position)
       return;
 
-  grow_array((void **)&mkv_d->cluster_positions, mkv_d->num_cluster_pos,
+  grow_array(&mkv_d->cluster_positions, mkv_d->num_cluster_pos,
              sizeof(uint64_t));
   mkv_d->cluster_positions[mkv_d->num_cluster_pos++] = position;
 }
@@ -1079,7 +1080,7 @@
       if (time != EBML_UINT_INVALID && track != EBML_UINT_INVALID
           && pos != EBML_UINT_INVALID)
         {
-          grow_array((void **)&mkv_d->indexes, mkv_d->num_indexes, sizeof(mkv_index_t));
+          grow_array(&mkv_d->indexes, mkv_d->num_indexes, sizeof(mkv_index_t));
           mkv_d->indexes[mkv_d->num_indexes].tnum = track;
           mkv_d->indexes[mkv_d->num_indexes].timecode = time;
           mkv_d->indexes[mkv_d->num_indexes].filepos =mkv_d->segment_start+pos;



More information about the MPlayer-dev-eng mailing list