Blob Blame History Raw
diff --git a/src/errors.h b/src/errors.h
index 3008020..3daf1a6 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -381,3 +381,5 @@ EXTERN char e_missing_end_block[]
 	INIT(= N_("E1171: Missing } after inline function"));
 EXTERN char e_cannot_use_default_values_in_lambda[]
 	INIT(= N_("E1172: Cannot use default values in a lambda"));
+EXTERN char e_resulting_text_too_long[]
+	INIT(= N_("E1240: Resulting text too long"));
diff --git a/src/indent.c b/src/indent.c
index 4f909d0..77d8b0a 100644
--- a/src/indent.c
+++ b/src/indent.c
@@ -1696,6 +1696,11 @@ ex_retab(exarg_T *eap)
 	    if (ptr[col] == NUL)
 		break;
 	    vcol += chartabsize(ptr + col, (colnr_T)vcol);
+	    if (vcol >= MAXCOL)
+	    {
+		emsg(_(e_resulting_text_too_long));
+		break;
+	    }
 	    if (has_mbyte)
 		col += (*mb_ptr2len)(ptr + col);
 	    else
diff --git a/src/testdir/test_retab.vim b/src/testdir/test_retab.vim
index c7190aa..6133e8f 100644
--- a/src/testdir/test_retab.vim
+++ b/src/testdir/test_retab.vim
@@ -70,6 +70,8 @@ func Test_retab()
   call assert_equal("    a       b        c    ",         Retab('!', 3))
   call assert_equal("    a       b        c    ",         Retab('',  5))
   call assert_equal("    a       b        c    ",         Retab('!', 5))
+
+  set tabstop& expandtab&
 endfunc
 
 func Test_retab_error()
@@ -80,4 +82,21 @@ func Test_retab_error()
   call assert_fails('ret 80000000000000000000', 'E475:')
 endfunc
 
+func Test_retab_endless()
+  new
+  call setline(1, "\t0\t")
+  let caught = 'no'
+  try
+    while 1
+      set ts=4000
+      retab 4
+    endwhile
+  catch /E1240/
+    let caught = 'yes'
+  endtry
+  bwipe!
+  set tabstop&
+endfunc
+
+
 " vim: shiftwidth=2 sts=2 expandtab