@@ -129,6 +129,7 @@ const char *_compilecode(const char *re, ByteProg *prog)
129129
130130 prog -> bytelen = pc ;
131131 re = _compilecode (re + 1 , prog );
132+ if (re == NULL || * re != ')' ) return NULL ; // error, or no matching paren
132133 pc = prog -> bytelen ;
133134
134135 EMIT (pc ++ , Save );
@@ -138,12 +139,14 @@ const char *_compilecode(const char *re, ByteProg *prog)
138139 break ;
139140 }
140141 case '?' :
142+ if (pc == term ) return NULL ; // nothing to repeat
141143 insert_code (code , term , 2 , & pc );
142144 EMIT (term , Split );
143145 EMIT (term + 1 , REL (term , pc ));
144146 prog -> len ++ ;
145147 break ;
146148 case '*' :
149+ if (pc == term ) return NULL ; // nothing to repeat
147150 insert_code (code , term , 2 , & pc );
148151 EMIT (pc , Jmp );
149152 EMIT (pc + 1 , REL (pc , term ));
@@ -158,6 +161,7 @@ const char *_compilecode(const char *re, ByteProg *prog)
158161 prog -> len += 2 ;
159162 break ;
160163 case '+' :
164+ if (pc == term ) return NULL ; // nothing to repeat
161165 if (re [1 ] == '?' ) {
162166 EMIT (pc , Split );
163167 re ++ ;
@@ -217,7 +221,8 @@ int re1_5_compilecode(ByteProg *prog, const char *re)
217221 prog -> insts [prog -> bytelen ++ ] = 0 ;
218222 prog -> len ++ ;
219223
220- _compilecode (re , prog );
224+ re = _compilecode (re , prog );
225+ if (re == NULL || * re ) return 1 ;
221226
222227 prog -> insts [prog -> bytelen ++ ] = Save ;
223228 prog -> insts [prog -> bytelen ++ ] = 1 ;
0 commit comments