shithub: libopusenc

Download patch

ref: 6d46f2d7f7617852bab7cbb2f7cae8350b99204e
parent: 205552370e058d99452fd34983942e10f2db6dff
author: Mark Harris <[email protected]>
date: Mon Dec 28 12:01:39 EST 2020

Fix use of uninitialized fields

enc->streams->end_granule used uninitialized in encode_buffer() if the
stream contains no audio (opusenc_example /dev/null out.opus).

enc->frame_size_request used uninitialized in encode_buffer() if the
frame size was not explicitly set.

enc->callbacks used uninitialized if the encoder is created with
ope_encoder_create_callbacks() and callbacks is NULL.

--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -361,8 +361,7 @@
   free(stream);
 }
 
-/* Create a new OggOpus file (callback-based). */
-OggOpusEnc *ope_encoder_create_callbacks(const OpusEncCallbacks *callbacks, void *user_data,
+static OggOpusEnc *ope_encoder_create_callbacks_impl(const OpusEncCallbacks *callbacks, void *user_data,
     OggOpusComments *comments, opus_int32 rate, int channels, int family, int *error) {
   OggOpusEnc *enc=NULL;
   int ret;
@@ -395,11 +394,11 @@
   enc->oggp = NULL;
   /* Not initializing anything is an unrecoverable error. */
   enc->unrecoverable = family == -1 ? OPE_TOO_LATE : 0;
-  enc->pull_api = 0;
   enc->packet_callback = NULL;
   enc->rate = rate;
   enc->channels = channels;
   enc->frame_size = 960;
+  enc->frame_size_request = OPUS_FRAMESIZE_20_MS;
   enc->decision_delay = 96000;
   enc->max_ogg_delay = 48000;
   enc->chaining_keyframe = NULL;
@@ -447,8 +446,12 @@
   if (callbacks != NULL)
   {
     enc->callbacks = *callbacks;
+    enc->pull_api = 0;
+  } else {
+    enc->pull_api = 1;
   }
   enc->streams->user_data = user_data;
+  enc->streams->end_granule = 0;
   if (error) *error = OPE_OK;
   return enc;
 fail:
@@ -462,11 +465,19 @@
   return NULL;
 }
 
+/* Create a new OggOpus stream (callback-based). */
+OggOpusEnc *ope_encoder_create_callbacks(const OpusEncCallbacks *callbacks, void *user_data,
+    OggOpusComments *comments, opus_int32 rate, int channels, int family, int *error) {
+  if (callbacks == NULL) {
+    if (error) *error = OPE_BAD_ARG;
+    return NULL;
+  }
+  return ope_encoder_create_callbacks_impl(callbacks, user_data, comments, rate, channels, family, error);
+}
+
 /* Create a new OggOpus stream, pulling one page at a time. */
 OggOpusEnc *ope_encoder_create_pull(OggOpusComments *comments, opus_int32 rate, int channels, int family, int *error) {
-  OggOpusEnc *enc = ope_encoder_create_callbacks(NULL, NULL, comments, rate, channels, family, error);
-  if (enc) enc->pull_api = 1;
-  return enc;
+  return ope_encoder_create_callbacks_impl(NULL, NULL, comments, rate, channels, family, error);
 }
 
 int ope_encoder_deferred_init_with_mapping(OggOpusEnc *enc, int family, int streams,