]> granicus.if.org Git - handbrake/commitdiff
libhb: prevent crashes in hb_get_preview2
authorJohn Stebbins <jstebbins.hb@gmail.com>
Wed, 21 Oct 2015 16:36:16 +0000 (09:36 -0700)
committerJohn Stebbins <jstebbins.hb@gmail.com>
Mon, 26 Oct 2015 15:32:38 +0000 (08:32 -0700)
Try to avoid failures to initialize sws context by setting minimum
dimensions.  And if initialization does fail, exit gracefully instead of
crashing in sws_scale.

libhb/hb.c

index c5c3c8cf14ead7307ee0b729a0564c911ab23c42..0665f1b511fdb5b538606da91a03f4ba8ea77860 100644 (file)
@@ -758,7 +758,8 @@ hb_image_t* hb_get_preview2(hb_handle_t * h, int title_idx, int picture,
                             hb_geometry_settings_t *geo, int deinterlace)
 {
     char                 filename[1024];
-    hb_buffer_t        * in_buf, * deint_buf = NULL, * preview_buf;
+    hb_buffer_t        * in_buf = NULL, * deint_buf = NULL;
+    hb_buffer_t        * preview_buf = NULL;
     uint32_t             swsflags;
     AVPicture            pic_in, pic_preview, pic_deint, pic_crop;
     struct SwsContext  * context;
@@ -767,6 +768,17 @@ hb_image_t* hb_get_preview2(hb_handle_t * h, int title_idx, int picture,
                 geo->geometry.par.num / geo->geometry.par.den;
     int height = geo->geometry.height;
 
+    // Set minimum dimensions to prevent failure to initialize
+    // sws context
+    if (width < 32)
+    {
+        width = 32;
+    }
+    if (height < 32)
+    {
+        height = 32;
+    }
+
     swsflags = SWS_LANCZOS | SWS_ACCURATE_RND;
 
     preview_buf = hb_frame_buffer_init(AV_PIX_FMT_RGB32, width, height);
@@ -816,6 +828,12 @@ hb_image_t* hb_get_preview2(hb_handle_t * h, int title_idx, int picture,
                 title->geometry.height - (geo->crop[0] + geo->crop[1]),
                 AV_PIX_FMT_YUV420P, width, height, AV_PIX_FMT_RGB32, swsflags);
 
+    if (context == NULL)
+    {
+        // if by chance hb_sws_get_context fails, don't crash in sws_scale
+        goto fail;
+    }
+
     // Scale
     sws_scale(context,
               (const uint8_t* const *)pic_crop.data, pic_crop.linesize,
@@ -836,6 +854,10 @@ hb_image_t* hb_get_preview2(hb_handle_t * h, int title_idx, int picture,
 
 fail:
 
+    hb_buffer_close( &in_buf );
+    hb_buffer_close( &deint_buf );
+    hb_buffer_close( &preview_buf );
+
     image = hb_image_init(AV_PIX_FMT_RGB32, width, height);
     return image;
 }