diff --git a/libavcodec/faxcompr.c b/libavcodec/faxcompr.c
index ec6b9e00ab038b5f81dd89030deb93ee9e0c7291..62775125e8d93b2e013bd190fe80d5722d646b78 100644
--- a/libavcodec/faxcompr.c
+++ b/libavcodec/faxcompr.c
@@ -196,11 +196,11 @@ static int decode_group3_2d_line(AVCodecContext *avctx, GetBitContext *gb,
                 }
                 *runs++ = run + saved_run;
                 saved_run = 0;
-                if(pix_left < run){
+                pix_left -= run;
+                if(pix_left < 0){
                     av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\n");
                     return -1;
                 }
-                pix_left -= run;
                 offs += run;
                 mode = !mode;
             }