From b55a27fdfbdbc787a4bf63df84daf545ae4d1db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 15 Jan 2023 05:22:22 +0100 Subject: [PATCH] Optimize regex expressions --- .../beatrice/hidekobot/listeners/MessageCommandListener.java | 2 +- src/main/java/wtf/beatrice/hidekobot/util/FormatUtil.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageCommandListener.java index f1baf84..4265141 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageCommandListener.java @@ -67,7 +67,7 @@ public class MessageCommandListener extends ListenerAdapter String eventMessage = event.getMessage().getContentRaw(); // check if the sent message matches the bot activation regex (prefix, name, ...) - if(!eventMessage.toLowerCase().matches(commandRegex + "((.|\\n)*)")) + if(!eventMessage.toLowerCase().matches("(?s)" + commandRegex + ".*")) return; // generate args from the string diff --git a/src/main/java/wtf/beatrice/hidekobot/util/FormatUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/FormatUtil.java index 098efc7..b92b772 100644 --- a/src/main/java/wtf/beatrice/hidekobot/util/FormatUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/util/FormatUtil.java @@ -106,8 +106,11 @@ public class FormatUtil eg: 3d, 33hours, 32048dojg, 3d2h5m22s. it does not match if the [digits and characters] blocks are missing. eg: 33, asd, 3g5hj, 4 asd. + + {1,10} is used to limit the size of the input to parse, to avoid stack overflows. + no one should be typing more than 10 arguments, or more than 10 digits for a single argument anyway. */ - if(!duration.matches("(\\d+[a-zA-Z]+)+")) + if(!duration.matches("(\\d{1,10}[a-zA-Z]{1,10}){1,10}")) return null; String[] durationTimes = duration.split("[a-zA-Z]+");