summaryrefslogtreecommitdiff
path: root/sys-apps/sed/files/sed-4.3-dfa-segv-3.patch
blob: d85022f754f0edcb8fcccdc6d915ec41e22d028b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
fix from upstream gnulib (fudged to apply to sed-4.3)

From 7c345c68cdf62737ccc4a9d0ba2cd921fae850fa Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <noritnk@kcn.ne.jp>
Date: Mon, 9 Jan 2017 08:21:21 +0900
Subject: [PATCH] dfa: melt down dfastate into build_state

* src/dfa.c (dfastate): Remove it.
(build_state): Insert content of dfastate() to bottom.
---
 lib/dfa.c | 97 +++++++++++++++++++++++++++++----------------------------------
 1 file changed, 45 insertions(+), 52 deletions(-)

diff --git a/lib/dfa.c b/lib/dfa.c
index bda4602b1094..6896ed320a7b 100644
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -2609,8 +2609,10 @@ realloc_trans_if_necessary (struct dfa *d)
     }
 }
 
-/* Return the transition out of state s of d for the input character uc,
-   updating the slots in trans accordingly.
+/*
+   Calculate the transition table for a new state derived from state s
+   for a compiled dfa d after input character uc, and return the new
+   state number.
 
    Do not worry about all possible input characters; calculate just the group
    of positions that match uc.  Label it with the set of characters that
@@ -2639,8 +2641,9 @@ realloc_trans_if_necessary (struct dfa *d)
    If after comparing with every group there are characters remaining in C,
    create a new group labeled with the characters of C and insert this
    position in that group.  */
+
 static state_num
-dfastate (state_num s, struct dfa *d, unsigned char uc, state_num trans[])
+build_state (state_num s, struct dfa *d, unsigned char uc)
 {
   leaf_set group;               /* Positions that match the input char.  */
   charclass label;              /* The group's label.  */
@@ -2652,6 +2655,45 @@ dfastate (state_num s, struct dfa *d, unsigned char uc, state_num trans[])
   fprintf (stderr, "build state %td\n", s);
 #endif
 
+  /* A pointer to the new transition table, and the table itself.  */
+  state_num **ptrans = (ACCEPTING (s, *d) ? d->fails : d->trans) + s;
+  state_num *trans = *ptrans;
+
+  if (!trans)
+    {
+      /* MAX_TRCOUNT is an arbitrary upper limit on the number of
+         transition tables that can exist at once, other than for
+         initial states.  Often-used transition tables are quickly
+         rebuilt, whereas rarely-used ones are cleared away.  */
+      if (MAX_TRCOUNT <= d->trcount)
+        {
+          for (state_num i = d->min_trcount; i < d->tralloc; i++)
+            {
+              free (d->trans[i]);
+              free (d->fails[i]);
+              d->trans[i] = d->fails[i] = NULL;
+            }
+          d->trcount = 0;
+        }
+
+      d->trcount++;
+      *ptrans = trans = xmalloc (NOTCHAR * sizeof *trans);
+
+      /* Fill transition table with a default value which means that the
+         transited state has not been calculated yet.  */
+      for (int i = 0; i < NOTCHAR; i++)
+        trans[i] = -2;
+    }
+
+  /* Set up the success bits for this state.  */
+  d->success[s] = 0;
+  if (ACCEPTS_IN_CONTEXT (d->states[s].context, CTX_NEWLINE, s, *d))
+    d->success[s] |= CTX_NEWLINE;
+  if (ACCEPTS_IN_CONTEXT (d->states[s].context, CTX_LETTER, s, *d))
+    d->success[s] |= CTX_LETTER;
+  if (ACCEPTS_IN_CONTEXT (d->states[s].context, CTX_NONE, s, *d))
+    d->success[s] |= CTX_NONE;
+
   group.elems = xnmalloc (d->nleaves, sizeof *group.elems);
   group.nelem = 0;
 
@@ -2889,55 +2931,6 @@ dfastate (state_num s, struct dfa *d, unsigned char uc, state_num trans[])
   return trans[uc];
 }
 
-/* Calculate the transition table for a new state derived from state s
-   for a compiled dfa d after input character uc, and return the new
-   state number.  */
-
-static state_num
-build_state (state_num s, struct dfa *d, unsigned char uc)
-{
-  /* A pointer to the new transition table, and the table itself.  */
-  state_num **ptrans = (ACCEPTING (s, *d) ? d->fails : d->trans) + s;
-  state_num *trans = *ptrans;
-
-  if (!trans)
-    {
-      /* MAX_TRCOUNT is an arbitrary upper limit on the number of
-         transition tables that can exist at once, other than for
-         initial states.  Often-used transition tables are quickly
-         rebuilt, whereas rarely-used ones are cleared away.  */
-      if (MAX_TRCOUNT <= d->trcount)
-        {
-          for (state_num i = d->min_trcount; i < d->tralloc; i++)
-            {
-              free (d->trans[i]);
-              free (d->fails[i]);
-              d->trans[i] = d->fails[i] = NULL;
-            }
-          d->trcount = 0;
-        }
-
-      d->trcount++;
-      *ptrans = trans = xmalloc (NOTCHAR * sizeof *trans);
-
-      /* Fill transition table with a default value which means that the
-         transited state has not been calculated yet.  */
-      for (int i = 0; i < NOTCHAR; i++)
-        trans[i] = -2;
-    }
-
-  /* Set up the success bits for this state.  */
-  d->success[s] = 0;
-  if (ACCEPTS_IN_CONTEXT (d->states[s].context, CTX_NEWLINE, s, *d))
-    d->success[s] |= CTX_NEWLINE;
-  if (ACCEPTS_IN_CONTEXT (d->states[s].context, CTX_LETTER, s, *d))
-    d->success[s] |= CTX_LETTER;
-  if (ACCEPTS_IN_CONTEXT (d->states[s].context, CTX_NONE, s, *d))
-    d->success[s] |= CTX_NONE;
-
-  return dfastate (s, d, uc, trans);
-}
-
 /* Multibyte character handling sub-routines for dfaexec.  */
 
 /* Consume a single byte and transit state from 's' to '*next_state'.
-- 
2.11.0