[xiph-commits] r3859 - liboggz/trunk/src/tools

conrad at svn.annodex.net conrad at svn.annodex.net
Sun Feb 22 19:58:27 PST 2009


Author: conrad
Date: 2009-02-22 19:58:27 -0800 (Sun, 22 Feb 2009)
New Revision: 3859

Modified:
   liboggz/trunk/src/tools/oggz-merge.c
   liboggz/trunk/src/tools/oggz-sort.c
Log:
oggz-merge, oggz-sort: handle out-of-memory, and use checked_fwrite()

Modified: liboggz/trunk/src/tools/oggz-merge.c
===================================================================
--- liboggz/trunk/src/tools/oggz-merge.c	2009-02-23 03:56:37 UTC (rev 3858)
+++ liboggz/trunk/src/tools/oggz-merge.c	2009-02-23 03:58:27 UTC (rev 3859)
@@ -56,6 +56,8 @@
   "  contain %d tracks in parallel, interleaved for simultaneous playback.\n"\
   "  If you want to sequence these files one after another, use cat instead.\n"
 
+static char * progname;
+
 static void
 usage (char * progname)
 {
@@ -71,6 +73,23 @@
   printf ("Please report bugs to <ogg-dev at xiph.org>\n");
 }
 
+static void
+exit_out_of_memory (void)
+{
+  fprintf (stderr, "%s: Out of memory\n", progname);
+  exit (1);
+}
+
+static void
+checked_fwrite (const void *data, size_t size, size_t count, FILE *stream)
+{
+  int n = fwrite (data, size, count, stream);
+  if ((size_t)n != count) {
+    perror ("write failed");
+    exit (1);
+  }
+}
+
 typedef struct _OMData OMData;
 typedef struct _OMInput OMInput;
 typedef struct _OMITrack OMITrack;
@@ -96,10 +115,22 @@
   ogg_page * new_og;
 
   new_og = malloc (sizeof (*og));
+  if (new_og == NULL) return NULL;
+
   new_og->header = malloc (og->header_len);
+  if (new_og->header == NULL) {
+    free (new_og);
+    return NULL;
+  }
   new_og->header_len = og->header_len;
   memcpy (new_og->header, og->header, og->header_len);
+
   new_og->body = malloc (og->body_len);
+  if (new_og->body == NULL) {
+    free (new_og->header);
+    free (new_og);
+    return NULL;
+  }
   new_og->body_len = og->body_len;
   memcpy (new_og->body, og->body, og->body_len);
 
@@ -129,6 +160,7 @@
   OMData * omdata;
 
   omdata = (OMData *) malloc (sizeof (OMData));
+  if (omdata == NULL) return NULL;
 
   omdata->inputs = oggz_table_new ();
   omdata->verbose = 0;
@@ -158,6 +190,7 @@
   OMInput * input = (OMInput *) user_data;
 
   input->og = _ogg_page_copy (og);
+  if (input->og == NULL) return OGGZ_STOP_ERR;
 
   return OGGZ_STOP_OK;
 }
@@ -226,7 +259,9 @@
 	    oggz_table_remove (omdata->inputs, key);
 	    ominput_delete (input);
 	    input = NULL;
-	  }
+	  } else if (n == OGGZ_ERR_STOP_ERR) {
+            exit_out_of_memory();
+          }
 	}
 	if (input && input->og) {
 	  if (ogg_page_bos ((ogg_page *)input->og)) {
@@ -306,8 +341,8 @@
     if (min_i != -1) {
       input = (OMInput *) oggz_table_nth (omdata->inputs, min_i, &key);
       og = input->og;
-      fwrite (og->header, 1, og->header_len, outfile);
-      fwrite (og->body, 1, og->body_len, outfile);
+      checked_fwrite (og->header, 1, og->header_len, outfile);
+      checked_fwrite (og->body, 1, og->body_len, outfile);
 
       _ogg_page_free (og);
       input->og = NULL;
@@ -323,7 +358,6 @@
   int show_version = 0;
   int show_help = 0;
 
-  char * progname;
   char * infilename = NULL, * outfilename = NULL;
   FILE * infile = NULL, * outfile = NULL;
   int used_stdin = 0; /* Flag usage of stdin, only use it once */
@@ -361,6 +395,8 @@
   }
 
   omdata = omdata_new();
+  if (omdata == NULL)
+    exit_out_of_memory();
 
   while (1) {
 #ifdef HAVE_GETOPT_LONG
@@ -428,7 +464,8 @@
       fprintf (stderr, "%s: unable to open input file %s\n", progname,
 	       infilename);
     } else {
-      omdata_add_input (omdata, infile);
+      if (omdata_add_input (omdata, infile) == NULL)
+        exit_out_of_memory();
     }
   }
 

Modified: liboggz/trunk/src/tools/oggz-sort.c
===================================================================
--- liboggz/trunk/src/tools/oggz-sort.c	2009-02-23 03:56:37 UTC (rev 3858)
+++ liboggz/trunk/src/tools/oggz-sort.c	2009-02-23 03:58:27 UTC (rev 3859)
@@ -44,6 +44,8 @@
 
 #define READ_SIZE 4096
 
+static char * progname;
+
 static void
 usage (char * progname)
 {
@@ -59,6 +61,23 @@
   printf ("Please report bugs to <ogg-dev at xiph.org>\n");
 }
 
+static void
+exit_out_of_memory (void)
+{
+  fprintf (stderr, "%s: Out of memory\n", progname);
+  exit (1);
+}
+
+static void
+checked_fwrite (const void *data, size_t size, size_t count, FILE *stream)
+{
+  int n = fwrite (data, size, count, stream);
+  if ((size_t)n != count) {
+    perror ("write failed");
+    exit (1);
+  }
+}
+
 typedef struct _OSData OSData;
 typedef struct _OSInput OSInput;
 typedef struct _OSITrack OSITrack;
@@ -86,10 +105,22 @@
   ogg_page * new_og;
 
   new_og = malloc (sizeof (*og));
+  if (new_og == NULL) return NULL;
+
   new_og->header = malloc (og->header_len);
+  if (new_og->header == NULL) {
+    free (new_og);
+    return NULL;
+  }
   new_og->header_len = og->header_len;
   memcpy (new_og->header, og->header, og->header_len);
+
   new_og->body = malloc (og->body_len);
+  if (new_og->body == NULL) {
+    free (new_og->header);
+    free (new_og);
+    return NULL;
+  }
   new_og->body_len = og->body_len;
   memcpy (new_og->body, og->body, og->body_len);
 
@@ -119,8 +150,14 @@
   OSData * osdata;
 
   osdata = (OSData *) malloc (sizeof (OSData));
+  if (osdata == NULL) return NULL;
 
   osdata->inputs = oggz_table_new ();
+  if (osdata->inputs == NULL) {
+    free (osdata);
+    return NULL;
+  }
+
   osdata->verbose = 0;
 
   return osdata;
@@ -152,6 +189,8 @@
   if (serialno == input->serialno) {
     ogg_page *iog;
     iog = _ogg_page_copy (og);
+    if (iog == NULL) return OGGZ_STOP_ERR;
+
     /* If this page's granulepos should be -1 but isn't then fix that before
      * storing and sorting the page. */
     if(ogg_page_packets(iog)==0&&ogg_page_granulepos(iog)!=-1) {
@@ -181,10 +220,15 @@
 
   if (is_bos) {
     input = (OSInput *) malloc (sizeof (OSInput));
-    if (input == NULL) return -1;
+    if (input == NULL) return OGGZ_STOP_ERR;
 
     input->osdata = osdata;
     input->reader = oggz_open (osdata->infilename, OGGZ_READ|OGGZ_AUTO);
+    if (input->reader == NULL) {
+      free (input);
+      return OGGZ_STOP_ERR;
+    }
+
     input->serialno = serialno;
     input->og = NULL;
 
@@ -193,7 +237,7 @@
     nfiles = oggz_table_size (osdata->inputs);
     if (!oggz_table_insert (osdata->inputs, nfiles++, input)) {
       osinput_delete (input);
-      return -1;
+      return OGGZ_STOP_ERR;
     }
 
     return OGGZ_CONTINUE;
@@ -255,6 +299,8 @@
 	    oggz_table_remove (osdata->inputs, key);
 	    osinput_delete (input);
 	    input = NULL;
+	  } else if (n == OGGZ_ERR_STOP_ERR) {
+            exit_out_of_memory();
 	  }
 	}
 	if (input && input->og) {
@@ -305,8 +351,8 @@
     if (min_i != -1) {
       input = (OSInput *) oggz_table_nth (osdata->inputs, min_i, &key);
       og = input->og;
-      fwrite (og->header, 1, og->header_len, outfile);
-      fwrite (og->body, 1, og->body_len, outfile);
+      checked_fwrite (og->header, 1, og->header_len, outfile);
+      checked_fwrite (og->body, 1, og->body_len, outfile);
 
       _ogg_page_free (og);
       input->og = NULL;
@@ -322,7 +368,6 @@
   int show_version = 0;
   int show_help = 0;
 
-  char * progname;
   char * infilename = NULL, * outfilename = NULL;
   FILE * infile = NULL, * outfile = NULL;
   OSData * osdata;
@@ -359,6 +404,8 @@
   }
 
   osdata = osdata_new();
+  if (osdata == NULL)
+    exit_out_of_memory();
 
   while (1) {
 #ifdef HAVE_GETOPT_LONG
@@ -407,7 +454,11 @@
   }
 
   infilename = argv[optind++];
-  osdata_add_file (osdata, infilename);
+  if (osdata_add_file (osdata, infilename) == -1) {
+    fprintf (stderr, "%s: unable to open input file %s\n",
+             progname, infilename);
+    goto exit_err;
+  }
 
   if (outfilename == NULL) {
     outfile = stdout;



More information about the commits mailing list