]> git.sesse.net Git - vlc/commitdiff
xiph_SplitHeaders: do not malloc+memcpy headers
authorRafaël Carré <funman@videolan.org>
Fri, 25 Oct 2013 18:46:37 +0000 (20:46 +0200)
committerRafaël Carré <funman@videolan.org>
Fri, 25 Oct 2013 19:02:36 +0000 (21:02 +0200)
Directly point into the provided extra data
The headers were always used then freed immediately

modules/codec/kate.c
modules/codec/opus.c
modules/codec/speex.c
modules/codec/theora.c
modules/codec/vorbis.c
modules/demux/ogg.c
modules/demux/xiph.h
modules/mux/ogg.c
modules/stream_out/rtpfmt.c

index 0953c4f2be9bdc5d5e5fa3f9c88c0adf84484f88..1ce6fba2a9a0717de837551489c0ae958f4c54f3 100644 (file)
@@ -515,21 +515,18 @@ static int ProcessHeaders( decoder_t *p_dec )
     if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
         return VLC_EGENERIC;
-    int i_ret = VLC_SUCCESS;
+
     if( i_count < 1 )
-    {
-        i_ret = VLC_EGENERIC;
-        goto end;
-    }
+        return VLC_EGENERIC;
 
     /* Take care of the initial Kate header */
     kp.nbytes = pi_size[0];
     kp.data   = pp_data[0];
-    i_ret = kate_decode_headerin( &p_sys->ki, &p_sys->kc, &kp );
+    int i_ret = kate_decode_headerin( &p_sys->ki, &p_sys->kc, &kp );
     if( i_ret < 0 )
     {
         msg_Err( p_dec, "this bitstream does not contain Kate data (%d)", i_ret );
-        goto end;
+        return VLC_EGENERIC;
     }
 
     msg_Dbg( p_dec, "%s %s text, granule rate %f, granule shift %d",
@@ -546,7 +543,7 @@ static int ProcessHeaders( decoder_t *p_dec )
         if( i_ret < 0 )
         {
             msg_Err( p_dec, "Kate header %d is corrupted: %d", i_headeridx, i_ret );
-            goto end;
+            return VLC_EGENERIC;
         }
 
         /* header 1 is comments */
@@ -581,10 +578,7 @@ static int ProcessHeaders( decoder_t *p_dec )
     }
 #endif
 
-end:
-    for( unsigned i = 0; i < i_count; i++ )
-        free( pp_data[i] );
-    return i_ret < 0 ? VLC_EGENERIC : VLC_SUCCESS;
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
index 3e31ebf8def3302995e50610ec4a8c8e4958c65d..60edb5f39f82d3ef190317192fc6b7bbc7cfd8e9 100644 (file)
@@ -241,13 +241,12 @@ static int ProcessHeaders( decoder_t *p_dec )
     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
     void     *pp_data[XIPH_MAX_HEADER_COUNT];
     unsigned i_count;
-    int ret = VLC_EGENERIC;
 
     if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
         return VLC_EGENERIC;
     if( i_count < 2 )
-        goto end;
+        return VLC_EGENERIC;;
 
     oggpacket.granulepos = -1;
     oggpacket.e_o_s = 0;
@@ -257,15 +256,11 @@ static int ProcessHeaders( decoder_t *p_dec )
     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
     oggpacket.bytes  = pi_size[0];
     oggpacket.packet = pp_data[0];
-    ret = ProcessInitialHeader( p_dec, &oggpacket );
+    int ret = ProcessInitialHeader( p_dec, &oggpacket );
 
     if (ret != VLC_SUCCESS)
         msg_Err( p_dec, "initial Opus header is corrupted" );
 
-end:
-    for( unsigned i = 0; i < i_count; i++ )
-        free( pp_data[i] );
-
     return ret;
 }
 
index 94fc27e1e4131ff5dd2c91866594b0e7971470fd..488b9c248f3207f94ed79a153e5c616c2ff67944 100644 (file)
@@ -326,7 +326,7 @@ static int ProcessHeaders( decoder_t *p_dec )
                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
         return VLC_EGENERIC;
     if( i_count < 2 )
-        goto error;
+        return VLC_EGENERIC;;
 
     oggpacket.granulepos = -1;
     oggpacket.e_o_s = 0;
@@ -339,7 +339,7 @@ static int ProcessHeaders( decoder_t *p_dec )
     if( ProcessInitialHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
     {
         msg_Err( p_dec, "initial Speex header is corrupted" );
-        goto error;
+        return VLC_EGENERIC;;
     }
 
     /* The next packet in order is the comments header */
@@ -357,14 +357,7 @@ static int ProcessHeaders( decoder_t *p_dec )
                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
     }
 
-    for( unsigned i = 0; i < i_count; i++ )
-        free( pp_data[i] );
     return VLC_SUCCESS;
-
-error:
-    for( unsigned i = 0; i < i_count; i++ )
-        free( pp_data[i] );
-    return VLC_EGENERIC;
 }
 
 /*****************************************************************************
index b57578f478fde57c878cc210ee85a374c7bcc811..b6831aebaca66df4573ab8d733d29b151abb9d8f 100644 (file)
@@ -249,7 +249,7 @@ static int ProcessHeaders( decoder_t *p_dec )
                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
         return VLC_EGENERIC;
     if( i_count < 3 )
-        goto error;
+        return VLC_EGENERIC;
 
     oggpacket.granulepos = -1;
     oggpacket.e_o_s = 0;
@@ -404,15 +404,11 @@ static int ProcessHeaders( decoder_t *p_dec )
                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
     }
 
-    for( unsigned i = 0; i < i_count; i++ )
-        free( pp_data[i] );
     /* Clean up the decoder setup info... we're done with it */
     th_setup_free( ts );
     return VLC_SUCCESS;
 
 error:
-    for( unsigned i = 0; i < i_count; i++ )
-        free( pp_data[i] );
     /* Clean up the decoder setup info... we're done with it */
     th_setup_free( ts );
     return VLC_EGENERIC;
index 0d2f020b58b88b25444b5a80a990b8528f57f1a6..a9554b692cd6fbfb08f734480eea67b9e3a83f27 100644 (file)
@@ -336,13 +336,13 @@ static int ProcessHeaders( decoder_t *p_dec )
     ogg_packet oggpacket;
 
     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
-    void     *pp_data[XIPH_MAX_HEADER_COUNT];
+    void *pp_data[XIPH_MAX_HEADER_COUNT];
     unsigned i_count;
     if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
         return VLC_EGENERIC;
     if( i_count < 3 )
-        goto error;
+        return VLC_EGENERIC;
 
     oggpacket.granulepos = -1;
     oggpacket.e_o_s = 0;
@@ -355,7 +355,7 @@ static int ProcessHeaders( decoder_t *p_dec )
     if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
     {
         msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
-        goto error;
+        return VLC_EGENERIC;
     }
 
     /* Setup the format */
@@ -366,7 +366,7 @@ static int ProcessHeaders( decoder_t *p_dec )
     {
         msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
                  p_dec->fmt_out.audio.i_channels );
-        goto error;
+        return VLC_EGENERIC;
     }
 
     p_dec->fmt_out.audio.i_physical_channels =
@@ -386,7 +386,7 @@ static int ProcessHeaders( decoder_t *p_dec )
     if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
     {
         msg_Err( p_dec, "2nd Vorbis header is corrupted" );
-        goto error;
+        return VLC_EGENERIC;
     }
     ParseVorbisComments( p_dec );
 
@@ -420,14 +420,7 @@ static int ProcessHeaders( decoder_t *p_dec )
     ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
             p_dec->fmt_out.audio.i_physical_channels, true);
 
-    for( unsigned i = 0; i < i_count; i++ )
-        free( pp_data[i] );
     return VLC_SUCCESS;
-
-error:
-    for( unsigned i = 0; i < i_count; i++ )
-        free( pp_data[i] );
-    return VLC_EGENERIC;
 }
 
 /*****************************************************************************
index ccfcfb5989f05262a10c471b98dd8f73e3b10998..67e226a98e18cf38a0486887b7ab3615cfab207e 100644 (file)
@@ -1949,10 +1949,6 @@ static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_for
             b_match = false;
     }
 
-    for( unsigned i = 0; i < i_new_count; i++ )
-        free( pp_new_data[i] );
-    for( unsigned i = 0; i < i_old_count; i++ )
-        free( pp_old_data[i] );
     return b_match;
 }
 
@@ -2033,10 +2029,7 @@ static bool Ogg_IsOpusFormatCompatible( const es_format_t *p_new,
                   memcmp(p_old_map, p_new_map,
                       i_new_channel_count*sizeof(*p_new_map)) == 0;
     }
-    for( unsigned i = 0; i < i_new_count; i++ )
-        free( pp_new_data[i] );
-    for( unsigned i = 0; i < i_old_count; i++ )
-        free( pp_old_data[i] );
+
     return b_match;
 }
 
@@ -2114,9 +2107,6 @@ static void Ogg_ExtractXiphMeta( demux_t *p_demux, es_format_t *p_fmt,
     {
         p_demux->info.i_update |= INPUT_UPDATE_TITLE_LIST;
     }
-
-    for( unsigned i = 0; i < i_count; i++ )
-        free( pp_data[i] );
 }
 static void Ogg_ExtractMeta( demux_t *p_demux, es_format_t *p_fmt, const uint8_t *p_headers, int i_headers )
 {
index 8ccc5dadf9356e335466c30754466f2d5240bea9..b00f52cf5a902368311099383a769f90b3b5d49c 100644 (file)
@@ -25,9 +25,9 @@
 #define XIPH_MAX_HEADER_COUNT (256)
 
 static inline int xiph_SplitHeaders(unsigned packet_size[], void *packet[], unsigned *packet_count,
-                                    unsigned extra_size, const void *extra)
+                                    unsigned extra_size, void *extra)
 {
-    const uint8_t *current = (const uint8_t*)extra;
+    uint8_t *current = extra;
     const uint8_t *end = &current[extra_size];
     if (extra_size < 1)
         return VLC_EGENERIC;
@@ -52,19 +52,12 @@ static inline int xiph_SplitHeaders(unsigned packet_size[], void *packet[], unsi
         return VLC_EGENERIC;
     packet_size[count - 1] = end - current - size;
 
-    /* Copy the payloads */
-    for (unsigned i = 0; i < count; i++) {
-        packet[i] = malloc(packet_size[i]);
-        if (!packet[i]) {
-            for (unsigned j = 0; j < i; j++)
-                free(packet[j]);
-            return VLC_ENOMEM;
-        }
+    for (unsigned i = 0; i < count; i++)
         if (packet_size[i] > 0) {
-            memcpy(packet[i], current, packet_size[i]);
+            packet[i] = current;
             current += packet_size[i];
         }
-    }
+
     return VLC_SUCCESS;
 }
 
@@ -131,7 +124,7 @@ static inline int xiph_AppendHeaders(int *extra_size, void **extra,
     if (count >= XIPH_MAX_HEADER_COUNT)
         return VLC_EGENERIC;
 
-    free(*extra);
+    void *old = *extra;
 
     packet_size[count] = size;
     packet[count]      = (void*)data;
@@ -140,8 +133,8 @@ static inline int xiph_AppendHeaders(int *extra_size, void **extra,
         *extra_size = 0;
         *extra      = NULL;
     }
-    for (unsigned i = 0; i < count; i++)
-        free(packet[i]);
+
+    free(old);
 
     if (*extra_size <= 0)
         return VLC_EGENERIC;
index fb4f67e8c70523d798f51b9afb47cfaad67a3f09..7e055e0452ac62891d0ff8a1cf054f15cbafcfd4 100644 (file)
@@ -664,9 +664,6 @@ static block_t *OggCreateHeader( sout_mux_t *p_mux )
                     p_stream->i_keyframe_granule_shift =
                         ( (op.packet[40] & 0x03) << 3 ) | ( (op.packet[41] & 0xe0) >> 5 );
                 }
-
-                for( unsigned i = 0; i < i_count; i++ )
-                    free( pp_data[i] );
             }
             else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
             {
@@ -751,8 +748,6 @@ static block_t *OggCreateHeader( sout_mux_t *p_mux )
                 if( p_og )
                     block_ChainAppend( &p_hdr, p_og );
             }
-            for( unsigned i = 0; i < i_count; i++ )
-                free( pp_data[i] );
         }
         else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
                  p_stream->i_fourcc != VLC_CODEC_DIRAC )
index 7b71588545b3bafd0e558e4455fd887ce3ae843e..cbc19abae3eec7539def673da495b4f89a87b38c 100644 (file)
@@ -65,23 +65,16 @@ static int rtp_xiph_pack_headers(size_t room, void *p_extra, size_t i_extra,
     unsigned packet_size[XIPH_MAX_HEADER_COUNT];
     void *packet[XIPH_MAX_HEADER_COUNT];
     unsigned packet_count;
-    int val = xiph_SplitHeaders(packet_size, packet, &packet_count,
-                                i_extra, p_extra);
-    if (val != VLC_SUCCESS)
-        return val;
+    if (xiph_SplitHeaders(packet_size, packet, &packet_count,
+                                i_extra, p_extra))
+        return VLC_EGENERIC;;
     if (packet_count < 3)
-    {
-        val = VLC_EGENERIC;
-        goto free;
-    }
+        return VLC_EGENERIC;;
 
     if (theora_pixel_fmt != NULL)
     {
         if (packet_size[0] < 42)
-        {
-            val = VLC_EGENERIC;
-            goto free;
-        }
+            return VLC_EGENERIC;
         *theora_pixel_fmt = (((uint8_t *)packet[0])[41] >> 3) & 0x03;
     }
 
@@ -100,10 +93,7 @@ static int rtp_xiph_pack_headers(size_t room, void *p_extra, size_t i_extra,
                 + packet_size[0] + packet_size[1] + packet_size[2];
     *p_buffer = malloc(*i_buffer);
     if (*p_buffer == NULL)
-    {
-        val = VLC_ENOMEM;
-        goto free;
-    }
+        return VLC_ENOMEM;
 
     uint8_t *p = *p_buffer + room;
     /* Number of headers */
@@ -126,12 +116,7 @@ static int rtp_xiph_pack_headers(size_t room, void *p_extra, size_t i_extra,
         p += packet_size[i];
     }
 
-    val = VLC_SUCCESS;
-free:
-    for (unsigned i = 0; i < packet_count; i++)
-        free(packet[i]);
-
-    return val;
+    return VLC_SUCCESS;
 }
 
 static char *rtp_xiph_b64_oob_config(void *p_extra, size_t i_extra,