diff --git a/libavcodec/faxcompr.c b/libavcodec/faxcompr.c
index 5aa7885679809f354f75813f5218ed5406e2a8b1..e7f9d00a115ccd2e133008845003bc2be291b570 100644
--- a/libavcodec/faxcompr.c
+++ b/libavcodec/faxcompr.c
@@ -228,7 +228,7 @@ static int decode_group3_2d_line(AVCodecContext *avctx, GetBitContext *gb,
             mode = !mode;
         }
         //sync line pointers
-        while(run_off <= offs){
+        while(offs < width && run_off <= offs){
             run_off += *ref++;
             run_off += *ref++;
         }