From 30ef28ce49c5b845b37d3e21fcfc938c10b0d3b7 Mon Sep 17 00:00:00 2001 From: Paillat Date: Tue, 15 Aug 2023 18:49:51 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(functionscalls.py):=20add=20?= =?UTF-8?q?timeout=20parameter=20to=20evaluate=5Fmath=20function=20to=20li?= =?UTF-8?q?mit=20execution=20time=20and=20prevent=20potential=20abuse=20or?= =?UTF-8?q?=20infinite=20loops?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functionscalls.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/functionscalls.py b/src/functionscalls.py index bb25c53..aefa360 100644 --- a/src/functionscalls.py +++ b/src/functionscalls.py @@ -316,13 +316,14 @@ async def send_ascii_art_image( async def evaluate_math( - message_in_channel_in_wich_to_send: discord.Message, arguments: dict + message_in_channel_in_wich_to_send: discord.Message, arguments: dict, timeout=10 ): evaluable = arguments.get("string", "") if evaluable == "": raise FuntionCallError("No string provided") + loop = asyncio.get_event_loop() try: - result = simple_eval(evaluable) + result = await asyncio.wait_for(loop.run_in_executor(None, simple_eval, evaluable), timeout=timeout) except Exception as e: result = f"Error: {e}" return f"Result to math eval of {evaluable}: ```\n{str(result)}```"