Commit 886f411a authored by Thomas Guillem's avatar Thomas Guillem Committed by Jean-Baptiste Kempf

libvlc: fix leaks and crash if no metadata found

you have to free char * returned by libvlc_media_get_meta.
parent d46af887
......@@ -132,6 +132,9 @@ jobjectArray read_track_info_internal(JNIEnv *env, jobject thiz, libvlc_media_t*
libvlc_media_track_t **p_tracks;
int i_nbTracks = libvlc_media_tracks_get(p_m, &p_tracks);
if (i_nbTracks == 0)
return NULL;
jobjectArray array = (*env)->NewObjectArray(env, i_nbTracks + 1, cls, NULL);
unsigned i;
......@@ -147,16 +150,25 @@ jobjectArray read_track_info_internal(JNIEnv *env, jobject thiz, libvlc_media_t*
// use last track for metadata
if (i == i_nbTracks)
{
#define SET_STRING_META(title, vlc_meta) do { \
char *psz_meta = libvlc_media_get_meta(p_m, vlc_meta); \
if (psz_meta) { \
setString(env, item, title, psz_meta); \
free(psz_meta); \
} \
} while (0)
setInt(env, item, "Type", 3 /* TYPE_META */);
setLong(env, item, "Length", libvlc_media_get_duration(p_m));
setString(env, item, "Title", libvlc_media_get_meta(p_m, libvlc_meta_Title));
setString(env, item, "Artist", libvlc_media_get_meta(p_m, libvlc_meta_Artist));
setString(env, item, "Album", libvlc_media_get_meta(p_m, libvlc_meta_Album));
setString(env, item, "Genre", libvlc_media_get_meta(p_m, libvlc_meta_Genre));
setString(env, item, "ArtworkURL", libvlc_media_get_meta(p_m, libvlc_meta_ArtworkURL));
setString(env, item, "NowPlaying", libvlc_media_get_meta(p_m, libvlc_meta_NowPlaying));
setString(env, item, "TrackNumber", libvlc_media_get_meta(p_m, libvlc_meta_TrackNumber));
setString(env, item, "AlbumArtist", libvlc_media_get_meta(p_m, libvlc_meta_AlbumArtist));
SET_STRING_META("Title", libvlc_meta_Title);
SET_STRING_META("Artist", libvlc_meta_Artist);
SET_STRING_META("Album", libvlc_meta_Album);
SET_STRING_META("Genre", libvlc_meta_Genre);
SET_STRING_META("ArtworkURL", libvlc_meta_ArtworkURL);
SET_STRING_META("NowPlaying", libvlc_meta_NowPlaying);
SET_STRING_META("TrackNumber", libvlc_meta_TrackNumber);
SET_STRING_META("AlbumArtist", libvlc_meta_AlbumArtist);
#undef SET_STRING_META
continue;
}
......
......@@ -606,6 +606,8 @@ jstring Java_org_videolan_libvlc_LibVLC_changeset(JNIEnv* env, jobject thiz)
jstring Java_org_videolan_libvlc_LibVLC_getMeta(JNIEnv *env, jobject thiz, int meta)
{
libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
char *psz_meta;
jstring string = NULL;
if (!mp)
return NULL;
......@@ -613,7 +615,12 @@ jstring Java_org_videolan_libvlc_LibVLC_getMeta(JNIEnv *env, jobject thiz, int m
if (!p_mp)
return NULL;
return (*env)->NewStringUTF(env, libvlc_media_get_meta(p_mp, meta));
psz_meta = libvlc_media_get_meta(p_mp, meta);
if (psz_meta) {
string = (*env)->NewStringUTF(env, psz_meta);
free(psz_meta);
}
return string;
}
jint Java_org_videolan_libvlc_LibVLC_getTitle(JNIEnv *env, jobject thiz)
......
......@@ -167,8 +167,14 @@ public class Media implements Comparable<Media> {
}
private void extractTrackInfo(TrackInfo[] tracks) {
if (tracks == null)
if (tracks == null) {
mTitle = null;
mArtist = getValueWrapper(null, UnknownStringType.Artist).trim();
mAlbum = getValueWrapper(null, UnknownStringType.Album).trim();
mGenre = getValueWrapper(null, UnknownStringType.Genre).trim();
mAlbumArtist = getValueWrapper(null, UnknownStringType.AlbumArtist).trim();
return;
}
for (TrackInfo track : tracks) {
if (track.Type == TrackInfo.TYPE_VIDEO) {
......@@ -179,7 +185,7 @@ public class Media implements Comparable<Media> {
mType = TYPE_AUDIO;
} else if (track.Type == TrackInfo.TYPE_META) {
mLength = track.Length;
mTitle = track.Title.trim();
mTitle = track.Title != null ? track.Title.trim() : null;
mArtist = getValueWrapper(track.Artist, UnknownStringType.Artist).trim();
mAlbum = getValueWrapper(track.Album, UnknownStringType.Album).trim();
mGenre = getValueWrapper(track.Genre, UnknownStringType.Genre).trim();
......@@ -301,7 +307,7 @@ public class Media implements Comparable<Media> {
*/
@Override
public int compareTo(Media another) {
return mTitle.toUpperCase(Locale.getDefault()).compareTo(
return getTitle().toUpperCase(Locale.getDefault()).compareTo(
another.getTitle().toUpperCase(Locale.getDefault()));
}
......
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