Blame SOURCES/make-4.3-filter-out.patch

705343
From e49e11e069fe7f214263be1782242b9b50f71eaa Mon Sep 17 00:00:00 2001
705343
From: Paul Smith <psmith@gnu.org>
705343
Date: Thu, 12 Nov 2020 17:00:39 -0500
705343
Subject: [SV 59093] Rewrite filter/filter-out to avoid large stack usage
705343
705343
* src/function.c (func_filter_filterout): Allocate arrays to hold
705343
pattern and word information rather than creating linked lists on
705343
the stack.
705343
* tests/scripts/functions/filter-out: Test large filters.
705343
705343
diff --git a/src/function.c b/src/function.c
705343
index 0917e0cd..5edfe8b3 100644
705343
--- a/src/function.c
705343
+++ b/src/function.c
705343
@@ -910,7 +910,6 @@ func_foreach (char *o, char **argv, const char *funcname UNUSED)
705343
 
705343
 struct a_word
705343
 {
705343
-  struct a_word *next;
705343
   struct a_word *chain;
705343
   char *str;
705343
   size_t length;
705343
@@ -941,7 +940,6 @@ a_word_hash_cmp (const void *x, const void *y)
705343
 
705343
 struct a_pattern
705343
 {
705343
-  struct a_pattern *next;
705343
   char *str;
705343
   char *percent;
705343
   size_t length;
705343
@@ -950,78 +948,84 @@ struct a_pattern
705343
 static char *
705343
 func_filter_filterout (char *o, char **argv, const char *funcname)
705343
 {
705343
-  struct a_word *wordhead;
705343
-  struct a_word **wordtail;
705343
+  struct a_word *words;
705343
+  struct a_word *word_end;
705343
   struct a_word *wp;
705343
-  struct a_pattern *pathead;
705343
-  struct a_pattern **pattail;
705343
+  struct a_pattern *patterns;
705343
+  struct a_pattern *pat_end;
705343
   struct a_pattern *pp;
705343
+  size_t pat_count = 0, word_count = 0;
705343
 
705343
   struct hash_table a_word_table;
705343
   int is_filter = funcname[CSTRLEN ("filter")] == '\0';
705343
-  const char *pat_iterator = argv[0];
705343
-  const char *word_iterator = argv[1];
705343
+  const char *cp;
705343
   int literals = 0;
705343
-  int words = 0;
705343
   int hashing = 0;
705343
   char *p;
705343
   size_t len;
705343
+  int doneany = 0;
705343
 
705343
-  /* Chop ARGV[0] up into patterns to match against the words.
705343
-     We don't need to preserve it because our caller frees all the
705343
-     argument memory anyway.  */
705343
+  /* Find the number of words and get memory for them.  */
705343
+  cp = argv[1];
705343
+  while ((p = find_next_token (&cp, NULL)) != 0)
705343
+    ++word_count;
705343
 
705343
-  pattail = &pathead;
705343
-  while ((p = find_next_token (&pat_iterator, &len)) != 0)
705343
-    {
705343
-      struct a_pattern *pat = alloca (sizeof (struct a_pattern));
705343
+  if (!word_count)
705343
+    return o;
705343
+
705343
+  words = xcalloc (word_count * sizeof (struct a_word));
705343
+  word_end = words + word_count;
705343
 
705343
-      *pattail = pat;
705343
-      pattail = &pat->next;
705343
+  /* Find the number of patterns and get memory for them.  */
705343
+  cp = argv[0];
705343
+  while ((p = find_next_token (&cp, NULL)) != 0)
705343
+    ++pat_count;
705343
 
705343
-      if (*pat_iterator != '\0')
705343
-        ++pat_iterator;
705343
+  patterns = xcalloc (pat_count * sizeof (struct a_pattern));
705343
+  pat_end = patterns + pat_count;
705343
+
705343
+  /* Chop argv[0] up into patterns to match against the words.  */
705343
+
705343
+  cp = argv[0];
705343
+  pp = patterns;
705343
+  while ((p = find_next_token (&cp, &len)) != 0)
705343
+    {
705343
+      if (*cp != '\0')
705343
+        ++cp;
705343
 
705343
-      pat->str = p;
705343
       p[len] = '\0';
705343
-      pat->percent = find_percent (p);
705343
-      if (pat->percent == 0)
705343
+      pp->str = p;
705343
+      pp->percent = find_percent (p);
705343
+      if (pp->percent == 0)
705343
         literals++;
705343
-
705343
       /* find_percent() might shorten the string so LEN is wrong.  */
705343
-      pat->length = strlen (pat->str);
705343
+      pp->length = strlen (pp->str);
705343
+
705343
+      ++pp;
705343
     }
705343
-  *pattail = 0;
705343
 
705343
   /* Chop ARGV[1] up into words to match against the patterns.  */
705343
 
705343
-  wordtail = &wordhead;
705343
-  while ((p = find_next_token (&word_iterator, &len)) != 0)
705343
+  cp = argv[1];
705343
+  wp = words;
705343
+  while ((p = find_next_token (&cp, &len)) != 0)
705343
     {
705343
-      struct a_word *word = alloca (sizeof (struct a_word));
705343
-
705343
-      *wordtail = word;
705343
-      wordtail = &word->next;
705343
-
705343
-      if (*word_iterator != '\0')
705343
-        ++word_iterator;
705343
+      if (*cp != '\0')
705343
+        ++cp;
705343
 
705343
       p[len] = '\0';
705343
-      word->str = p;
705343
-      word->length = len;
705343
-      word->matched = 0;
705343
-      word->chain = 0;
705343
-      words++;
705343
+      wp->str = p;
705343
+      wp->length = len;
705343
+      ++wp;
705343
     }
705343
-  *wordtail = 0;
705343
 
705343
   /* Only use a hash table if arg list lengths justifies the cost.  */
705343
-  hashing = (literals >= 2 && (literals * words) >= 10);
705343
+  hashing = (literals > 1 && (literals * word_count) >= 10);
705343
   if (hashing)
705343
     {
705343
-      hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2,
705343
+      hash_init (&a_word_table, word_count, a_word_hash_1, a_word_hash_2,
705343
                  a_word_hash_cmp);
705343
-      for (wp = wordhead; wp != 0; wp = wp->next)
705343
+      for (wp = words; wp < word_end; ++wp)
705343
         {
705343
           struct a_word *owp = hash_insert (&a_word_table, wp);
705343
           if (owp)
705343
@@ -1029,51 +1033,49 @@ func_filter_filterout (char *o, char **argv, const char *funcname)
705343
         }
705343
     }
705343
 
705343
-  if (words)
705343
+  /* Run each pattern through the words, killing words.  */
705343
+  for (pp = patterns; pp < pat_end; ++pp)
705343
     {
705343
-      int doneany = 0;
705343
-
705343
-      /* Run each pattern through the words, killing words.  */
705343
-      for (pp = pathead; pp != 0; pp = pp->next)
705343
+      if (pp->percent)
705343
+        for (wp = words; wp < word_end; ++wp)
705343
+          wp->matched |= pattern_matches (pp->str, pp->percent, wp->str);
705343
+      else if (hashing)
705343
         {
705343
-          if (pp->percent)
705343
-            for (wp = wordhead; wp != 0; wp = wp->next)
705343
-              wp->matched |= pattern_matches (pp->str, pp->percent, wp->str);
705343
-          else if (hashing)
705343
+          struct a_word a_word_key;
705343
+          a_word_key.str = pp->str;
705343
+          a_word_key.length = pp->length;
705343
+          wp = hash_find_item (&a_word_table, &a_word_key);
705343
+          while (wp)
705343
             {
705343
-              struct a_word a_word_key;
705343
-              a_word_key.str = pp->str;
705343
-              a_word_key.length = pp->length;
705343
-              wp = hash_find_item (&a_word_table, &a_word_key);
705343
-              while (wp)
705343
-                {
705343
-                  wp->matched |= 1;
705343
-                  wp = wp->chain;
705343
-                }
705343
+              wp->matched |= 1;
705343
+              wp = wp->chain;
705343
             }
705343
-          else
705343
-            for (wp = wordhead; wp != 0; wp = wp->next)
705343
-              wp->matched |= (wp->length == pp->length
705343
-                              && strneq (pp->str, wp->str, wp->length));
705343
         }
705343
+      else
705343
+        for (wp = words; wp < word_end; ++wp)
705343
+          wp->matched |= (wp->length == pp->length
705343
+                          && strneq (pp->str, wp->str, wp->length));
705343
+    }
705343
 
705343
-      /* Output the words that matched (or didn't, for filter-out).  */
705343
-      for (wp = wordhead; wp != 0; wp = wp->next)
705343
-        if (is_filter ? wp->matched : !wp->matched)
705343
-          {
705343
-            o = variable_buffer_output (o, wp->str, strlen (wp->str));
705343
-            o = variable_buffer_output (o, " ", 1);
705343
-            doneany = 1;
705343
-          }
705343
+  /* Output the words that matched (or didn't, for filter-out).  */
705343
+  for (wp = words; wp < word_end; ++wp)
705343
+    if (is_filter ? wp->matched : !wp->matched)
705343
+      {
705343
+        o = variable_buffer_output (o, wp->str, strlen (wp->str));
705343
+        o = variable_buffer_output (o, " ", 1);
705343
+        doneany = 1;
705343
+      }
705343
 
705343
-      if (doneany)
705343
-        /* Kill the last space.  */
705343
-        --o;
705343
-    }
705343
+  if (doneany)
705343
+    /* Kill the last space.  */
705343
+    --o;
705343
 
705343
   if (hashing)
705343
     hash_free (&a_word_table, 0);
705343
 
705343
+  free (patterns);
705343
+  free (words);
705343
+
705343
   return o;
705343
 }
705343
 
705343
diff --git a/tests/scripts/functions/filter-out b/tests/scripts/functions/filter-out
705343
index 1fe4819d..dec5343e 100644
705343
--- a/tests/scripts/functions/filter-out
705343
+++ b/tests/scripts/functions/filter-out
705343
@@ -27,6 +27,22 @@ all: ; @echo '$(files1) $(files2)'
705343
 !,
705343
               '', "foo.elc foo.elc\n");
705343
 
705343
+# Force use of hash (see function.c:func_filter_filterout for params)
705343
+
705343
+my $base = 'foo.1 foo.2 foo.3 foo.4 foo.5 foo.6 foo.7 foo.8 foo.9 foo.10';
705343
+
705343
+my $base10 = join(' ', ($base) x 10);
705343
+my $out3 = join(' ', ('foo.3') x 10);
705343
+my $out456 = join(' ', ('foo.4 foo.5 foo.6') x 10);
705343
+
705343
+run_make_test("words := $base10" . q!
705343
+files1 := $(filter %.3, $(words))
705343
+files2 := $(filter %.4 foo.5 foo.6, $(words))
705343
+all: ; @echo '$(files1) $(files2)'
705343
+!,
705343
+              '', "$out3 $out456\n");
705343
+
705343
+
705343
 # Escaped patterns
705343
 run_make_test(q!all:;@echo '$(filter foo\%bar,foo%bar fooXbar)'!,
705343
               '', "foo%bar\n");