[xiph-commits] r3816 - in liboggz/trunk: include/oggz src/liboggz

conrad at svn.annodex.net conrad at svn.annodex.net
Sun Dec 7 04:35:04 PST 2008


Author: conrad
Date: 2008-12-07 04:35:03 -0800 (Sun, 07 Dec 2008)
New Revision: 3816

Modified:
   liboggz/trunk/include/oggz/oggz_constants.h
   liboggz/trunk/include/oggz/oggz_table.h
   liboggz/trunk/include/oggz/oggz_write.h
   liboggz/trunk/src/liboggz/oggz.c
   liboggz/trunk/src/liboggz/oggz_auto.c
   liboggz/trunk/src/liboggz/oggz_comments.c
   liboggz/trunk/src/liboggz/oggz_dlist.c
   liboggz/trunk/src/liboggz/oggz_dlist.h
   liboggz/trunk/src/liboggz/oggz_read.c
   liboggz/trunk/src/liboggz/oggz_table.c
   liboggz/trunk/src/liboggz/oggz_vector.c
   liboggz/trunk/src/liboggz/oggz_write.c
Log:
liboggz: handle allocation failure due to out of memory
throughout, for Mozilla bug 468280
Adds new error return OGGZ_ERR_OUT_OF_MEMORY


Modified: liboggz/trunk/include/oggz/oggz_constants.h
===================================================================
--- liboggz/trunk/include/oggz/oggz_constants.h	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/include/oggz/oggz_constants.h	2008-12-07 12:35:03 UTC (rev 3816)
@@ -173,6 +173,9 @@
   /** Hole (sequence number gap) detected in input data */
   OGGZ_ERR_HOLE_IN_DATA                 = -17,
 
+  /** Out of memory */
+  OGGZ_ERR_OUT_OF_MEMORY                = -18,
+
   /** The requested serialno does not exist in this OGGZ */
   OGGZ_ERR_BAD_SERIALNO                 = -20,
 

Modified: liboggz/trunk/include/oggz/oggz_table.h
===================================================================
--- liboggz/trunk/include/oggz/oggz_table.h	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/include/oggz/oggz_table.h	2008-12-07 12:35:03 UTC (rev 3816)
@@ -48,6 +48,7 @@
 /**
  * Instantiate a new OggzTable
  * \returns A new OggzTable
+ * \retval NULL Could not allocate memory for table
  */
 OggzTable *
 oggz_table_new (void);

Modified: liboggz/trunk/include/oggz/oggz_write.h
===================================================================
--- liboggz/trunk/include/oggz/oggz_write.h	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/include/oggz/oggz_write.h	2008-12-07 12:35:03 UTC (rev 3816)
@@ -177,6 +177,7 @@
  *         32 bits, ie. within the range (-(2^31), (2^31)-1)
  * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
  * \retval OGGZ_ERR_INVALID Operation not suitable for this OGGZ
+ * \retval OGGZ_ERR_OUT_OF_MEMORY Unable to allocate memory to queue packet
  *
  * \note If \a op->b_o_s is initialized to \a -1 before calling
  *       oggz_write_feed(), Oggz will fill it in with the appropriate

Modified: liboggz/trunk/src/liboggz/oggz.c
===================================================================
--- liboggz/trunk/src/liboggz/oggz.c	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/src/liboggz/oggz.c	2008-12-07 12:35:03 UTC (rev 3816)
@@ -92,6 +92,10 @@
   oggz->cb_next = 0;
 
   oggz->streams = oggz_vector_new ();
+  if (oggz->streams == NULL) {
+    goto err_oggz_new;
+  }
+  
   oggz->all_at_eos = 0;
 
   oggz->metric = NULL;
@@ -102,14 +106,26 @@
   oggz->order_user_data = NULL;
 
   oggz->packet_buffer = oggz_dlist_new ();
+  if (oggz->packet_buffer == NULL) {
+    goto err_streams_new;
+  }
 
   if (OGGZ_CONFIG_WRITE && (oggz->flags & OGGZ_WRITE)) {
-    oggz_write_init (oggz);
+    if (oggz_write_init (oggz) == NULL)
+      goto err_packet_buffer_new;
   } else if (OGGZ_CONFIG_READ) {
     oggz_read_init (oggz);
   }
 
   return oggz;
+
+err_packet_buffer_new:
+  oggz_free (oggz->packet_buffer);
+err_streams_new:
+  oggz_free (oggz->streams);
+err_oggz_new:
+  oggz_free (oggz);
+  return NULL;
 }
 
 OGGZ *

Modified: liboggz/trunk/src/liboggz/oggz_auto.c
===================================================================
--- liboggz/trunk/src/liboggz/oggz_auto.c	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/src/liboggz/oggz_auto.c	2008-12-07 12:35:03 UTC (rev 3816)
@@ -458,6 +458,7 @@
 
   if (stream->calculate_data == NULL) {
     stream->calculate_data = malloc(sizeof(auto_calc_speex_info_t));
+    if (stream->calculate_data == NULL) return -1;
     info = stream->calculate_data;
     info->encountered_first_data_packet = 0;
     info->packet_size =
@@ -511,6 +512,8 @@
 
   if (stream->calculate_data == NULL) {
     stream->calculate_data = malloc(sizeof(auto_calc_celt_info_t));
+    if (stream->calculate_data == NULL) return -1;
+
     info = stream->calculate_data;
     info->encountered_first_data_packet = 0;
 
@@ -576,6 +579,7 @@
   {
     if (info == NULL) {
       stream->calculate_data = malloc(sizeof(auto_calc_theora_info_t));
+      if (stream->calculate_data == NULL) return -1;
       info = stream->calculate_data;
     }
     info->encountered_first_data_packet = 0;
@@ -699,6 +703,8 @@
     short_size = 1 << (op->packet[28] & 0xF);
 
     stream->calculate_data = malloc(sizeof(auto_calc_vorbis_info_t));
+    if (stream->calculate_data == NULL) return -1;
+
     info = (auto_calc_vorbis_info_t *)stream->calculate_data;
     info->nln_increments[3] = long_size >> 1;
     info->nln_increments[2] = 3 * (long_size >> 2) - (short_size >> 2);
@@ -841,10 +847,12 @@
       /*
        * store mode size information in our info struct
        */
-      stream->calculate_data = realloc(stream->calculate_data,
+      info = realloc(stream->calculate_data,
               sizeof(auto_calc_vorbis_info_t) + (size - 1) * sizeof(int));
-      info = (auto_calc_vorbis_info_t *)(stream->calculate_data);
+      if (info == NULL) return -1;
 
+      stream->calculate_data = info;
+
       i = -1;
       while ((1 << (++i)) < size);
       info->log2_num_modes = i;
@@ -969,6 +977,8 @@
 
   if (stream->calculate_data == NULL) {
     stream->calculate_data = malloc(sizeof(auto_calc_flac_info_t));
+    if (stream->calculate_data == NULL) return -1;
+
     info = (auto_calc_flac_info_t *)stream->calculate_data;
     info->previous_gp = 0;
     info->encountered_first_data_packet = 0;

Modified: liboggz/trunk/src/liboggz/oggz_comments.c
===================================================================
--- liboggz/trunk/src/liboggz/oggz_comments.c	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/src/liboggz/oggz_comments.c	2008-12-07 12:35:03 UTC (rev 3816)
@@ -57,6 +57,8 @@
   char * ret;
   if (s == NULL) return NULL;
   ret = oggz_malloc (strlen(s) + 1);
+  if (ret == NULL) return NULL;
+
   return strcpy (ret, s);
 }
 
@@ -89,11 +91,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
@@ -130,47 +127,6 @@
                                      buf[base+1]=(char)(((val)>>8)&0xff); \
                                      buf[base+2]=(char)((val)&0xff);
 
-#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*)oggz_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*)oggz_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
 oggz_comment_validate_byname (const char * name, const char * value)
 {
@@ -200,6 +156,8 @@
   if (!oggz_comment_validate_byname (name, value)) return NULL;
 
   comment = oggz_malloc (sizeof (OggzComment));
+  if (comment == NULL) return NULL;
+
   comment->name = oggz_strdup (name);
   comment->value = oggz_strdup (value);
 

Modified: liboggz/trunk/src/liboggz/oggz_dlist.c
===================================================================
--- liboggz/trunk/src/liboggz/oggz_dlist.c	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/src/liboggz/oggz_dlist.c	2008-12-07 12:35:03 UTC (rev 3816)
@@ -49,11 +49,25 @@
 OggzDList *
 oggz_dlist_new (void) {
 
-  OggzDList *dlist = malloc(sizeof(OggzDList));
+  OggzDList *dlist;
+  OggzDListElem *dummy_front, *dummy_back;
+  
+  dlist = malloc(sizeof(OggzDList));
+  if (dlist == NULL) return NULL;
 
-  OggzDListElem * dummy_front = malloc(sizeof(OggzDListElem));
-  OggzDListElem * dummy_back = malloc(sizeof(OggzDListElem));
-  
+  dummy_front = malloc(sizeof(OggzDListElem));
+  if (dummy_front == NULL) {
+    free (dlist);
+    return NULL;
+  }
+
+  dummy_back = malloc(sizeof(OggzDListElem));
+  if (dummy_back == NULL) {
+    free (dummy_front);
+    free (dlist);
+    return NULL;
+  }
+
   dummy_front->next = dummy_back;
   dummy_front->prev = NULL;
 
@@ -64,7 +78,6 @@
   dlist->tail = dummy_back;
 
   return dlist;
-
 }
 
 void
@@ -86,29 +99,42 @@
   return (dlist->head->next == dlist->tail);
 }
 
-void
+int
 oggz_dlist_append(OggzDList *dlist, void *elem) {
 
-  OggzDListElem *new_elem = malloc(sizeof(OggzDListElem));
+  OggzDListElem *new_elem;
 
+  if (dlist == NULL) return -1;
+
+  new_elem = malloc(sizeof(OggzDListElem));
+  if (new_elem == NULL) return -1;
+
   new_elem->data = elem;
   new_elem->next = dlist->tail;
   new_elem->prev = dlist->tail->prev;
   new_elem->prev->next = new_elem;
   new_elem->next->prev = new_elem;
+
+  return 0;
 }
 
-void
+int
 oggz_dlist_prepend(OggzDList *dlist, void *elem) {
 
-  OggzDListElem *new_elem = malloc(sizeof(OggzDListElem));
+  OggzDListElem *new_elem;
 
+  if (dlist == NULL) return -1;
+
+  new_elem = malloc(sizeof(OggzDListElem));
+  if (new_elem == NULL) return -1;
+
   new_elem->data = elem;
   new_elem->prev = dlist->head;
   new_elem->next = dlist->head->next;
   new_elem->prev->next = new_elem;
   new_elem->next->prev = new_elem;
 
+  return 0;
 }
 
 void

Modified: liboggz/trunk/src/liboggz/oggz_dlist.h
===================================================================
--- liboggz/trunk/src/liboggz/oggz_dlist.h	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/src/liboggz/oggz_dlist.h	2008-12-07 12:35:03 UTC (rev 3816)
@@ -49,10 +49,10 @@
 int
 oggz_dlist_is_empty(OggzDList *dlist);
 
-void
+int
 oggz_dlist_append(OggzDList *dlist, void *elem);
 
-void
+int
 oggz_dlist_prepend(OggzDList *dlist, void *elem);
 
 void

Modified: liboggz/trunk/src/liboggz/oggz_read.c
===================================================================
--- liboggz/trunk/src/liboggz/oggz_read.c	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/src/liboggz/oggz_read.c	2008-12-07 12:35:03 UTC (rev 3816)
@@ -227,6 +227,8 @@
             OggzReader *reader) {
 
   OggzBufferedPacket *p = malloc(sizeof(OggzBufferedPacket));
+  if (p == NULL) return NULL;
+
   memcpy(&(p->packet), packet, sizeof(ogg_packet));
   p->packet.packet = malloc(packet->bytes);
   memcpy(p->packet.packet, packet->packet, packet->bytes);

Modified: liboggz/trunk/src/liboggz/oggz_table.c
===================================================================
--- liboggz/trunk/src/liboggz/oggz_table.c	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/src/liboggz/oggz_table.c	2008-12-07 12:35:03 UTC (rev 3816)
@@ -49,6 +49,8 @@
   OggzTable * table;
 
   table = oggz_malloc (sizeof (OggzTable));
+  if (table == NULL) return NULL;
+
   table->keys = oggz_vector_new ();
   table->data = oggz_vector_new ();
 
@@ -70,6 +72,8 @@
 {
   int i, size;
 
+  if (table == NULL) return NULL;
+
   size = oggz_vector_size (table->keys);
   for (i = 0; i < size; i++) {
     if (oggz_vector_nth_l (table->keys, i) == key) {

Modified: liboggz/trunk/src/liboggz/oggz_vector.c
===================================================================
--- liboggz/trunk/src/liboggz/oggz_vector.c	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/src/liboggz/oggz_vector.c	2008-12-07 12:35:03 UTC (rev 3816)
@@ -78,6 +78,7 @@
   OggzVector * vector;
 
   vector = oggz_malloc (sizeof (OggzVector));
+  if (vector == NULL) return NULL;
 
   vector->max_elements = 0;
   vector->nr_elements = 0;
@@ -266,7 +267,6 @@
 
     if (new_elements == NULL) {
       vector->nr_elements--;
-      vector->data = NULL;
       return NULL;
     }
 
@@ -359,8 +359,7 @@
         oggz_realloc (vector->data,
         (size_t)new_max_elements * sizeof (oggz_data_t));
 
-      if (new_elements == NULL)
-      {
+      if (new_elements == NULL) {
         vector->data = NULL;
         return NULL;
       }
@@ -405,52 +404,13 @@
 oggz_vector_pop (OggzVector * vector)
 {
   void * data;
-#if 0
-  void * new_elements;
-  int new_max_elements;
-#endif
 
-  if (!vector || vector->data == NULL) return NULL;
+  if (vector == NULL || vector->data == NULL) return NULL;
 
   data = vector->data[0].p;
 
-#if 0
-  vector->nr_elements--;
-
-  if (vector->nr_elements == 0) {
-    oggz_vector_clear (vector);
-  } else {
-#if 0
-    memmove (vector->data, &vector->data[1],
-	     vector->nr_elements * sizeof (void *));
-#else
-    {
-      int i;
-      for (i = 0; i < vector->nr_elements; i++) {
-	vector->data[i].p = vector->data[i+1].p;
-      }
-    }
-#endif
-    if (vector->nr_elements < vector->max_elements/2) {
-      new_max_elements = vector->max_elements/2;
-
-      new_elements =
-        oggz_realloc (vector->data,
-        (size_t)new_max_elements * sizeof (oggz_data_t));
-
-      if (new_elements != NULL) {
-        vector->max_elements = new_max_elements;
-        vector->data = new_elements;
-      }
-    }
-
-  }
-#else
-
   oggz_vector_remove_nth (vector, 0);
 
-#endif
-
   return data;
 
 }

Modified: liboggz/trunk/src/liboggz/oggz_write.c
===================================================================
--- liboggz/trunk/src/liboggz/oggz_write.c	2008-12-06 03:05:15 UTC (rev 3815)
+++ liboggz/trunk/src/liboggz/oggz_write.c	2008-12-07 12:35:03 UTC (rev 3816)
@@ -91,6 +91,7 @@
   writer->next_zpacket = NULL;
 
   writer->packet_queue = oggz_vector_new ();
+  if (writer->packet_queue == NULL) return NULL;
 
 #ifdef ZPACKET_CMP
   /* XXX: comparison function should only kick in when a metric is set */
@@ -205,7 +206,7 @@
   oggz_stream_t * stream;
   oggz_writer_packet_t * packet;
   ogg_packet * new_op;
-  unsigned char * new_buf;
+  unsigned char * new_buf = NULL;
   int b_o_s, e_o_s, bos_auto;
   int strict, prefix, suffix;
 
@@ -306,12 +307,18 @@
   /* Now set up the packet and add it to the queue */
   if (guard == NULL) {
     new_buf = oggz_malloc ((size_t)op->bytes);
+    if (new_buf == NULL) return OGGZ_ERR_OUT_OF_MEMORY;
+
     memcpy (new_buf, op->packet, (size_t)op->bytes);
   } else {
     new_buf = op->packet;
   }
 
   packet = oggz_malloc (sizeof (oggz_writer_packet_t));
+  if (packet == NULL) {
+    if (guard == NULL && new_buf != NULL) free (new_buf);
+    return OGGZ_ERR_OUT_OF_MEMORY;
+  }
 
   new_op = &packet->op;
   new_op->packet = new_buf;



More information about the commits mailing list