8
8
((code ? memmove(code + at + num, code + at, pc - at) : 0), pc += num)
9
9
#define REL (at , to ) (to - at - 2)
10
10
#define EMIT (at , byte ) (code ? (code[at] = byte) : (at))
11
+ #define EMIT_CHECKED (at , byte ) (_emit_checked(at, code, byte, &err))
11
12
#define PC (prog->bytelen)
12
13
14
+ static void _emit_checked (int at , char * code , int val , bool * err ) {
15
+ * err |= val != (int8_t )val ;
16
+ if (code ) {
17
+ code [at ] = val ;
18
+ }
19
+ }
20
+
13
21
static const char * _compilecode (const char * re , ByteProg * prog , int sizecode )
14
22
{
15
23
char * code = sizecode ? NULL : prog -> insts ;
24
+ bool err = false;
16
25
int start = PC ;
17
26
int term = PC ;
18
27
int alt_label = 0 ;
@@ -64,7 +73,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode)
64
73
}
65
74
EMIT (PC ++ , * re );
66
75
}
67
- EMIT (term + 1 , cnt );
76
+ EMIT_CHECKED (term + 1 , cnt );
68
77
break ;
69
78
}
70
79
case '(' : {
@@ -75,7 +84,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode)
75
84
if (capture ) {
76
85
sub = ++ prog -> sub ;
77
86
EMIT (PC ++ , Save );
78
- EMIT (PC ++ , 2 * sub );
87
+ EMIT_CHECKED (PC ++ , 2 * sub );
79
88
prog -> len ++ ;
80
89
} else {
81
90
re += 2 ;
@@ -86,7 +95,7 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode)
86
95
87
96
if (capture ) {
88
97
EMIT (PC ++ , Save );
89
- EMIT (PC ++ , 2 * sub + 1 );
98
+ EMIT_CHECKED (PC ++ , 2 * sub + 1 );
90
99
prog -> len ++ ;
91
100
}
92
101
@@ -101,23 +110,23 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode)
101
110
} else {
102
111
EMIT (term , Split );
103
112
}
104
- EMIT (term + 1 , REL (term , PC ));
113
+ EMIT_CHECKED (term + 1 , REL (term , PC ));
105
114
prog -> len ++ ;
106
115
term = PC ;
107
116
break ;
108
117
case '*' :
109
118
if (PC == term ) return NULL ; // nothing to repeat
110
119
INSERT_CODE (term , 2 , PC );
111
120
EMIT (PC , Jmp );
112
- EMIT (PC + 1 , REL (PC , term ));
121
+ EMIT_CHECKED (PC + 1 , REL (PC , term ));
113
122
PC += 2 ;
114
123
if (re [1 ] == '?' ) {
115
124
EMIT (term , RSplit );
116
125
re ++ ;
117
126
} else {
118
127
EMIT (term , Split );
119
128
}
120
- EMIT (term + 1 , REL (term , PC ));
129
+ EMIT_CHECKED (term + 1 , REL (term , PC ));
121
130
prog -> len += 2 ;
122
131
term = PC ;
123
132
break ;
@@ -129,20 +138,20 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode)
129
138
} else {
130
139
EMIT (PC , RSplit );
131
140
}
132
- EMIT (PC + 1 , REL (PC , term ));
141
+ EMIT_CHECKED (PC + 1 , REL (PC , term ));
133
142
PC += 2 ;
134
143
prog -> len ++ ;
135
144
term = PC ;
136
145
break ;
137
146
case '|' :
138
147
if (alt_label ) {
139
- EMIT (alt_label , REL (alt_label , PC ) + 1 );
148
+ EMIT_CHECKED (alt_label , REL (alt_label , PC ) + 1 );
140
149
}
141
150
INSERT_CODE (start , 2 , PC );
142
151
EMIT (PC ++ , Jmp );
143
152
alt_label = PC ++ ;
144
153
EMIT (start , Split );
145
- EMIT (start + 1 , REL (start , PC ));
154
+ EMIT_CHECKED (start + 1 , REL (start , PC ));
146
155
prog -> len += 2 ;
147
156
term = PC ;
148
157
break ;
@@ -160,9 +169,9 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode)
160
169
}
161
170
162
171
if (alt_label ) {
163
- EMIT (alt_label , REL (alt_label , PC ) + 1 );
172
+ EMIT_CHECKED (alt_label , REL (alt_label , PC ) + 1 );
164
173
}
165
- return re ;
174
+ return err ? NULL : re ;
166
175
}
167
176
168
177
int re1_5_sizecode (const char * re )
0 commit comments