[Android] libvlc: fix leaks and crash if no metadata found
Thomas Guillem
git at videolan.org
Fri Dec 19 13:36:48 CET 2014
vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Fri Dec 19 10:32:51 2014 +0100| [886f411a9c1d4f1da7e4875886cbdbc712d54fa6] | committer: Jean-Baptiste Kempf
libvlc: fix leaks and crash if no metadata found
you have to free char * returned by libvlc_media_get_meta.
> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=886f411a9c1d4f1da7e4875886cbdbc712d54fa6
---
libvlc/jni/libvlcjni-track.c | 28 ++++++++++++++++++++--------
libvlc/jni/libvlcjni.c | 9 ++++++++-
libvlc/src/org/videolan/libvlc/Media.java | 12 +++++++++---
3 files changed, 37 insertions(+), 12 deletions(-)
diff --git a/libvlc/jni/libvlcjni-track.c b/libvlc/jni/libvlcjni-track.c
index 398242b..7b1dc05 100644
--- a/libvlc/jni/libvlcjni-track.c
+++ b/libvlc/jni/libvlcjni-track.c
@@ -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;
}
diff --git a/libvlc/jni/libvlcjni.c b/libvlc/jni/libvlcjni.c
index 94b216c..1242e3b 100644
--- a/libvlc/jni/libvlcjni.c
+++ b/libvlc/jni/libvlcjni.c
@@ -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)
diff --git a/libvlc/src/org/videolan/libvlc/Media.java b/libvlc/src/org/videolan/libvlc/Media.java
index 4e333be..8dd0b0f 100644
--- a/libvlc/src/org/videolan/libvlc/Media.java
+++ b/libvlc/src/org/videolan/libvlc/Media.java
@@ -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()));
}
More information about the Android
mailing list