[xiph-commits] r3849 - in libfishsound/trunk: include/fishsound src/libfishsound

conrad at svn.annodex.net conrad at svn.annodex.net
Wed Feb 18 21:46:09 PST 2009


Author: conrad
Date: 2009-02-18 21:46:08 -0800 (Wed, 18 Feb 2009)
New Revision: 3849

Modified:
   libfishsound/trunk/include/fishsound/fishsound.h
   libfishsound/trunk/src/libfishsound/comments.c
   libfishsound/trunk/src/libfishsound/fishsound.c
   libfishsound/trunk/src/libfishsound/flac.c
   libfishsound/trunk/src/libfishsound/fs_vector.c
   libfishsound/trunk/src/libfishsound/speex.c
   libfishsound/trunk/src/libfishsound/vorbis.c
Log:
libfishsound: handle allocation failure due to out of memory
towards Mozilla bug 468293

Modified: libfishsound/trunk/include/fishsound/fishsound.h
===================================================================
--- libfishsound/trunk/include/fishsound/fishsound.h	2009-02-10 23:37:07 UTC (rev 3848)
+++ libfishsound/trunk/include/fishsound/fishsound.h	2009-02-19 05:46:08 UTC (rev 3849)
@@ -505,7 +505,7 @@
  * \param fsound A FishSound* handle
  * \retval 0 \a fsound uses non-interleaved PCM
  * \retval 1 \a fsound uses interleaved PCM
- * \retval -1 Invalid \a fsound
+ * \retval -1 Invalid \a fsound, or out of memory.
  */
 int fish_sound_get_interleave (FishSound * fsound);
 

Modified: libfishsound/trunk/src/libfishsound/comments.c
===================================================================
--- libfishsound/trunk/src/libfishsound/comments.c	2009-02-10 23:37:07 UTC (rev 3848)
+++ libfishsound/trunk/src/libfishsound/comments.c	2009-02-19 05:46:08 UTC (rev 3849)
@@ -46,6 +46,7 @@
   char * ret;
   if (s == NULL) return NULL;
   ret = fs_malloc (strlen(s) + 1);
+  if (ret == NULL) return NULL;
   return strcpy (ret, s);
 }
 
@@ -56,6 +57,7 @@
   if (s == NULL) return NULL;
   if (len == 0) return NULL;
   ret = fs_malloc (len + 1);
+  if (ret == NULL) return NULL;
   if (strncpy (ret, s, len) == NULL) {
     fs_free (ret);
     return NULL;
@@ -79,11 +81,6 @@
   return NULL;
 }
 
-#if 0
-static void comment_init(char **comments, int* length, char *vendor_string);
-static void comment_add(char **comments, int* length, char *tag, char *val);
-#endif
-
 /*                 
  Comments will be stored in the Vorbis style.            
  It is describled in the "Structure" section of
@@ -114,47 +111,6 @@
                                      buf[base]=(val)&0xff; \
                                  }while(0)
 
-#if 0
-static void
-comment_init(char **comments, int* length, char *vendor_string)
-{
-  int vendor_length=strlen(vendor_string);
-  int user_comment_list_length=0;
-  int len=4+vendor_length+4;
-  char *p=(char*)fs_malloc(len);
-  if(p==NULL){
-  }
-  writeint(p, 0, vendor_length);
-  memcpy(p+4, vendor_string, vendor_length);
-  writeint(p, 4+vendor_length, user_comment_list_length);
-  *length=len;
-  *comments=p;
-}
-
-static void
-comment_add(char **comments, int* length, char *tag, char *val)
-{
-  char* p=*comments;
-  int vendor_length=readint(p, 0);
-  int user_comment_list_length=readint(p, 4+vendor_length);
-  int tag_len=(tag?strlen(tag):0);
-  int val_len=strlen(val);
-  int len=(*length)+4+tag_len+val_len;
-
-  p=(char*)fs_realloc(p, len);
-  if(p==NULL){
-  }
-
-  writeint(p, *length, tag_len+val_len);      /* length of comment */
-  if(tag) memcpy(p+*length+4, tag, tag_len);  /* comment */
-  memcpy(p+*length+4+tag_len, val, val_len);  /* comment */
-  writeint(p, 4+vendor_length, user_comment_list_length+1);
-
-  *comments=p;
-  *length=len;
-}
-#endif
-
 static int
 fs_comment_validate_byname (const char * name, const char * value)
 {
@@ -184,6 +140,8 @@
   if (!fs_comment_validate_byname (name, value)) return NULL;
 
   comment = fs_malloc (sizeof (FishSoundComment));
+  if (comment == NULL) return NULL;
+
   comment->name = fs_strdup (name);
   comment->value = fs_strdup (value);
 

Modified: libfishsound/trunk/src/libfishsound/fishsound.c
===================================================================
--- libfishsound/trunk/src/libfishsound/fishsound.c	2009-02-10 23:37:07 UTC (rev 3848)
+++ libfishsound/trunk/src/libfishsound/fishsound.c	2009-02-19 05:46:08 UTC (rev 3849)
@@ -70,7 +70,7 @@
   }
 
   if (fsound->codec && fsound->codec->init)
-    fsound->codec->init (fsound);
+    if (fsound->codec->init (fsound) == NULL) return -1;
 
   fsound->info.format = format;
 
@@ -105,6 +105,7 @@
   }
 
   fsound = fs_malloc (sizeof (FishSound));
+  if (fsound == NULL) return NULL;
 
   fsound->mode = mode;
   fsound->interleave = 0;

Modified: libfishsound/trunk/src/libfishsound/flac.c
===================================================================
--- libfishsound/trunk/src/libfishsound/flac.c	2009-02-10 23:37:07 UTC (rev 3848)
+++ libfishsound/trunk/src/libfishsound/flac.c	2009-02-19 05:46:08 UTC (rev 3849)
@@ -369,7 +369,7 @@
         printf("fs_flac_enc_write_callback: generating FLAC header packet: "
                "%c%c%c%c\n", buffer[0], buffer[1], buffer[2], buffer[3]);
 #endif
-	fi->buffer = (unsigned char*)malloc(sizeof(unsigned char)*(bytes+9));
+	fi->buffer = (unsigned char*)fs_malloc(sizeof(unsigned char)*(bytes+9));
 	fi->buffer[0] = 0x7f;
 	fi->buffer[1] = 0x46; /* 'F' */
 	fi->buffer[2] = 0x4c; /* 'L' */
@@ -387,7 +387,7 @@
         /* Make a temporary copy of the metadata header to pass to the user
          * callback.
          */
-	unsigned char* tmp = (unsigned char*)malloc(sizeof(unsigned char)*(bytes+fi->bufferlength));
+	unsigned char* tmp = (unsigned char*)fs_malloc(sizeof(unsigned char)*(bytes+fi->bufferlength));
 	memcpy (tmp, fi->buffer, fi->bufferlength);
 	memcpy (tmp+fi->bufferlength, buffer, bytes);
 	fs_free(fi->buffer);
@@ -771,6 +771,7 @@
   FishSoundCodec * codec;
 
   codec = (FishSoundCodec *) fs_malloc (sizeof (FishSoundCodec));
+  if (codec == NULL) return NULL;
 
   codec->format.format = FISH_SOUND_FLAC;
   codec->format.name = "Flac (Xiph.Org)";

Modified: libfishsound/trunk/src/libfishsound/fs_vector.c
===================================================================
--- libfishsound/trunk/src/libfishsound/fs_vector.c	2009-02-10 23:37:07 UTC (rev 3848)
+++ libfishsound/trunk/src/libfishsound/fs_vector.c	2009-02-19 05:46:08 UTC (rev 3849)
@@ -58,6 +58,7 @@
   FishSoundVector * vector;
 
   vector = fs_malloc (sizeof (FishSoundVector));
+  if (vector == NULL) return NULL;
 
   vector->max_elements = 0;
   vector->nr_elements = 0;
@@ -176,6 +177,8 @@
 void *
 fs_vector_insert (FishSoundVector * vector, void * data)
 {
+  if (vector == NULL) return NULL;
+
   if (fs_vector_grow (vector) == NULL)
     return NULL;
 

Modified: libfishsound/trunk/src/libfishsound/speex.c
===================================================================
--- libfishsound/trunk/src/libfishsound/speex.c	2009-02-10 23:37:07 UTC (rev 3848)
+++ libfishsound/trunk/src/libfishsound/speex.c	2009-02-19 05:46:08 UTC (rev 3849)
@@ -371,8 +371,8 @@
   int modeID;
   SpeexMode * mode = NULL;
   SpeexHeader header;
-  unsigned char * buf;
-  int bytes;
+  unsigned char * header_buf = NULL, * comments_buf = NULL;
+  int header_bytes, comments_bytes;
   size_t buflen;
 
   modeID = 1;
@@ -392,24 +392,23 @@
   fss->st = speex_encoder_init (mode);
 
   if (fsound->callback.encoded) {
-    FishSoundEncoded encoded = (FishSoundEncoded)fsound->callback.encoded;
     char vendor_string[128];
 
-    /* header */
-    buf = (unsigned char *) speex_header_to_packet (&header, &bytes);
-    encoded (fsound, buf, (long)bytes, fsound->user_data);
-    fss->packetno++;
-    fs_free (buf);
+    /* Allocate and create header */
+    header_buf = (unsigned char *) speex_header_to_packet (&header, &header_bytes);
+    if (header_buf == NULL) {
+      return NULL;
+    }
 
-    /* comments */
+    /* Allocate and create comments */
     snprintf (vendor_string, 128, VENDOR_FORMAT, header.speex_version);
     fish_sound_comment_set_vendor (fsound, vendor_string);
-    bytes = fish_sound_comments_encode (fsound, NULL, 0);
-    buf = fs_malloc (bytes);
-    bytes = fish_sound_comments_encode (fsound, buf, bytes);
-    encoded (fsound, buf, (long)bytes, fsound->user_data);
-    fss->packetno++;
-    fs_free (buf);
+    comments_bytes = fish_sound_comments_encode (fsound, NULL, 0);
+    comments_buf = fs_malloc (comments_bytes);
+    if (comments_buf == NULL) {
+      fs_free (header_buf);
+      return NULL;
+    }
   }
 
   speex_encoder_ctl (fss->st, SPEEX_SET_SAMPLING_RATE,
@@ -421,12 +420,33 @@
   printf ("got frame size %d\n", fss->frame_size);
 #endif
 
-  /* XXX: blah blah blah ... set VBR etc. */
+  /* XXX: set VBR etc. */
 
   buflen = fss->frame_size * fsound->info.channels * sizeof (float);
   fss->ipcm = fs_malloc (buflen);
+  if (fss->ipcm == NULL) {
+    if (comments_buf) fs_free (comments_buf);
+    if (header_buf) fs_free (header_buf);
+    return NULL;
+  }
   memset (fss->ipcm, 0, buflen);
 
+  /* Allocations succeeded, actually call encoded callback for headers */
+  if (fsound->callback.encoded) {
+    FishSoundEncoded encoded = (FishSoundEncoded)fsound->callback.encoded;
+
+    /* header */
+    encoded (fsound, header_buf, (long)header_bytes, fsound->user_data);
+    fss->packetno++;
+    fs_free (header_buf);
+
+    /* comments */
+    comments_bytes = fish_sound_comments_encode (fsound, comments_buf, comments_bytes);
+    encoded (fsound, comments_buf, (long)comments_bytes, fsound->user_data);
+    fss->packetno++;
+    fs_free (comments_buf);
+  }
+
   return fsound;
 }
 
@@ -604,11 +624,14 @@
 {
   FishSoundSpeexInfo * fss = (FishSoundSpeexInfo *)fsound->codec_data;
   size_t pcm_size = sizeof (float);
+  float *ipcm_new, *pcm0, *pcm1;
 
-  fss->ipcm = (float *)
-    fs_realloc (fss->ipcm,
-		pcm_size * fss->frame_size * fsound->info.channels);
+  ipcm_new = (float *)fs_realloc (fss->ipcm,
+		  pcm_size * fss->frame_size * fsound->info.channels);
+  if (ipcm_new == NULL) return -1;
 
+  fss->ipcm = ipcm_new;
+
   if (interleave) {
     /* if transitioning from non-interleave to interleave,
        free non-ilv buffers */
@@ -622,8 +645,19 @@
     if (fsound->info.channels == 1) {
       fss->pcm[0] = (float *) fss->ipcm;
     } else if (fsound->info.channels == 2) {
-      fss->pcm[0] = fs_realloc (fss->pcm[0], pcm_size * fss->frame_size);
-      fss->pcm[1] = fs_realloc (fss->pcm[1], pcm_size * fss->frame_size);
+      pcm0 = fs_realloc (fss->pcm[0], pcm_size * fss->frame_size);
+      if (pcm0 == NULL) {
+        return -1;
+      }
+
+      pcm1 = fs_realloc (fss->pcm[1], pcm_size * fss->frame_size);
+      if (pcm1 == NULL) {
+        fs_free (pcm0);
+        return -1;
+      }
+
+      fss->pcm[0] = pcm0;
+      fss->pcm[1] = pcm1;
     }
   }
 
@@ -705,6 +739,7 @@
   FishSoundCodec * codec;
 
   codec = (FishSoundCodec *) fs_malloc (sizeof (FishSoundCodec));
+  if (codec == NULL) return NULL;
 
   codec->format.format = FISH_SOUND_SPEEX;
   codec->format.name = "Speex (Xiph.Org)";

Modified: libfishsound/trunk/src/libfishsound/vorbis.c
===================================================================
--- libfishsound/trunk/src/libfishsound/vorbis.c	2009-02-10 23:37:07 UTC (rev 3848)
+++ libfishsound/trunk/src/libfishsound/vorbis.c	2009-02-19 05:46:08 UTC (rev 3849)
@@ -113,6 +113,7 @@
   FishSoundVorbisInfo * fsv = (FishSoundVorbisInfo *)fsound->codec_data;
   ogg_packet op;
   long samples;
+  float ** pcm_new;
   int ret;
 
   /* Make an ogg_packet structure to pass the data to libvorbis */
@@ -160,9 +161,15 @@
 
       if (fsound->interleave) {
 	if (samples > fsv->max_pcm) {
-	  fsv->ipcm = realloc (fsv->ipcm, sizeof(float) * samples *
-			       fsound->info.channels);
-	  fsv->max_pcm = samples;
+          pcm_new = realloc (fsv->ipcm, sizeof(float) * samples *
+			     fsound->info.channels);
+          if (pcm_new == NULL) {
+            /* Allocation failure; just truncate here, fail gracefully elsewhere */
+            samples = fsv->max_pcm;
+          } else {
+	    fsv->ipcm = pcm_new;
+	    fsv->max_pcm = samples;
+          }
 	}
 	_fs_interleave (fsv->pcm, (float **)fsv->ipcm, samples,
 			fsound->info.channels, 1.0);
@@ -473,6 +480,7 @@
   FishSoundCodec * codec;
 
   codec = (FishSoundCodec *) fs_malloc (sizeof (FishSoundCodec));
+  if (codec == NULL) return NULL;
 
   codec->format.format = FISH_SOUND_VORBIS;
   codec->format.name = "Vorbis (Xiph.Org)";



More information about the commits mailing list