shithub: opus

Download patch

ref: 4f0c07f640964ec75b6643fa09ca1290a01f1076
parent: f7ceaa609a0c0c00b50e04d20fa4ca521ce5e052
author: Gregory Maxwell <[email protected]>
date: Mon Apr 23 09:52:00 EDT 2012

Add fwrite() error handling in repacketizer_demo.

--- a/src/repacketizer_demo.c
+++ b/src/repacketizer_demo.c
@@ -69,7 +69,7 @@
    if (argc < 3)
    {
       usage(argv[0]);
-      return 1;
+      return EXIT_FAILURE;
    }
    for (i=1;i<argc-2;i++)
    {
@@ -79,7 +79,7 @@
          if(merge<1)
          {
             fprintf(stderr, "-merge parameter must be at least 1.\n");
-            return 1;
+            return EXIT_FAILURE;
          }
          i++;
       } else if (strcmp(argv[i], "-split")==0)
@@ -88,7 +88,7 @@
       {
          fprintf(stderr, "Unknown option: %s\n", argv[i]);
          usage(argv[0]);
-         return 1;
+         return EXIT_FAILURE;
       }
    }
    fin = fopen(argv[argc-2], "r");
@@ -95,7 +95,7 @@
    if(fin==NULL)
    {
      fprintf(stderr, "Error opening input file: %s\n", argv[argc-2]);
-     return 1;
+     return EXIT_FAILURE;
    }
    fout = fopen(argv[argc-1], "w");
    if(fout==NULL)
@@ -102,7 +102,7 @@
    {
      fprintf(stderr, "Error opening output file: %s\n", argv[argc-1]);
      fclose(fin);
-     return 1;
+     return EXIT_FAILURE;
    }
 
    rp = opus_repacketizer_create();
@@ -126,7 +126,7 @@
                 fprintf(stderr, "Invalid payload length\n");
                 fclose(fin);
                 fclose(fout);
-                return 1;
+                return EXIT_FAILURE;
              }
              break;
          }
@@ -156,10 +156,19 @@
          if (err>0) {
             unsigned char int_field[4];
             int_to_char(err, int_field);
-            fwrite(int_field, 1, 4, fout);
+            if(fwrite(int_field, 1, 4, fout)!=4){
+               fprintf(stderr, "Error writing.\n");
+               return EXIT_FAILURE;
+            }
             int_to_char(rng[nb_packets-1], int_field);
-            fwrite(int_field, 1, 4, fout);
-            fwrite(output_packet, 1, err, fout);
+            if (fwrite(int_field, 1, 4, fout)!=4) {
+               fprintf(stderr, "Error writing.\n");
+               return EXIT_FAILURE;
+            }
+            if (fwrite(output_packet, 1, err, fout)!=(unsigned)err) {
+               fprintf(stderr, "Error writing.\n");
+               return EXIT_FAILURE;
+            }
             /*fprintf(stderr, "out len = %d\n", err);*/
          } else {
             fprintf(stderr, "opus_repacketizer_out() failed: %s\n", opus_strerror(err));
@@ -172,13 +181,22 @@
             if (err>0) {
                unsigned char int_field[4];
                int_to_char(err, int_field);
-               fwrite(int_field, 1, 4, fout);
+               if (fwrite(int_field, 1, 4, fout)!=4) {
+                  fprintf(stderr, "Error writing.\n");
+                  return EXIT_FAILURE;
+               }
                if (i==nb_frames-1)
                   int_to_char(rng[nb_packets-1], int_field);
                else
                   int_to_char(0, int_field);
-               fwrite(int_field, 1, 4, fout);
-               fwrite(output_packet, 1, err, fout);
+               if (fwrite(int_field, 1, 4, fout)!=4) {
+                  fprintf(stderr, "Error writing.\n");
+                  return EXIT_FAILURE;
+               }
+               if (fwrite(output_packet, 1, err, fout)!=(unsigned)err) {
+                  fprintf(stderr, "Error writing.\n");
+                  return EXIT_FAILURE;
+               }
                /*fprintf(stderr, "out len = %d\n", err);*/
             } else {
                fprintf(stderr, "opus_repacketizer_out() failed: %s\n", opus_strerror(err));
@@ -186,10 +204,9 @@
 
          }
       }
-
    }
 
    fclose(fin);
    fclose(fout);
-   return 0;
+   return EXIT_SUCCESS;
 }