diff --git a/gdb/completer.c b/gdb/completer.c
index bf4137e..6aa8d35 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -157,7 +157,7 @@ filename_completer (struct completer_data *cdata,
   subsequent_name = 0;
   while (1)
     {
-      char *p, *q;
+      char *p;
 
       p = rl_filename_completion_function (text, subsequent_name);
       if (p == NULL)
@@ -175,26 +175,9 @@ filename_completer (struct completer_data *cdata,
 	  continue;
 	}
 
-      if (word == text)
-	/* Return exactly p.  */
-	q = p;
-      else if (word > text)
-	{
-	  /* Return some portion of p.  */
-	  q = xmalloc (strlen (p) + 5);
-	  strcpy (q, p + (word - text));
-	  xfree (p);
-	}
-      else
-	{
-	  /* Return some of TEXT plus p.  */
-	  q = xmalloc (strlen (p) + (text - word) + 5);
-	  strncpy (q, word, text - word);
-	  q[text - word] = '\0';
-	  strcat (q, p);
-	  xfree (p);
-	}
-      VEC_safe_push (char_ptr, return_val, q);
+      if (add_completion (cdata, &return_val, p, text, word)
+	  == ADD_COMPLETION_MAX_REACHED)
+	break;
     }
 #if 0
   /* There is no way to do this just long enough to affect quote
diff --git a/gdb/testsuite/gdb.base/completion.exp b/gdb/testsuite/gdb.base/completion.exp
index 710aac0..f0b7f2c 100644
--- a/gdb/testsuite/gdb.base/completion.exp
+++ b/gdb/testsuite/gdb.base/completion.exp
@@ -858,6 +858,10 @@ gdb_test "complete break need" "need_malloc"
 # Completion limiting.
 #
 
+# The terminal at the end of the complete command
+set end "\\\*\\\*\\\* List may be truncated, "
+append end "max-completions reached\\\. \\\*\\\*\\\*"
+
 gdb_test_no_output "set max-completions 5"
 
 set test "command-name completion limiting using tab character"
@@ -866,7 +870,7 @@ gdb_test_multiple "" "$test" {
     -re "^p\\\x07$" {
 	send_gdb "\t"
 	gdb_test_multiple "" "$test" {
-	    -re "List may be truncated, max-completions reached.*\r\n$gdb_prompt p$" {
+	    -re "$end\r\n$gdb_prompt p$" {
 		# Complete the command and ignore the output to resync
 		# gdb for the next test.
 		send_gdb "\n"
@@ -898,7 +902,8 @@ gdb_test_multiple "" "$test" {
     }
 }
 
-gdb_test_no_output "set max-completions 3"
+set max_completions 3
+gdb_test_no_output "set max-completions $max_completions"
 
 set test "symbol-name completion limiting using tab character"
 send_gdb "p marker\t"
@@ -906,7 +911,7 @@ gdb_test_multiple "" "$test" {
     -re "^p marker\\\x07$" {
 	send_gdb "\t"
 	gdb_test_multiple "" "$test" {
-	    -re "List may be truncated, max-completions reached.*\r\n$gdb_prompt p marker$" {
+	    -re "$end\r\n$gdb_prompt p marker$" {
 		# Complete the command and ignore the output to resync
 		# gdb for the next test.
 		send_gdb "\n"
@@ -933,11 +938,67 @@ gdb_test_multiple "" "$test" {
 set test "symbol-name completion limiting using complete command"
 send_gdb "complete p mark\n"
 gdb_test_multiple "" "$test" {
-    -re "List may be truncated, max-completions reached.*\r\n$gdb_prompt $" {
+    -re "$end\r\n$gdb_prompt $" {
 	pass "$test"
     }
 }
 
+# A convenience function for testing completion limiting.
+# CMD is a GDB command to to run with "complete".
+# PATTERN is a regexp pattern matching the expected output
+#   of completion items "seen" in the output.
+# NUM is the number of maximum completions expected.
+#
+# The test will use the test name "limit complete CMD"
+# and will only count the number of completion items matching
+# PATTERN.  No assumptions are made on the order of the items
+# seen in GDB's output.
+#
+# If NUM items are seen before the truncation message, the test
+# passes, otherwise it fails.  The test can also fail if no
+# truncation message is seen at all, in which case the test
+# failure message will say "(unlimited)".
+
+proc test_completion_limit {cmd pattern num} {
+    global gdb_prompt
+
+    # The terminal at the end of the complete command
+    set end "\\\*\\\*\\\* List may be truncated, "
+    append end "max-completions reached\\\. \\\*\\\*\\\*"
+
+    set cmdr [string_to_regexp $cmd]
+    set seen 0
+    gdb_test_multiple "complete $cmd" "limit complete $cmd" {
+	"complete $cmdr" { exp_continue }
+	-re "$pattern\r\n" {
+	    incr seen
+	    exp_continue
+	}
+	-re ".*$end\r\n$gdb_prompt $" {
+	    if {$seen == $num} {
+		pass "limit complete $cmd"
+	    } else {
+		fail "limit complete $cmd ($seen/$num)"
+	    }
+	}
+	-re ".*$gdb_prompt $" {
+	    fail "limit complete $cmd (unlimited)"
+	}
+    }
+}
+
+test_completion_limit "file ./gdb.base/jit-s" \
+    "file \\\./gdb\\\.base/jit-s(imple|olib|o)(\\\.c|\\\.exp)?" \
+    $max_completions
+
+# same as above but completing on directory names.
+test_completion_limit "file ./gdb.a" "file \\\./gdb\\\.a(da|rch|sm)" \
+    $max_completions
+
+#
+# Test TUI completions
+#
+
 gdb_test_no_output "set max-completions unlimited"
 
 if {![skip_tui_tests]} {
