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 ) \
{ \
size_t len = strlen( string ); \
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 */
#define INSERT_STRING_NO_FREE( string ) \
{ \
size_t len = strlen( string ); \
dst = xrealloc( dst, i_size = i_size + len );\
memcpy( dst+d, string, len ); \
d += len; \
}
char *str_format_meta( input_thread_t *p_input, const char *s )
{ {
char *dst = strdup( s ); if (item == NULL)
if( unlikely(dst == NULL) ) return EOF;
char *value = input_item_GetMeta(item, type);
if (value == NULL)
return EOF;
int ret = fputs(value, stream);
free(value);
return ret;
}
char *str_format_meta(input_thread_t *input, const char *s)
{
char *str;
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')
{
s++;
if (!b_is_format)
{ {
if( b_is_format ) if (c == '$')
{ {
switch( *s ) b_is_format = true;
b_empty_if_na = false;
continue;
}
fputc(c, stream);
continue;
}
b_is_format = false;
switch (c)
{ {
case 'a': case 'a':
if( p_item ) write_meta(stream, item, vlc_meta_Artist);
INSERT_STRING( input_item_GetArtist( p_item ) );
break; break;
case 'b': case 'b':
if( p_item ) write_meta(stream, item, vlc_meta_Album);
INSERT_STRING( input_item_GetAlbum( p_item ) );
break; break;
case 'c': case 'c':
if( p_item ) write_meta(stream, item, vlc_meta_Copyright);
INSERT_STRING( input_item_GetCopyright( p_item ) );
break; break;
case 'd': case 'd':
if( p_item ) write_meta(stream, item, vlc_meta_Description);
INSERT_STRING( input_item_GetDescription( p_item ) );
break; break;
case 'e': case 'e':
if( p_item ) write_meta(stream, item, vlc_meta_EncodedBy);
INSERT_STRING( input_item_GetEncodedBy( p_item ) );
break; break;
case 'f': case 'f':
if( p_item && p_item->p_stats ) if (item != NULL && item->p_stats != NULL)
{ {
vlc_mutex_lock( &p_item->p_stats->lock ); vlc_mutex_lock(&item->p_stats->lock);
snprintf( buf, 10, "%"PRIi64, fprintf(stream, "%"PRIi64,
p_item->p_stats->i_displayed_pictures ); item->p_stats->i_displayed_pictures);
vlc_mutex_unlock( &p_item->p_stats->lock ); vlc_mutex_unlock(&item->p_stats->lock);
} }
else else if (!b_empty_if_na)
strcpy( buf, b_empty_if_na ? "" : "-" ); fputc('-', stream);
INSERT_STRING_NO_FREE( buf );
break; break;
case 'g': case 'g':
if( p_item ) write_meta(stream, item, vlc_meta_Genre);
INSERT_STRING( input_item_GetGenre( p_item ) );
break; break;
case 'l': case 'l':
if( p_item ) write_meta(stream, item, vlc_meta_Language);
INSERT_STRING( input_item_GetLanguage( p_item ) );
break; break;
case 'n': case 'n':
if( p_item ) write_meta(stream, item, vlc_meta_TrackNumber);
INSERT_STRING( input_item_GetTrackNum( p_item ) );
break; break;
case 'p': case 'p':
if( p_item ) write_meta(stream, item, vlc_meta_NowPlaying);
INSERT_STRING( input_item_GetNowPlaying( p_item ) );
break; break;
case 'r': case 'r':
if( p_item ) write_meta(stream, item, vlc_meta_Rating);
INSERT_STRING( input_item_GetRating( p_item ) );
break; break;
case 's': case 's':
{ {
char *psz_lang = NULL; char *lang = NULL;
if( p_input )
psz_lang = var_GetNonEmptyString( p_input, "sub-language" ); if (input != NULL)
if( psz_lang == NULL ) lang = var_GetNonEmptyString(input, "sub-language");
psz_lang = strdup( b_empty_if_na ? "" : "-" ); if (lang != NULL)
INSERT_STRING( psz_lang ); {
fputs(lang, stream);
free(lang);
}
else if (!b_empty_if_na)
fputc('-', stream);
break; break;
} }
case 't': case 't':
if( p_item ) write_meta(stream, item, vlc_meta_Title);
INSERT_STRING( input_item_GetTitle( p_item ) );
break; break;
case 'u': case 'u':
if( p_item ) write_meta(stream, item, vlc_meta_URL);
INSERT_STRING( input_item_GetURL( p_item ) );
break; break;
case 'A': case 'A':
if( p_item ) write_meta(stream, item, vlc_meta_Date);
INSERT_STRING( input_item_GetDate( p_item ) );
break; break;
case 'B': case 'B':
if( p_input ) if (input != NULL)
snprintf( buf, 10, "%"PRId64, fprintf(stream, "%"PRId64,
var_GetInteger( p_input, "bit-rate" )/1000 ); var_GetInteger(input, "bit-rate") / 1000);
else else if (!b_empty_if_na)
strcpy( buf, b_empty_if_na ? "" : "-" ); fputc('-', stream);
INSERT_STRING_NO_FREE( buf );
break; break;
case 'C': case 'C':
if( p_input ) if (input != NULL)
snprintf( buf, 10, "%"PRId64, fprintf(stream, "%"PRId64,
var_GetInteger( p_input, "chapter" ) ); var_GetInteger(input, "chapter"));
else else if (!b_empty_if_na)
strcpy( buf, b_empty_if_na ? "" : "-" ); fputc('-', stream);
INSERT_STRING_NO_FREE( buf );
break; break;
case 'D': case 'D':
if( p_item ) if (item != NULL)
{ write_duration(stream, input_item_GetDuration(item));
mtime_t i_duration = input_item_GetDuration( p_item ); else if (!b_empty_if_na)
format_duration (buf, sizeof (buf), i_duration); fputs("--:--:--", stream);
}
else
strcpy( buf, b_empty_if_na ? "" : "--:--:--" );
INSERT_STRING_NO_FREE( buf );
break; break;
case 'F': case 'F':
if( p_item ) if (item != NULL)
INSERT_STRING( input_item_GetURI( p_item ) ); {
char *uri = input_item_GetURI(item);
if (uri != NULL)
{
fputs(uri, stream);
free(uri);
}
}
break; break;
case 'I': case 'I':
if( p_input ) if (input != NULL)
snprintf( buf, 10, "%"PRId64, fprintf(stream, "%"PRId64, var_GetInteger(input, "title"));
var_GetInteger( p_input, "title" ) ); else if (!b_empty_if_na)
else fputc('-', stream);
strcpy( buf, b_empty_if_na ? "" : "-" );
INSERT_STRING_NO_FREE( buf );
break; break;
case 'L': case 'L':
if( p_item && p_input ) if (item != NULL)
{ {
mtime_t i_duration = input_item_GetDuration( p_item ); assert(input != NULL);
int64_t i_time = var_GetTime( p_input, "time" ); write_duration(stream, input_item_GetDuration(item)
format_duration( buf, sizeof(buf), - var_GetTime(input, "time"));
i_duration - i_time );
} }
else else if (!b_empty_if_na)
strcpy( buf, b_empty_if_na ? "" : "--:--:--" ); fputs("--:--:--", stream);
INSERT_STRING_NO_FREE( buf );
break; break;
case 'N': case 'N':
if( p_item ) if (item != NULL)
INSERT_STRING( input_item_GetName( p_item ) ); {
char *name = input_item_GetName(item);
if (name != NULL)
{
fputs(name, stream);
free(name);
}
}
break; break;
case 'O': case 'O':
{ {
char *lang = NULL; char *lang = NULL;
if( p_input )
lang = var_GetNonEmptyString( p_input, if (input != NULL)
"audio-language" ); lang = var_GetNonEmptyString(input, "audio-language");
if( lang == NULL ) if (lang != NULL)
lang = strdup( b_empty_if_na ? "" : "-" ); {
INSERT_STRING( lang ); fputs(lang, stream);
free(lang);
}
else if (!b_empty_if_na)
fputc('-', stream);
break; break;
} }
case 'P': case 'P':
if( p_input ) if (input != NULL)
snprintf( buf, 10, "%2.1lf", fprintf(stream, "%2.1f",
var_GetFloat( p_input, "position" ) * 100. ); var_GetFloat(input, "position") * 100.f);
else else if (!b_empty_if_na)
snprintf( buf, 10, b_empty_if_na ? "" : "--.-%%" ); fputs("--.-%", stream);
INSERT_STRING_NO_FREE( buf );
break; break;
case 'R': case 'R':
if( p_input ) if (input != NULL)
{ fprintf(stream, "%.3f", var_GetFloat(input, "rate"));
float f = var_GetFloat( p_input, "rate" ); else if (!b_empty_if_na)
snprintf( buf, 10, "%.3f", f ); fputc('-', stream);
}
else
strcpy( buf, b_empty_if_na ? "" : "-" );
INSERT_STRING_NO_FREE( buf );
break; break;
case 'S': case 'S':
if( p_input ) if (input != NULL)
{ {
int r = var_GetInteger( p_input, "sample-rate" ); int rate = var_GetInteger(input, "sample-rate");
snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 ); div_t dr = div((rate + 50) / 100, 10);
fprintf(stream, "%d.%01d", dr.quot, dr.rem);
} }
else else if (!b_empty_if_na)
strcpy( buf, b_empty_if_na ? "" : "-" ); fputc('-', stream);
INSERT_STRING_NO_FREE( buf );
break; break;
case 'T': case 'T':
if( p_input ) if (input != NULL)
{ write_duration(stream, var_GetTime(input, "time"));
int64_t i_time = var_GetTime( p_input, "time" ); else if (!b_empty_if_na)
format_duration( buf, sizeof(buf), i_time ); fputs("--:--:--", stream);
}
else
strcpy( buf, b_empty_if_na ? "" : "--:--:--" );
INSERT_STRING_NO_FREE( buf );
break; break;
case 'U': case 'U':
if( p_item ) write_meta(stream, item, vlc_meta_Publisher);
INSERT_STRING( input_item_GetPublisher( p_item ) );
break; break;
case 'V': case 'V':
{ {
float vol = 0.f; float vol = 0.f;
if( p_input ) if (input != NULL)
{ {
audio_output_t *aout = input_GetAout( p_input ); audio_output_t *aout = input_GetAout(input);
if( aout ) if (aout != NULL)
{ {
vol = aout_VolumeGet( aout ); vol = aout_VolumeGet(aout);
vlc_object_release( aout ); vlc_object_release(aout);
} }
} }
if( vol >= 0.f ) if (vol >= 0.f)
{ fprintf(stream, "%ld", lroundf(vol * 256.f));
snprintf( buf, 10, "%ld", lroundf(vol * 256.f) ); else if (!b_empty_if_na)
INSERT_STRING_NO_FREE( buf ); fputs("---", stream);
}
else
INSERT_STRING_NO_FREE( "---" );
break; break;
} }
case '_': case '_':
*(dst+d) = '\n'; fputc('\n', stream);
d++;
break; break;
case 'Z': case 'Z':
if( p_item ) if (write_meta(stream, item, vlc_meta_NowPlaying) == EOF)
{
char *psz_now_playing = input_item_GetNowPlaying( p_item );
if( EMPTY_STR( psz_now_playing ) )
{ {
char *psz_temp = input_item_GetTitleFbName( p_item ); char *title = input_item_GetTitleFbName(item);
char *psz_artist = input_item_GetArtist( p_item );
if( !EMPTY_STR( psz_artist ) ) if (write_meta(stream, item, vlc_meta_Artist) >= 0
&& title != NULL)
fputs(" - ", stream);
if (title != NULL)
{ {
INSERT_STRING( psz_artist ); fputs(title, stream);
if ( !EMPTY_STR( psz_temp ) ) free(title);
INSERT_STRING_NO_FREE( " - " );
} }
INSERT_STRING( psz_temp );
}
else
INSERT_STRING( psz_now_playing );
} }
break; break;
case ' ': case ' ':
b_empty_if_na = true; b_empty_if_na = true;
b_is_format = true;
break; break;
default: default:
*(dst+d) = *s; fputc(c, stream);
d++;
break; break;
} }
if( *s != ' ' )
b_is_format = false;
}
else if( *s == '$' )
{
b_is_format = true;
b_empty_if_na = false;
} }
else
#ifdef HAVE_OPEN_MEMSTREAM
return (fclose(stream) == 0) ? str : NULL;
#else
len = ftell(stream);
if (len != (size_t)-1)
{ {
*(dst+d) = *s; rewind(stream);
d++; str = xmalloc(len + 1);
fread(str, len, 1, stream);
str[len] = '\0';
} }
s++; fclose(stream);
} return str;
*(dst+d) = '\0'; #endif
return dst;
} }
#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