diff --git a/ChangeLog b/ChangeLog
index ac16f711c737af1321217a1fcc31a5c9f2b2dd59..eb00c5a33b2ccaa401600e3e3c28f2e925b8bb65 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2016-02-20  Benno Schulenberg  <bensberg@justemail.net>
+	* src/search.c (get_history_completion): Avoid leaking memory
+	when tabbing on a string that does not occur in the history.
+	This fixes Savannah bug #47124 reported by Mike Frysinger.
+
 2016-02-18  Benno Schulenberg  <bensberg@justemail.net>
 	* src/search.c (do_replace_loop), src/text.c (do_int_spell_fix),
 	src/winio.c (edit_refresh): Fix Savannah bug #47127 the proper way.
diff --git a/src/prompt.c b/src/prompt.c
index 6c63c0c9ada870c30c248b87413d1b5eb87effa0..d2ddd1c3faef10ea0e58eda6e81e198a28bbc391 100644
--- a/src/prompt.c
+++ b/src/prompt.c
@@ -616,9 +616,8 @@ functionptrtype get_prompt_string(int *actual, bool allow_tabs,
 		    complete_len = strlen(answer);
 
 		if (complete_len > 0) {
-		    answer = mallocstrcpy(answer,
-				get_history_completion(history_list,
-					answer, complete_len));
+		    answer = get_history_completion(history_list,
+					answer, complete_len);
 		    statusbar_x = strlen(answer);
 		}
 	    } else
diff --git a/src/proto.h b/src/proto.h
index 06c8c2b61adf3f45497f0c5b6c251670a390eb6f..864e5e8982323be18a49b31082e8d81af474a00a 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -623,7 +623,7 @@ void do_gotolinecolumn_void(void);
 bool find_bracket_match(bool reverse, const char *bracket_set);
 void do_find_bracket(void);
 #ifndef DISABLE_TABCOMP
-char *get_history_completion(filestruct **h, const char *s, size_t len);
+char *get_history_completion(filestruct **h, char *s, size_t len);
 #endif
 #endif
 #ifndef DISABLE_HISTORIES
diff --git a/src/search.c b/src/search.c
index 85345466194958ee35cac108b38784197c01f088..b94e5b4e55d83a9390976bb475604d21b7ae87d2 100644
--- a/src/search.c
+++ b/src/search.c
@@ -1351,7 +1351,7 @@ void get_history_older_void(void)
  * looking at only the first len characters of s, and return that
  * string.  If there isn't one, or if len is 0, don't move h and return
  * s. */
-char *get_history_completion(filestruct **h, const char *s, size_t len)
+char *get_history_completion(filestruct **h, char *s, size_t len)
 {
     assert(s != NULL);
 
@@ -1380,7 +1380,7 @@ char *get_history_completion(filestruct **h, const char *s, size_t len)
 
 	if (p != NULL) {
 	    *h = p;
-	    return (*h)->data;
+	    return mallocstrcpy(s, (*h)->data);
 	}
 
 	/* Search the history list from the top to the current position
@@ -1392,7 +1392,7 @@ char *get_history_completion(filestruct **h, const char *s, size_t len)
 
 	if (p != NULL) {
 	    *h = p;
-	    return (*h)->data;
+	    return mallocstrcpy(s, (*h)->data);
 	}
     }