diff --git a/src/softmagic.c b/src/softmagic.c
index 8d08cad..8262788 100644
--- a/src/softmagic.c
+++ b/src/softmagic.c
@@ -199,9 +199,9 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
if (file_check_mem(ms, ++cont_level) == -1)
return -1;
- while (magic[magindex+1].cont_level != 0 &&
- ++magindex < nmagic) {
- m = &magic[magindex];
+ while (magindex + 1 < nmagic &&
+ magic[magindex + 1].cont_level != 0) {
+ m = &magic[++magindex];
ms->line = m->lineno; /* for messages */
if (cont_level < m->cont_level)