From 03fce4775e5a1cf2fa8859a5074ea5bd37e1828a Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Tue, 9 Nov 2021 10:43:58 +0100 Subject: kerneldevice,generic: input pattern to string match is not regex The input pattern given to the string_match() method is not a regex pattern and we cannot use it as that, because all the special characters (e.g. '.') would not be treated correctly. Also, the prefix matching with the wildcard at the end of the string needs to be converted to a proper regex wildcard, i.e. '.*'. This logic also adds support for suffix matching, with a wildcard at the beginning of the string, e.g. as the ones used for the wwan kernel device name matching rules (i.e. '*MBIM'). Unit tests are added to cover all cases, most of these tests were failing without the fixes implemented here. --- src/kerneldevice/mm-kernel-device-generic.c | 27 --------------------------- 1 file changed, 27 deletions(-) (limited to 'src/kerneldevice/mm-kernel-device-generic.c') diff --git a/src/kerneldevice/mm-kernel-device-generic.c b/src/kerneldevice/mm-kernel-device-generic.c index a8f34fed..e0ee7720 100644 --- a/src/kerneldevice/mm-kernel-device-generic.c +++ b/src/kerneldevice/mm-kernel-device-generic.c @@ -739,33 +739,6 @@ kernel_device_cmp (MMKernelDevice *a, /*****************************************************************************/ -static gboolean -string_match (MMKernelDeviceGeneric *self, - const gchar *str, - const gchar *pattern) -{ - g_autoptr(GError) inner_error = NULL; - g_autoptr(GRegex) regex = NULL; - g_autoptr(GMatchInfo) match_info = NULL; - - regex = g_regex_new (pattern, 0, 0, &inner_error); - if (!regex) { - mm_obj_warn (self, "invalid pattern in rule '%s': %s", pattern, inner_error->message); - return FALSE; - } - g_regex_match_full (regex, str, -1, 0, 0, &match_info, &inner_error); - if (inner_error) { - mm_obj_warn (self, "couldn't apply pattern match in rule '%s': %s", pattern, inner_error->message); - return FALSE; - } - - if (!g_match_info_matches (match_info)) - return FALSE; - - mm_obj_dbg (self, "pattern '%s' matched: '%s'", pattern, str); - return TRUE; -} - static gboolean check_condition (MMKernelDeviceGeneric *self, MMUdevRuleMatch *match) -- cgit v1.2.3-70-g09d2