[vlc-devel] [PATCH 3/3] subtitle-demux: fix memory leaks (fixes #11908)

Tristan Matthews le.businessman at gmail.com
Wed Sep 10 05:45:17 CEST 2014


Hi, see comments inline

On Mon, Sep 8, 2014 at 8:32 PM, Hannes Domani <ssbssa at yahoo.de> wrote:
> p_sys->psz_header was never freed in the loop, nor at the end.
>
> And the use of asprintf() slows the loop down tremendously (at
> least with the example of #11908).

> @@ -1082,6 +1083,7 @@ static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
>  {
>      demux_sys_t *p_sys = p_demux->p_sys;
>      text_t      *txt = &p_sys->txt;
> +    int header_len = 0;

Should be a size_t.

>
>      for( ;; )
>      {
> @@ -1154,11 +1156,14 @@ static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
>          free( psz_text );
>
>          /* All the other stuff we add to the header field */
> -        char *psz_header;
> -        if( asprintf( &psz_header, "%s%s\n",
> -                       p_sys->psz_header ? p_sys->psz_header : "", s ) == -1 )
> +        int s_len = strlen( s );

Same here.

> +        p_sys->psz_header = realloc_or_free( p_sys->psz_header, header_len + s_len + 2 );
> +        if( !p_sys->psz_header )
>              return VLC_ENOMEM;
> -        p_sys->psz_header = psz_header;
> +        memcpy( p_sys->psz_header + header_len, s, s_len );
> +        header_len += s_len;
> +        memcpy( p_sys->psz_header + header_len, "\n", 2 );
> +        header_len++;

Could this be simplified into a single snprintf, followed by
increasing header_len?

Best,
Tristan



More information about the vlc-devel mailing list