patch 8.2.2121: internal error when using \ze before \zs in a pattern

Problem:    Internal error when using \ze before \zs in a pattern.
Solution:   Check the end is never before the start. (closes #7442)
diff --git a/src/regexp_bt.c b/src/regexp_bt.c
index 03b07a1..476f34e 100644
--- a/src/regexp_bt.c
+++ b/src/regexp_bt.c
@@ -4805,6 +4805,23 @@
     if (backpos.ga_maxlen > BACKPOS_INITIAL)
 	ga_clear(&backpos);
 
+    // Make sure the end is never before the start.  Can happen when \zs and
+    // \ze are used.
+    if (REG_MULTI)
+    {
+	lpos_T *start = &rex.reg_mmatch->startpos[0];
+	lpos_T *end = &rex.reg_mmatch->endpos[0];
+
+	if (end->lnum < start->lnum
+			|| (end->lnum == start->lnum && end->col < start->col))
+	    rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
+    }
+    else
+    {
+	if (rex.reg_match->endp[0] < rex.reg_match->startp[0])
+	    rex.reg_match->endp[0] = rex.reg_match->startp[0];
+    }
+
     return retval;
 }
 
diff --git a/src/regexp_nfa.c b/src/regexp_nfa.c
index c75c575..da80ee9 100644
--- a/src/regexp_nfa.c
+++ b/src/regexp_nfa.c
@@ -7225,6 +7225,23 @@
 #endif
 
 theend:
+    // Make sure the end is never before the start.  Can happen when \zs and
+    // \ze are used.
+    if (REG_MULTI)
+    {
+	lpos_T *start = &rex.reg_mmatch->startpos[0];
+	lpos_T *end = &rex.reg_mmatch->endpos[0];
+
+	if (end->lnum < start->lnum
+			|| (end->lnum == start->lnum && end->col < start->col))
+	    rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
+    }
+    else
+    {
+	if (rex.reg_match->endp[0] < rex.reg_match->startp[0])
+	    rex.reg_match->endp[0] = rex.reg_match->startp[0];
+    }
+
     return retval;
 }
 
diff --git a/src/testdir/test_regexp_latin.vim b/src/testdir/test_regexp_latin.vim
index d1885ab..0b00244 100644
--- a/src/testdir/test_regexp_latin.vim
+++ b/src/testdir/test_regexp_latin.vim
@@ -911,6 +911,13 @@
   bwipe!
 endfunc
 
+func Test_ze_before_zs()
+  call assert_equal('', matchstr(' ', '\%#=1\ze \zs'))
+  call assert_equal('', matchstr(' ', '\%#=2\ze \zs'))
+  call assert_equal(repeat([''], 10), matchlist(' ', '\%#=1\ze \zs'))
+  call assert_equal(repeat([''], 10), matchlist(' ', '\%#=2\ze \zs'))
+endfunc
+
 " Check for detecting error
 func Test_regexp_error()
   call assert_fails("call matchlist('x x', '\\%#=1 \\zs*')", 'E888:')
diff --git a/src/version.c b/src/version.c
index 9d09c07..9bb587d 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    2121,
+/**/
     2120,
 /**/
     2119,