shithub: libopusenc

Download patch

ref: a1539a993ce9f825a253b1ce9e8d4de8d2814c73
parent: 00053b2a3f6db71f330a4795419c27d9e999e87b
author: Jean-Marc Valin <[email protected]>
date: Fri May 5 17:12:33 EDT 2017

Properly cleanup on failure to open a file

--- a/examples/opusenc_example.c
+++ b/examples/opusenc_example.c
@@ -13,12 +13,13 @@
   }
   fin = fopen(argv[1], "r");
   if (!fin) {
-    printf("cannout open input file: %s\n", argv[1]);
+    fprintf(stderr, "cannot open input file: %s\n", argv[1]);
     return 1;
   }
   enc = ope_create_file(argv[2], 48000, 2, 0, &error);
   if (!enc) {
-    printf("cannout open output file: %s\n", argv[2]);
+    fprintf(stderr, "cannout open output file: %s\n", argv[2]);
+    fclose(fin);
     return 1;
   }
   ope_add_comment(enc, "ARTIST", "Someone");
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -166,7 +166,8 @@
 
 int stdio_close(void *user_data) {
   struct StdioObject *obj = (struct StdioObject*)user_data;
-  int ret = fclose(obj->file);
+  int ret = 0;
+  if (obj->file) ret = fclose(obj->file);
   free(obj);
   return ret;
 }
@@ -188,8 +189,7 @@
   obj->file = fopen(path, "wb");
   if (!obj->file) {
     if (error) *error = OPE_CANNOT_OPEN;
-    /* FIXME: Destroy the encoder properly. */
-    free(obj);
+    ope_destroy(enc);
     return NULL;
   }
   return enc;
@@ -624,7 +624,7 @@
   if (enc->chaining_keyframe) free(enc->chaining_keyframe);
   free(enc->buffer);
 #ifdef USE_OGGP
-  oggp_destroy(enc->oggp);
+  if (enc->oggp) oggp_destroy(enc->oggp);
 #endif
   opus_multistream_encoder_destroy(enc->st);
   if (enc->re) speex_resampler_destroy(enc->re);