diff --git a/libavformat/4xm.c b/libavformat/4xm.c
index c1d5f67e8ceb000259d072b4f57b4a4447dd8896..b45fd05b1d2b0acf7427c27b0e61d33ad9d2c82e 100644
--- a/libavformat/4xm.c
+++ b/libavformat/4xm.c
@@ -151,8 +151,9 @@ static int fourxm_read_header(AVFormatContext *s,
             }
             current_track = LE_32(&header[i + 8]);
             if (current_track + 1 > fourxm->track_count) {
-                fourxm->track_count++;
-                fourxm->tracks = av_realloc(fourxm->tracks, fourxm->track_count);
+                fourxm->track_count = current_track + 1;
+                fourxm->tracks = av_realloc(fourxm->tracks, 
+                    fourxm->track_count * sizeof(AudioTrack));
                 if (!fourxm->tracks) {
                     av_free(header);
                     return AVERROR_NOMEM;