[FFmpeg-cvslog] In avcodec_open(), set return code to an error value only when an error occurs

Justin Ruggles git at videolan.org
Wed Apr 20 04:52:19 CEST 2011


ffmpeg | branch: master | Justin Ruggles <justin.ruggles at gmail.com> | Sun Apr 17 16:57:55 2011 -0400| [31d76ec2883c04d29d1f173ea99e605a6936613a] | committer: Justin Ruggles

In avcodec_open(), set return code to an error value only when an error occurs
instead of unconditionally at the start of the function.

This fixes a bug where a successful call to ff_thread_init() masks errors that
occur after that point in the function.  It also makes future bugs like this
less likely since the error code is now set near to the point in the code
where the error is found.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=31d76ec2883c04d29d1f173ea99e605a6936613a
---

 libavcodec/utils.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index a46dda7..0190e66 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -456,7 +456,7 @@ AVFrame *avcodec_alloc_frame(void){
 
 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
 {
-    int ret= -1;
+    int ret = 0;
 
     /* If there is a user-supplied mutex locking routine, call it. */
     if (ff_lockmgr_cb) {
@@ -467,11 +467,14 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
     entangled_thread_counter++;
     if(entangled_thread_counter != 1){
         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
+        ret = -1;
         goto end;
     }
 
-    if(avctx->codec || !codec)
+    if(avctx->codec || !codec) {
+        ret = AVERROR(EINVAL);
         goto end;
+    }
 
     if (codec->priv_data_size > 0) {
       if(!avctx->priv_data){
@@ -521,6 +524,7 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
+        ret = AVERROR(EINVAL);
         goto free_and_end;
     }
     avctx->frame_number = 0;
@@ -535,6 +539,7 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
     if (avctx->codec->max_lowres < avctx->lowres) {
         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
                avctx->codec->max_lowres);
+        ret = AVERROR(EINVAL);
         goto free_and_end;
     }
     if (avctx->codec->sample_fmts && avctx->codec->encode) {
@@ -544,6 +549,7 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
                 break;
         if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
             av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
+            ret = AVERROR(EINVAL);
             goto free_and_end;
         }
     }
@@ -554,7 +560,6 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
             goto free_and_end;
         }
     }
-    ret=0;
 end:
     entangled_thread_counter--;
 



More information about the ffmpeg-cvslog mailing list