From 120e1e75ef6bbea1578eebf11ef6d11ec5ddc4f6 Mon Sep 17 00:00:00 2001
From: izabera <izaberina@gmail.com>
Date: Wed, 6 May 2015 12:26:20 +0200
Subject: [PATCH] replace patterns in empty strings

---
 lib/glob/gmisc.c |  3 ---
 subst.c          | 13 ++++++++++++-
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/lib/glob/gmisc.c b/lib/glob/gmisc.c
index c28f7b1..e3aecf9 100644
--- a/lib/glob/gmisc.c
+++ b/lib/glob/gmisc.c
@@ -248,9 +248,6 @@ match_pattern_char (pat, string, flags)
 {
   char c;
 
-  if (*string == 0)
-    return (0);
-
   switch (c = *pat++)
     {
     default:
diff --git a/subst.c b/subst.c
index cb45329..adf082d 100644
--- a/subst.c
+++ b/subst.c
@@ -4435,7 +4435,7 @@ match_pattern (string, pat, mtype, sp, ep)
   size_t slen, plen, mslen, mplen;
 #endif
 
-  if (string == 0 || *string == 0 || pat == 0 || *pat == 0)
+  if (string == 0 || pat == 0 || *pat == 0)
     return (0);
 
 #if defined (HANDLE_MULTIBYTE)
@@ -7041,6 +7041,8 @@ pat_subst (string, pat, rep, mflags)
    *	    with REP and return the result.
    *	2.  A null pattern with mtype == MATCH_END means to append REP to
    *	    STRING and return the result.
+   *	3.  A null STRING with a matching pattern means to append REP to
+   *	    STRING and return the result.
    * These don't understand or process `&' in the replacement string.
    */
   if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
@@ -7062,6 +7064,13 @@ pat_subst (string, pat, rep, mflags)
 	}
       return (ret);
     }
+  else if (( *string == 0 ) &&
+	   (match_pattern (string, pat, mtype, &s, &e) != 0))
+    {
+      ret = (char *)xmalloc (STRLEN(rep) + 1);
+      strcpy (ret, rep);
+      return (ret);
+    }
 
   ret = (char *)xmalloc (rsize = 64);
   ret[0] = '\0';
@@ -7121,6 +7130,8 @@ pat_subst (string, pat, rep, mflags)
 	  ret[rptr++] = *str++;
 	  e++;		/* avoid infinite recursion on zero-length match */
 	}
+
+      /* Check if the string is empty here, not inside match_pattern */
+      if ( *str == 0 ) break;
     }
 
   /* Now copy the unmatched portion of the input string */
-- 
2.4.0

