From 8fb00704aad835b826d51b3d25fe8f395e4ade79 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 21 Sep 2010 20:24:28 +0200
Subject: [PATCH] Fix mac checker regex

Currently, the mac checker regex could match a corner case of
11:22:33:44:55:66: (one extra colon at the end). We fix this, and we
also move the regex compilation outside of this function, at module
level.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/utils.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/utils.py b/lib/utils.py
index 887a74e4b..e527d00f6 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -92,6 +92,9 @@ _STRUCT_UCRED_SIZE = struct.calcsize(_STRUCT_UCRED)
 _MCL_CURRENT = 1
 _MCL_FUTURE = 2
 
+#: MAC checker regexp
+_MAC_CHECK = re.compile("^([0-9a-f]{2}:){5}[0-9a-f]{2}$", re.I)
+
 
 class RunResult(object):
   """Holds the result of running external programs.
@@ -1697,8 +1700,7 @@ def NormalizeAndValidateMac(mac):
   @raise errors.OpPrereqError: If the MAC isn't valid
 
   """
-  mac_check = re.compile("^([0-9a-f]{2}(:|$)){6}$", re.I)
-  if not mac_check.match(mac):
+  if not _MAC_CHECK.match(mac):
     raise errors.OpPrereqError("Invalid MAC address specified: %s" %
                                mac, errors.ECODE_INVAL)
 
-- 
GitLab