shithub: opus

Download patch

ref: 28b1d248a775cfdd7bd11281db0b635124647303
parent: cb05e7cd962fc82d1ea46932c064f13481ca6390
author: Gregory Maxwell <[email protected]>
date: Mon Apr 23 09:51:23 EDT 2012

Fix negative write bug in opus_demo and add some fwrite() error handling.

--- a/src/opus_demo.c
+++ b/src/opus_demo.c
@@ -665,10 +665,19 @@
         {
             unsigned char int_field[4];
             int_to_char(len[toggle], 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(enc_final_range[toggle], int_field);
-            fwrite(int_field, 1, 4, fout);
-            fwrite(data[toggle], 1, len[toggle], fout);
+            if (fwrite(int_field, 1, 4, fout) != 4) {
+               fprintf(stderr, "Error writing.\n");
+               return EXIT_FAILURE;
+            }
+            if (fwrite(data[toggle], 1, len[toggle], fout) != (unsigned)len[toggle]) {
+               fprintf(stderr, "Error writing.\n");
+               return EXIT_FAILURE;
+            }
         } else {
             int output_samples;
             lost = len[toggle]==0 || (packet_loss_perc>0 && rand()%100 < packet_loss_perc);
@@ -687,8 +696,14 @@
                 }
                 if (output_samples>0)
                 {
-                    fwrite(out+skip*channels, sizeof(short)*channels, output_samples-skip, fout);
-                    skip = 0;
+                    if (output_samples>skip) {
+                       if (fwrite(out+skip*channels, sizeof(short)*channels, output_samples-skip, fout) != (unsigned)(output_samples-skip)){
+                          fprintf(stderr, "Error writing.\n");
+                          return EXIT_FAILURE;
+                       }
+                    }
+                    if (output_samples<skip) skip -= output_samples;
+                    else skip = 0;
                 } else {
                    fprintf(stderr, "error decoding frame: %s\n",
                                    opus_strerror(output_samples));