Commit 24cb4c71 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

str_format_meta: rewrite and fix leaks due to expansion-unsafe macros

(cherry picked from commit 1d410d6d4cb3de58125cbb5db431d6ad9381b0e0)

Conflicts:
	configure.ac
parent e35772f8
...@@ -529,7 +529,7 @@ need_libc=false ...@@ -529,7 +529,7 @@ need_libc=false
dnl Check for usual libc functions dnl Check for usual libc functions
AC_CHECK_DECLS([nanosleep],,,[#include <time.h>]) AC_CHECK_DECLS([nanosleep],,,[#include <time.h>])
AC_CHECK_FUNCS([daemon fcntl fstatvfs fork getenv getpwuid_r isatty lstat memalign mmap openat pread posix_fadvise posix_madvise setlocale stricmp strnicmp strptime uselocale]) AC_CHECK_FUNCS([daemon fcntl fstatvfs fork getenv getpwuid_r isatty lstat memalign mmap open_memstream openat pread posix_fadvise posix_madvise setlocale stricmp strnicmp strptime uselocale])
AC_REPLACE_FUNCS([atof atoll dirfd fdopendir flockfile fsync getdelim getpid gmtime_r lldiv localtime_r nrand48 poll posix_memalign rewind setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strsep strtof strtok_r strtoll swab tdestroy strverscmp]) AC_REPLACE_FUNCS([atof atoll dirfd fdopendir flockfile fsync getdelim getpid gmtime_r lldiv localtime_r nrand48 poll posix_memalign rewind setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strsep strtof strtok_r strtoll swab tdestroy strverscmp])
AC_CHECK_FUNCS(fdatasync,, AC_CHECK_FUNCS(fdatasync,,
[AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.]) [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
......
...@@ -502,314 +502,305 @@ char *str_format_time( const char *tformat ) ...@@ -502,314 +502,305 @@ char *str_format_time( const char *tformat )
assert (0); assert (0);
} }
static void format_duration (char *buf, size_t len, int64_t duration) static void write_duration(FILE *stream, int64_t duration)
{ {
lldiv_t d; lldiv_t d;
int sec; long long sec;
duration /= CLOCK_FREQ; duration /= CLOCK_FREQ;
d = lldiv (duration, 60); d = lldiv(duration, 60);
sec = d.rem; sec = d.rem;
d = lldiv (d.quot, 60); d = lldiv(d.quot, 60);
snprintf (buf, len, "%02lld:%02d:%02d", d.quot, (int)d.rem, sec); fprintf(stream, "%02lld:%02lld:%02lld", d.quot, d.rem, sec);
} }
#define INSERT_STRING( string ) \ static int write_meta(FILE *stream, input_item_t *item, vlc_meta_type_t type)
if( string != NULL ) \ {
{ \ if (item == NULL)
size_t len = strlen( string ); \ return EOF;
dst = xrealloc( dst, i_size = i_size + len );\
memcpy( (dst+d), string, len ); \
d += len; \
free( string ); \
}
/* same than INSERT_STRING, except that string won't be freed */ char *value = input_item_GetMeta(item, type);
#define INSERT_STRING_NO_FREE( string ) \ if (value == NULL)
{ \ return EOF;
size_t len = strlen( string ); \
dst = xrealloc( dst, i_size = i_size + len );\ int ret = fputs(value, stream);
memcpy( dst+d, string, len ); \ free(value);
d += len; \ return ret;
} }
char *str_format_meta( input_thread_t *p_input, const char *s )
char *str_format_meta(input_thread_t *input, const char *s)
{ {
char *dst = strdup( s ); char *str;
if( unlikely(dst == NULL) ) size_t len;
#ifdef HAVE_OPEN_MEMSTREAM
FILE *stream = open_memstream(&str, &len);
#else
FILE *stream = tmpfile();
#endif
if (stream == NULL)
return NULL; return NULL;
input_item_t *p_item = p_input ? input_GetItem(p_input) : NULL; input_item_t *item = (input != NULL) ? input_GetItem(input) : NULL;
size_t i_size = strlen( s ) + 1; /* +1 to store '\0' */
size_t d = 0;
char c;
bool b_is_format = false; bool b_is_format = false;
bool b_empty_if_na = false; bool b_empty_if_na = false;
char buf[10];
while( *s ) while ((c = *s) != '\0')
{ {
if( b_is_format ) s++;
if (!b_is_format)
{ {
switch( *s ) if (c == '$')
{ {
case 'a': b_is_format = true;
if( p_item ) b_empty_if_na = false;
INSERT_STRING( input_item_GetArtist( p_item ) ); continue;
break; }
case 'b':
if( p_item ) fputc(c, stream);
INSERT_STRING( input_item_GetAlbum( p_item ) ); continue;
break; }
case 'c':
if( p_item ) b_is_format = false;
INSERT_STRING( input_item_GetCopyright( p_item ) );
break; switch (c)
case 'd': {
if( p_item ) case 'a':
INSERT_STRING( input_item_GetDescription( p_item ) ); write_meta(stream, item, vlc_meta_Artist);
break; break;
case 'e': case 'b':
if( p_item ) write_meta(stream, item, vlc_meta_Album);
INSERT_STRING( input_item_GetEncodedBy( p_item ) ); break;
break; case 'c':
case 'f': write_meta(stream, item, vlc_meta_Copyright);
if( p_item && p_item->p_stats ) break;
{ case 'd':
vlc_mutex_lock( &p_item->p_stats->lock ); write_meta(stream, item, vlc_meta_Description);
snprintf( buf, 10, "%"PRIi64, break;
p_item->p_stats->i_displayed_pictures ); case 'e':
vlc_mutex_unlock( &p_item->p_stats->lock ); write_meta(stream, item, vlc_meta_EncodedBy);
} break;
else case 'f':
strcpy( buf, b_empty_if_na ? "" : "-" ); if (item != NULL && item->p_stats != NULL)
INSERT_STRING_NO_FREE( buf );
break;
case 'g':
if( p_item )
INSERT_STRING( input_item_GetGenre( p_item ) );
break;
case 'l':
if( p_item )
INSERT_STRING( input_item_GetLanguage( p_item ) );
break;
case 'n':
if( p_item )
INSERT_STRING( input_item_GetTrackNum( p_item ) );
break;
case 'p':
if( p_item )
INSERT_STRING( input_item_GetNowPlaying( p_item ) );
break;
case 'r':
if( p_item )
INSERT_STRING( input_item_GetRating( p_item ) );
break;
case 's':
{
char *psz_lang = NULL;
if( p_input )
psz_lang = var_GetNonEmptyString( p_input, "sub-language" );
if( psz_lang == NULL )
psz_lang = strdup( b_empty_if_na ? "" : "-" );
INSERT_STRING( psz_lang );
break;
}
case 't':
if( p_item )
INSERT_STRING( input_item_GetTitle( p_item ) );
break;
case 'u':
if( p_item )
INSERT_STRING( input_item_GetURL( p_item ) );
break;
case 'A':
if( p_item )
INSERT_STRING( input_item_GetDate( p_item ) );
break;
case 'B':
if( p_input )
snprintf( buf, 10, "%"PRId64,
var_GetInteger( p_input, "bit-rate" )/1000 );
else
strcpy( buf, b_empty_if_na ? "" : "-" );
INSERT_STRING_NO_FREE( buf );
break;
case 'C':
if( p_input )
snprintf( buf, 10, "%"PRId64,
var_GetInteger( p_input, "chapter" ) );
else
strcpy( buf, b_empty_if_na ? "" : "-" );
INSERT_STRING_NO_FREE( buf );
break;
case 'D':
if( p_item )
{
mtime_t i_duration = input_item_GetDuration( p_item );
format_duration (buf, sizeof (buf), i_duration);
}
else
strcpy( buf, b_empty_if_na ? "" : "--:--:--" );
INSERT_STRING_NO_FREE( buf );
break;
case 'F':
if( p_item )
INSERT_STRING( input_item_GetURI( p_item ) );
break;
case 'I':
if( p_input )
snprintf( buf, 10, "%"PRId64,
var_GetInteger( p_input, "title" ) );
else
strcpy( buf, b_empty_if_na ? "" : "-" );
INSERT_STRING_NO_FREE( buf );
break;
case 'L':
if( p_item && p_input )
{
mtime_t i_duration = input_item_GetDuration( p_item );
int64_t i_time = var_GetTime( p_input, "time" );
format_duration( buf, sizeof(buf),
i_duration - i_time );
}
else
strcpy( buf, b_empty_if_na ? "" : "--:--:--" );
INSERT_STRING_NO_FREE( buf );
break;
case 'N':
if( p_item )
INSERT_STRING( input_item_GetName( p_item ) );
break;
case 'O':
{ {
char *lang = NULL; vlc_mutex_lock(&item->p_stats->lock);
if( p_input ) fprintf(stream, "%"PRIi64,
lang = var_GetNonEmptyString( p_input, item->p_stats->i_displayed_pictures);
"audio-language" ); vlc_mutex_unlock(&item->p_stats->lock);
if( lang == NULL )
lang = strdup( b_empty_if_na ? "" : "-" );
INSERT_STRING( lang );
break;
} }
case 'P': else if (!b_empty_if_na)
if( p_input ) fputc('-', stream);
snprintf( buf, 10, "%2.1lf", break;
var_GetFloat( p_input, "position" ) * 100. ); case 'g':
else write_meta(stream, item, vlc_meta_Genre);
snprintf( buf, 10, b_empty_if_na ? "" : "--.-%%" ); break;
INSERT_STRING_NO_FREE( buf ); case 'l':
break; write_meta(stream, item, vlc_meta_Language);
case 'R': break;
if( p_input ) case 'n':
{ write_meta(stream, item, vlc_meta_TrackNumber);
float f = var_GetFloat( p_input, "rate" ); break;
snprintf( buf, 10, "%.3f", f ); case 'p':
} write_meta(stream, item, vlc_meta_NowPlaying);
else break;
strcpy( buf, b_empty_if_na ? "" : "-" ); case 'r':
INSERT_STRING_NO_FREE( buf ); write_meta(stream, item, vlc_meta_Rating);
break; break;
case 'S': case 's':
if( p_input ) {
char *lang = NULL;
if (input != NULL)
lang = var_GetNonEmptyString(input, "sub-language");
if (lang != NULL)
{
fputs(lang, stream);
free(lang);
}
else if (!b_empty_if_na)
fputc('-', stream);
break;
}
case 't':
write_meta(stream, item, vlc_meta_Title);
break;
case 'u':
write_meta(stream, item, vlc_meta_URL);
break;
case 'A':
write_meta(stream, item, vlc_meta_Date);
break;
case 'B':
if (input != NULL)
fprintf(stream, "%"PRId64,
var_GetInteger(input, "bit-rate") / 1000);
else if (!b_empty_if_na)
fputc('-', stream);
break;
case 'C':
if (input != NULL)
fprintf(stream, "%"PRId64,
var_GetInteger(input, "chapter"));
else if (!b_empty_if_na)
fputc('-', stream);
break;
case 'D':
if (item != NULL)
write_duration(stream, input_item_GetDuration(item));
else if (!b_empty_if_na)
fputs("--:--:--", stream);
break;
case 'F':
if (item != NULL)
{
char *uri = input_item_GetURI(item);
if (uri != NULL)
{ {
int r = var_GetInteger( p_input, "sample-rate" ); fputs(uri, stream);
snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 ); free(uri);
} }
else }
strcpy( buf, b_empty_if_na ? "" : "-" ); break;
INSERT_STRING_NO_FREE( buf ); case 'I':
break; if (input != NULL)
case 'T': fprintf(stream, "%"PRId64, var_GetInteger(input, "title"));
if( p_input ) else if (!b_empty_if_na)
fputc('-', stream);
break;
case 'L':
if (item != NULL)
{
assert(input != NULL);
write_duration(stream, input_item_GetDuration(item)
- var_GetTime(input, "time"));
}
else if (!b_empty_if_na)
fputs("--:--:--", stream);
break;
case 'N':
if (item != NULL)
{
char *name = input_item_GetName(item);
if (name != NULL)
{ {
int64_t i_time = var_GetTime( p_input, "time" ); fputs(name, stream);
format_duration( buf, sizeof(buf), i_time ); free(name);
} }
else }
strcpy( buf, b_empty_if_na ? "" : "--:--:--" ); break;
INSERT_STRING_NO_FREE( buf ); case 'O':
break; {
case 'U': char *lang = NULL;
if( p_item )
INSERT_STRING( input_item_GetPublisher( p_item ) ); if (input != NULL)
break; lang = var_GetNonEmptyString(input, "audio-language");
case 'V': if (lang != NULL)
{
fputs(lang, stream);
free(lang);
}
else if (!b_empty_if_na)
fputc('-', stream);
break;
}
case 'P':
if (input != NULL)
fprintf(stream, "%2.1f",
var_GetFloat(input, "position") * 100.f);
else if (!b_empty_if_na)
fputs("--.-%", stream);
break;
case 'R':
if (input != NULL)
fprintf(stream, "%.3f", var_GetFloat(input, "rate"));
else if (!b_empty_if_na)
fputc('-', stream);
break;
case 'S':
if (input != NULL)
{ {
float vol = 0.f; int rate = var_GetInteger(input, "sample-rate");
div_t dr = div((rate + 50) / 100, 10);
if( p_input ) fprintf(stream, "%d.%01d", dr.quot, dr.rem);
{
audio_output_t *aout = input_GetAout( p_input );
if( aout )
{
vol = aout_VolumeGet( aout );
vlc_object_release( aout );
}
}
if( vol >= 0.f )
{
snprintf( buf, 10, "%ld", lroundf(vol * 256.f) );
INSERT_STRING_NO_FREE( buf );
}
else
INSERT_STRING_NO_FREE( "---" );
break;
} }
case '_': else if (!b_empty_if_na)
*(dst+d) = '\n'; fputc('-', stream);
d++; break;
break; case 'T':
case 'Z': if (input != NULL)
if( p_item ) write_duration(stream, var_GetTime(input, "time"));
else if (!b_empty_if_na)
fputs("--:--:--", stream);
break;
case 'U':
write_meta(stream, item, vlc_meta_Publisher);
break;
case 'V':
{
float vol = 0.f;
if (input != NULL)
{
audio_output_t *aout = input_GetAout(input);
if (aout != NULL)
{ {
char *psz_now_playing = input_item_GetNowPlaying( p_item ); vol = aout_VolumeGet(aout);
if( EMPTY_STR( psz_now_playing ) ) vlc_object_release(aout);
{
char *psz_temp = input_item_GetTitleFbName( p_item );
char *psz_artist = input_item_GetArtist( p_item );
if( !EMPTY_STR( psz_artist ) )
{
INSERT_STRING( psz_artist );
if ( !EMPTY_STR( psz_temp ) )
INSERT_STRING_NO_FREE( " - " );
}
INSERT_STRING( psz_temp );
}
else
INSERT_STRING( psz_now_playing );
} }
break; }
if (vol >= 0.f)
fprintf(stream, "%ld", lroundf(vol * 256.f));
else if (!b_empty_if_na)
fputs("---", stream);
break;
}
case '_':
fputc('\n', stream);
break;
case 'Z':
if (write_meta(stream, item, vlc_meta_NowPlaying) == EOF)
{
char *title = input_item_GetTitleFbName(item);
case ' ': if (write_meta(stream, item, vlc_meta_Artist) >= 0
b_empty_if_na = true; && title != NULL)
break; fputs(" - ", stream);
default: if (title != NULL)
*(dst+d) = *s; {
d++; fputs(title, stream);
break; free(title);
} }
if( *s != ' ' ) }
b_is_format = false; break;
} case ' ':
else if( *s == '$' ) b_empty_if_na = true;
{ b_is_format = true;
b_is_format = true; break;
b_empty_if_na = false; default:
} fputc(c, stream);
else break;
{
*(dst+d) = *s;
d++;
} }
s++;
} }
*(dst+d) = '\0';
return dst; #ifdef HAVE_OPEN_MEMSTREAM
return (fclose(stream) == 0) ? str : NULL;
#else
len = ftell(stream);
if (len != (size_t)-1)
{
rewind(stream);
str = xmalloc(len + 1);
fread(str, len, 1, stream);
str[len] = '\0';
}
fclose(stream);
return str;
#endif
} }
#undef INSERT_STRING
#undef INSERT_STRING_NO_FREE
/** /**
* Remove forbidden, potentially forbidden and otherwise evil characters from * Remove forbidden, potentially forbidden and otherwise evil characters from
......
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