diff --git a/libavcodec/sgidec.c b/libavcodec/sgidec.c
index 5741945dd2e2c94536dd1b611451bfc8f2c2b39a..e9d7731e9aa2dba313b5f078006129a3ba1cbea9 100644
--- a/libavcodec/sgidec.c
+++ b/libavcodec/sgidec.c
@@ -101,7 +101,7 @@ static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
             dest_row -= s->linesize;
             start_offset = bytestream2_get_be32(&g_table);
             bytestream2_seek(&s->g, start_offset, SEEK_SET);
-            if (expand_rle_row(s, dest_row + z, dest_row + FFABS(s->linesize),
+            if (expand_rle_row(s, dest_row + z, dest_row + s->width*s->depth,
                                s->depth) != s->width) {
                 return AVERROR_INVALIDDATA;
             }