From afaeb6abf4ce0d6e1ee9d546e4c8fefabae3c412 Mon Sep 17 00:00:00 2001
From: Daniel Kang <daniel.d.kang@gmail.com>
Date: Mon, 10 Jan 2011 01:13:40 -0500
Subject: [PATCH] Add check to see if tracks are initialized.

---
 libavformat/4xm.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/libavformat/4xm.c b/libavformat/4xm.c
index a697f8d..1f6a6d6 100644
--- a/libavformat/4xm.c
+++ b/libavformat/4xm.c
@@ -66,6 +66,7 @@ typedef struct AudioTrack {
     int channels;
     int stream_index;
     int adpcm;
+    int init;
     int64_t audio_pts;
 } AudioTrack;

@@ -185,8 +186,15 @@ static int fourxm_read_header(AVFormatContext *s,
             fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
             fourxm->tracks[current_track].bits        = AV_RL32(&header[i + 44]);
             fourxm->tracks[current_track].audio_pts   = 0;
+            fourxm->tracks[current_track].init        = 1;
             i += 8 + size;

+            if (fourxm->tracks[current_track].channels <= 0) {
+                av_log(s, AV_LOG_ERROR, "Unsupported number of channels: %d\n", fourxm->tracks[current_track].channels);
+                ret = AVERROR(EINVAL);
+                goto fail;
+            }
+
             /* allocate a new AVStream */
             st = av_new_stream(s, current_track);
             if (!st){
@@ -295,6 +303,10 @@ static int fourxm_read_packet(AVFormatContext *s,
                 ret= av_get_packet(s->pb, pkt, size);
                 if(ret<0)
                     return AVERROR(EIO);
+                if(fourxm->tracks[track_number].init == 0) {
+                    av_log(s, AV_LOG_ERROR, "Track not initialized.\n");
+                    return AVERROR(EINVAL);
+                }
                 pkt->stream_index =
                     fourxm->tracks[track_number].stream_index;
                 pkt->pts = fourxm->tracks[track_number].audio_pts;
--
1.7.2.2
