From e05fb81a20541833a2d62ce08552b18c0920b9a1 Mon Sep 17 00:00:00 2001
From: Cosmin Truta <ctruta@gmail.com>
Date: Sat, 25 Nov 2017 23:17:46 -0500
Subject: [PATCH] gifread: Detect indirect circular dependencies in LZW tables

---
 src/gifread/gifread.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/gifread/gifread.c b/src/gifread/gifread.c
index 0123456789abcdef..0123456789abcdef 100644
--- a/src/gifread/gifread.c
+++ b/src/gifread/gifread.c
@@ -499,6 +499,8 @@ static int LZWReadByte(int init_flag, int input_code_size, FILE *stream)
             *sp++ = table[1][code];
             if (code == table[0][code])
                 GIFError("GIF/LZW error: circular table entry");
+            if ((size_t)(sp - stack) >= sizeof(stack) / sizeof(stack[0]))
+                GIFError("GIF/LZW error: circular table");
             code = table[0][code];
         }
 
