Skip to content

Commit

Permalink
Globbing.py: Add time limit for glob expansion
Browse files Browse the repository at this point in the history
Added a decorator for glob evaluating functions that sets up an
upper time limit(60 secs for now) for the functions to evaluate the glob
or else Error with an exception

Fixes : coala#5967
  • Loading branch information
PrasanthChettri committed Jun 7, 2021
1 parent 52b9d18 commit 35f799c
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion coalib/parsing/Globbing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
import os
import platform
import re
import signal
from functools import lru_cache
from contextlib import contextmanager

from coala_utils.decorators import yield_once
from coalib.misc.Constants import GLOBBING_SPECIAL_CHARS


GLOB_EXPAND_TIME_LIMIT = 60


class GlobTimeoutException(Exception):
"""
Glob Timeout Exception
"""


@contextmanager
def time_limit():
def signal_handler(signum, frame):
raise GlobTimeoutException(
'glob expansion taking too much time')

signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(GLOB_EXPAND_TIME_LIMIT)

try:
yield
finally:
signal.alarm(0)


def glob_time_limit(glob_func):
def glob_time_limit_wrapper(*args, **kwargs):
try:
with time_limit():
return glob_func(*args, **kwargs)
except GlobTimeoutException as e:
raise e
return glob_time_limit_wrapper


def _end_of_set_index(string, start_index):
"""
Returns the position of the appropriate closing bracket for a glob set in
Expand Down Expand Up @@ -106,6 +142,7 @@ def _boundary_of_alternatives_indices(pattern):
return start_pos, end_pos


@glob_time_limit
@yield_once
def _iter_choices(pattern):
"""
Expand All @@ -125,6 +162,7 @@ def _iter_choices(pattern):
start_pos = end_pos + 1


@glob_time_limit
@yield_once
def _iter_alternatives(pattern):
"""
Expand Down Expand Up @@ -256,6 +294,7 @@ def _absolute_flat_glob(pattern):
return


@glob_time_limit
def _iter_relative_dirs(dirname):
"""
Recursively iterates subdirectories of all levels from dirname
Expand Down Expand Up @@ -316,6 +355,7 @@ def relative_flat_glob(dirname, basename):
return []


@glob_time_limit
def relative_recursive_glob(dirname, pattern):
"""
Recursive Glob for one directory and all its (nested) subdirectories.
Expand Down Expand Up @@ -347,6 +387,7 @@ def has_wildcard(pattern):
return match is not None


@glob_time_limit
def _iglob(pattern):
dirname, basename = os.path.split(pattern)
if not has_wildcard(pattern):
Expand Down Expand Up @@ -378,7 +419,6 @@ def _iglob(pattern):
yield os.path.join(dirname, name)


@yield_once
def iglob(pattern):
"""
Iterates all filesystem paths that get matched by the glob pattern.
Expand Down

0 comments on commit 35f799c

Please sign in to comment.