Commit f173b636 authored by Rafaël Carré's avatar Rafaël Carré

str_format_meta(): fix HUGE memory leak & segfault.

realloc() can change the pointer initialised with malloc() if memory is low !
parent 034015da
...@@ -634,44 +634,38 @@ char *str_format_time( const char *tformat ) ...@@ -634,44 +634,38 @@ char *str_format_time( const char *tformat )
return strdup( buffer ); return strdup( buffer );
} }
#define INSERT_STRING( check, string ) \ #define INSERT_STRING( string ) \
if( check ) \ if( string != NULL ) \
{ \ { \
psz_meta = string; \ int len = strlen( string ); \
if( psz_meta != NULL ) \ dst = realloc( dst, i_size = i_size + len );\
{ \ memcpy( (dst+d), string, len ); \
int len = strlen( string ); \ d += len; \
dst = realloc( dst, \ free( string ); \
i_size = i_size + len + 1 ); \ } \
strncpy( d, psz_meta, len+1 ); \ else \
d += len; \ { \
} \ *(dst+d) = '-'; \
else \ d++; \
{ \ } \
*d = '-'; \
d++; \
} \
}
/* same than INSERT_STRING, except that string won't be freed */ /* same than INSERT_STRING, except that string won't be freed */
#define INSERT_STRING_NO_FREE( string ) \ #define INSERT_STRING_NO_FREE( string ) \
{ \ { \
int len = strlen( string ); \ int len = strlen( string ); \
dst = realloc( dst, \ dst = realloc( dst, i_size = i_size + len );\
i_size = i_size + len + 1 ); \ memcpy( dst+d, string, len ); \
strncpy( d, string, len+1 ); \ d += len; \
d += len; \ }
free( string ); \
}
char *__str_format_meta( vlc_object_t *p_object, const char *string ) char *__str_format_meta( vlc_object_t *p_object, const char *string )
{ {
const char *s = string; const char *s = string;
char *dst = malloc( 1000 );
char *d = dst;
int b_is_format = 0; int b_is_format = 0;
int b_empty_if_na = 0; int b_empty_if_na = 0;
char buf[10]; char buf[10];
int i_size = strlen( string ); int i_size = strlen( string ) + 1; /* +1 to store '\0' */
char *dst = malloc( i_size );
int d = 0;
playlist_t *p_playlist = pl_Yield( p_object ); playlist_t *p_playlist = pl_Yield( p_object );
input_thread_t *p_input = p_playlist->p_input; input_thread_t *p_input = p_playlist->p_input;
...@@ -691,36 +685,65 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string ) ...@@ -691,36 +685,65 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
{ {
switch( *s ) switch( *s )
{ {
char *psz_meta; /* used by INSERT_STRING */
case 'a': case 'a':
INSERT_STRING( p_item, input_item_GetArtist(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetArtist( p_item ) );
}
break; break;
case 'b': case 'b':
INSERT_STRING( p_item, input_item_GetAlbum(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetAlbum( p_item ) );
}
break; break;
case 'c': case 'c':
INSERT_STRING( p_item, input_item_GetCopyright(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetCopyright( p_item ) );
}
break; break;
case 'd': case 'd':
INSERT_STRING( p_item, input_item_GetDescription(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetDescription( p_item ) );
}
break; break;
case 'e': case 'e':
INSERT_STRING( p_item, input_item_GetEncodedBy(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetEncodedBy( p_item ) );
}
break; break;
case 'g': case 'g':
INSERT_STRING( p_item, input_item_GetGenre(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetGenre( p_item ) );
}
break; break;
case 'l': case 'l':
INSERT_STRING( p_item, input_item_GetLanguage(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetLanguage( p_item ) );
}
break; break;
case 'n': case 'n':
INSERT_STRING( p_item, input_item_GetTrackNum(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetTrackNum( p_item ) );
}
break; break;
case 'p': case 'p':
INSERT_STRING( p_item, input_item_GetNowPlaying(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetNowPlaying( p_item ) );
}
break; break;
case 'r': case 'r':
INSERT_STRING( p_item, input_item_GetRating(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetRating( p_item ) );
}
break; break;
case 's': case 's':
{ {
...@@ -729,17 +752,26 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string ) ...@@ -729,17 +752,26 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
lang = var_GetNonEmptyString( p_input, "sub-language" ); lang = var_GetNonEmptyString( p_input, "sub-language" );
if( lang == NULL ) if( lang == NULL )
lang = strdup( b_empty_if_na ? "" : "-" ); lang = strdup( b_empty_if_na ? "" : "-" );
INSERT_STRING( 1, lang ); INSERT_STRING( lang );
break; break;
} }
case 't': case 't':
INSERT_STRING( p_item, input_item_GetTitle(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetTitle( p_item ) );
}
break; break;
case 'u': case 'u':
INSERT_STRING( p_item, input_item_GetURL(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetURL( p_item ) );
}
break; break;
case 'A': case 'A':
INSERT_STRING( p_item, input_item_GetDate(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetDate( p_item ) );
}
break; break;
case 'B': case 'B':
if( p_input ) if( p_input )
...@@ -781,7 +813,10 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string ) ...@@ -781,7 +813,10 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
INSERT_STRING_NO_FREE( buf ); INSERT_STRING_NO_FREE( buf );
break; break;
case 'F': case 'F':
INSERT_STRING( p_item, input_item_GetURI( p_item ) ); if( p_item )
{
INSERT_STRING( input_item_GetURI( p_item ) );
}
break; break;
case 'I': case 'I':
if( p_input ) if( p_input )
...@@ -812,7 +847,10 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string ) ...@@ -812,7 +847,10 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
INSERT_STRING_NO_FREE( buf ); INSERT_STRING_NO_FREE( buf );
break; break;
case 'N': case 'N':
INSERT_STRING( p_item, input_item_GetName( p_item ) ); if( p_item )
{
INSERT_STRING( input_item_GetName( p_item ) );
}
break; break;
case 'O': case 'O':
{ {
...@@ -822,7 +860,7 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string ) ...@@ -822,7 +860,7 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
"audio-language" ); "audio-language" );
if( lang == NULL ) if( lang == NULL )
lang = strdup( b_empty_if_na ? "" : "-" ); lang = strdup( b_empty_if_na ? "" : "-" );
INSERT_STRING( 1, lang ); INSERT_STRING( lang );
break; break;
} }
case 'P': case 'P':
...@@ -876,7 +914,10 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string ) ...@@ -876,7 +914,10 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
INSERT_STRING_NO_FREE( buf ); INSERT_STRING_NO_FREE( buf );
break; break;
case 'U': case 'U':
INSERT_STRING( p_item, input_item_GetPublisher(p_item) ); if( p_item )
{
INSERT_STRING( input_item_GetPublisher( p_item ) );
}
break; break;
case 'V': case 'V':
{ {
...@@ -887,7 +928,7 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string ) ...@@ -887,7 +928,7 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
break; break;
} }
case '_': case '_':
*d = '\n'; *(dst+d) = '\n';
d++; d++;
break; break;
...@@ -896,7 +937,7 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string ) ...@@ -896,7 +937,7 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
break; break;
default: default:
*d = *s; *(dst+d) = *s;
d++; d++;
break; break;
} }
...@@ -910,12 +951,12 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string ) ...@@ -910,12 +951,12 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
} }
else else
{ {
*d = *s; *(dst+d) = *s;
d++; d++;
} }
s++; s++;
} }
*d = '\0'; *(dst+d) = '\0';
if( p_input ) if( p_input )
vlc_object_release( p_input ); vlc_object_release( p_input );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment