diff --git a/lz77/lz77.c b/lz77/lz77.c
index 1ffa0c8046fce063ec8d0de291bc9d900f84a26b..ca35f4b328fcb733fa5b084f9c671a5beb909d55 100644
--- a/lz77/lz77.c
+++ b/lz77/lz77.c
@@ -161,7 +161,7 @@ int main(int argc, char *argv[])
                     size_t dif = 0;
                     size_t dist = cursor - pos->value;
                                                                  /* take mod to account for case when length longer than distance */
-                    while (cursor + dif < input_file_length && inp[cursor + dif] == inp[pos->value + dif % dist]) dif += 1;
+                    while (cursor + dif < input_file_length && inp[cursor + dif] == inp[pos->value + dif % dist] && dif < 258) dif += 1;
                     assert(dif >= 3);
                     if (dif > best_len)
                     {