Skip to content

Commit dac35b7

Browse files
committed
Fixed handling of out-of-bounds starting position in regex match/search
1 parent b8d3e5f commit dac35b7

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

graalpython/lib-graalpython/_sre.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ def __check_input_type(self, input):
271271
if self.__binary and isinstance(input, str):
272272
raise TypeError("cannot use a bytes pattern on a string-like object")
273273

274+
@staticmethod
275+
def __check_pos(pos):
276+
if pos > maxsize():
277+
raise OverflowError('Python int too large to convert to Java int')
274278

275279
def __tregex_compile(self, pattern, flags=None):
276280
if flags is None:
@@ -313,7 +317,7 @@ def _search(self, pattern, string, pos, endpos, sticky=False):
313317
input_str = string
314318
if endpos == -1 or endpos >= len(string):
315319
endpos = len(string)
316-
result = tregex_call_exec(pattern.exec, input_str, min(pos, len(string) + 1))
320+
result = tregex_call_exec(pattern.exec, input_str, min(pos, endpos))
317321
else:
318322
input_str = string[:endpos]
319323
result = tregex_call_exec(pattern.exec, input_str, min(pos, endpos % len(string) + 1))
@@ -323,14 +327,17 @@ def _search(self, pattern, string, pos, endpos, sticky=False):
323327
return None
324328

325329
def search(self, string, pos=0, endpos=None):
330+
self.__check_pos(pos)
326331
self.__check_input_type(string)
327332
return self._search(self.pattern, string, pos, default(endpos, -1))
328333

329334
def match(self, string, pos=0, endpos=None):
335+
self.__check_pos(pos)
330336
self.__check_input_type(string)
331337
return self._search(self.pattern, string, pos, default(endpos, -1), sticky=True)
332338

333339
def fullmatch(self, string, pos=0, endpos=None):
340+
self.__check_pos(pos)
334341
self.__check_input_type(string)
335342
return self._search(_append_end_assert(self.pattern), string, pos, default(endpos, -1), sticky=True)
336343

0 commit comments

Comments
 (0)