Skip to content

Commit f21225a

Browse files
committed
Use str.maketrans to ensure that only hex characters are given to UUID init
1 parent 412da70 commit f21225a

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

Lib/uuid.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"""
5858

5959
import os
60-
import re
6160
import sys
6261
import time
6362

@@ -84,6 +83,7 @@
8483
_MAC_DELIM = b'.'
8584
_MAC_OMITS_LEADING_ZEROES = True
8685

86+
_HEX_TT = str.maketrans('', '', 'abcdefABCDEF0123456789')
8787
RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
8888
'reserved for NCS compatibility', 'specified in RFC 4122',
8989
'reserved for Microsoft compatibility', 'reserved for future definition']
@@ -216,7 +216,8 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
216216
elif hex is not None:
217217
hex = hex.replace('urn:', '').replace('uuid:', '')
218218
hex = hex.strip('{}').replace('-', '')
219-
if not re.fullmatch(r'[0-9A-Fa-f]{32}', hex):
219+
# ensure that only 32 hex characters pass through
220+
if len(hex) != 32 or hex.translate(_HEX_TT):
220221
raise ValueError('badly formed hexadecimal UUID string')
221222
int = int_(hex, 16)
222223
elif bytes_le is not None:

0 commit comments

Comments
 (0)