diff --git a/src/encode/lzh.c b/src/encode/lzh.c
index bb364e2f56a88852349d75c9e081d283a8f3628e..f28251fc580331db1f251e5ffc3f4bdfffbdbc80 100644
--- a/src/encode/lzh.c
+++ b/src/encode/lzh.c
@@ -468,7 +468,7 @@ static void lzh_update(lzh_t* lzh, short int c)
 
 			c = l;
 		}
-	} while ((c = lzh->prnt[c]) != 0);	/* do it until reaching the root */
+	} while (((c = lzh->prnt[c]) != 0) && c < (sizeof(lzh->son)/sizeof(lzh->son[0])));	/* do it until reaching the root */
 }
 
 static void lzh_encode_char(lzh_t* lzh, unsigned short c, uint8_t *outbuf, int32_t *outlen)