annotate libgfortran/runtime/select_inc.c @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Implement the SELECT statement for character variables.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2008-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of the GNU Fortran runtime library (libgfortran).
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 Libgfortran is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
8 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
9 any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 Libgfortran is distributed in the hope that it will be useful,
kono
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
14 GNU General Public License for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 #define select_string SUFFIX(select_string)
kono
parents:
diff changeset
26 #define select_struct SUFFIX(select_struct)
kono
parents:
diff changeset
27 #define compare_string SUFFIX(compare_string)
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 typedef struct
kono
parents:
diff changeset
30 {
kono
parents:
diff changeset
31 CHARTYPE *low;
kono
parents:
diff changeset
32 gfc_charlen_type low_len;
kono
parents:
diff changeset
33 CHARTYPE *high;
kono
parents:
diff changeset
34 gfc_charlen_type high_len;
kono
parents:
diff changeset
35 int address;
kono
parents:
diff changeset
36 }
kono
parents:
diff changeset
37 select_struct;
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 extern int select_string (select_struct *table, int table_len,
kono
parents:
diff changeset
40 const CHARTYPE *selector,
kono
parents:
diff changeset
41 gfc_charlen_type selector_len);
kono
parents:
diff changeset
42 export_proto(select_string);
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 /* select_string()-- Given a selector string and a table of
kono
parents:
diff changeset
46 * select_struct structures, return the address to jump to. */
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 int
kono
parents:
diff changeset
49 select_string (select_struct *table, int table_len, const CHARTYPE *selector,
kono
parents:
diff changeset
50 gfc_charlen_type selector_len)
kono
parents:
diff changeset
51 {
kono
parents:
diff changeset
52 select_struct *t;
kono
parents:
diff changeset
53 int i, low, high, mid;
kono
parents:
diff changeset
54 int default_jump = -1;
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 if (table_len == 0)
kono
parents:
diff changeset
57 return -1;
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 /* Record the default address if present */
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 if (table->low == NULL && table->high == NULL)
kono
parents:
diff changeset
62 {
kono
parents:
diff changeset
63 default_jump = table->address;
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 table++;
kono
parents:
diff changeset
66 table_len--;
kono
parents:
diff changeset
67 if (table_len == 0)
kono
parents:
diff changeset
68 return default_jump;
kono
parents:
diff changeset
69 }
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 /* Try the high and low bounds if present. */
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 if (table->low == NULL)
kono
parents:
diff changeset
74 {
kono
parents:
diff changeset
75 if (compare_string (table->high_len, table->high,
kono
parents:
diff changeset
76 selector_len, selector) >= 0)
kono
parents:
diff changeset
77 return table->address;
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 table++;
kono
parents:
diff changeset
80 table_len--;
kono
parents:
diff changeset
81 if (table_len == 0)
kono
parents:
diff changeset
82 return default_jump;
kono
parents:
diff changeset
83 }
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 t = table + table_len - 1;
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 if (t->high == NULL)
kono
parents:
diff changeset
88 {
kono
parents:
diff changeset
89 if (compare_string (t->low_len, t->low, selector_len, selector) <= 0)
kono
parents:
diff changeset
90 return t->address;
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 table_len--;
kono
parents:
diff changeset
93 if (table_len == 0)
kono
parents:
diff changeset
94 return default_jump;
kono
parents:
diff changeset
95 }
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 /* At this point, the only table entries are bounded entries. Find
kono
parents:
diff changeset
98 the right entry with a binary chop. */
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 low = -1;
kono
parents:
diff changeset
101 high = table_len;
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 while (low + 1 < high)
kono
parents:
diff changeset
104 {
kono
parents:
diff changeset
105 mid = (low + high) / 2;
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 t = table + mid;
kono
parents:
diff changeset
108 i = compare_string (t->low_len, t->low, selector_len, selector);
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 if (i == 0)
kono
parents:
diff changeset
111 return t->address;
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 if (i < 0)
kono
parents:
diff changeset
114 low = mid;
kono
parents:
diff changeset
115 else
kono
parents:
diff changeset
116 high = mid;
kono
parents:
diff changeset
117 }
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 /* The string now lies between the low indeces of the now-adjacent
kono
parents:
diff changeset
120 high and low entries. Because it is less than the low entry of
kono
parents:
diff changeset
121 'high', it can't be that one. If low is still -1, then no
kono
parents:
diff changeset
122 entries match. Otherwise, we have to check the high entry of
kono
parents:
diff changeset
123 'low'. */
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 if (low == -1)
kono
parents:
diff changeset
126 return default_jump;
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 t = table + low;
kono
parents:
diff changeset
129 if (compare_string (selector_len, selector, t->high_len, t->high) <= 0)
kono
parents:
diff changeset
130 return t->address;
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 return default_jump;
kono
parents:
diff changeset
133 }