$OpenBSD: patch-avilib_avilib_c,v 1.1.1.1 2005/01/25 21:58:59 naddy Exp $
--- avilib/avilib.c.orig	Tue Nov  2 19:45:45 2004
+++ avilib/avilib.c	Thu Jan  6 12:19:11 2005
@@ -289,6 +289,7 @@ static int avi_init_super_index(avi_t *A
 	AVI_errno = AVI_ERR_NO_MEM;
 	return -1;
     }
+    memset(sil, 0, sizeof (avisuperindex_chunk));
     memcpy (sil->fcc, "indx", 4);
     sil->dwSize = 0; // size of this chunk
     sil->wLongsPerEntry = 4;
@@ -313,6 +314,7 @@ static int avi_init_super_index(avi_t *A
     }
     for (k = 0; k < NR_IXNN_CHUNKS; k++) {
 	sil->stdindex[k] = malloc (sizeof (avistdindex_chunk));
+	memset(sil->stdindex[k], 0, sizeof (avistdindex_chunk));
 	// gets rewritten later
 	sil->stdindex[k]->qwBaseOffset = (uint64_t)k * NEW_RIFF_THRES;
     }
@@ -1888,7 +1890,7 @@ int  AVI_get_comment_fd(avi_t *AVI)
 int AVI_close(avi_t *AVI)
 {
    int ret;
-   int j;
+   int j,k;
 
    /* If the file was open for writing, the header and index still have
       to be written */
@@ -1907,6 +1909,15 @@ int AVI_close(avi_t *AVI)
    if(AVI->idx) free(AVI->idx);
    if(AVI->video_index) free(AVI->video_index);
    if(AVI->video_superindex) {
+       for (j = 0; j < NR_IXNN_CHUNKS; j++) {
+	   if (AVI->video_superindex->stdindex[j]) {
+	       if (AVI->video_superindex->stdindex[j]->aIndex) {
+		   free(AVI->video_superindex->stdindex[j]->aIndex);
+	       }
+	       free(AVI->video_superindex->stdindex[j]);
+	   }
+       }
+       if(AVI->video_superindex->stdindex) free(AVI->video_superindex->stdindex);
        if(AVI->video_superindex->aIndex) free(AVI->video_superindex->aIndex);
        free(AVI->video_superindex);
    }
@@ -1915,8 +1926,19 @@ int AVI_close(avi_t *AVI)
    {
        if(AVI->track[j].audio_index) free(AVI->track[j].audio_index);
        if(AVI->track[j].audio_superindex) {
-	   if(AVI->track[j].audio_superindex->aIndex) free(AVI->track[j].audio_superindex->aIndex);
-	   free(AVI->track[j].audio_superindex);
+	   // shortcut
+	   avisuperindex_chunk *a = AVI->track[j].audio_superindex;
+	   for (k = 0; k < NR_IXNN_CHUNKS; k++) {
+	       if (a->stdindex[k]) {
+		   if (a->stdindex[k]->aIndex) {
+		       free(a->stdindex[k]->aIndex);
+		   }
+		   free(a->stdindex[k]);
+	       }
+	   }
+	   if(a->stdindex) free(a->stdindex);
+	   if(a->aIndex) free(a->aIndex);
+	   free(a);
        }
    }
 
