Skip to content

Commit

Permalink
pythongh-125651: Use regex to avoid any unallowed character to passed…
Browse files Browse the repository at this point in the history
… to int
  • Loading branch information
sevdog committed Oct 18, 2024
1 parent 6c7a1c5 commit cde9e36
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions Lib/test/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,17 @@ def test_exceptions(self):
# Badly formed hex strings.
badvalue(lambda: self.uuid.UUID(''))
badvalue(lambda: self.uuid.UUID('abc'))
badvalue(lambda: self.uuid.UUID("123_4567812345678123456781234567"))
badvalue(lambda: self.uuid.UUID("123_4567812345678123456781_23456"))
badvalue(lambda: self.uuid.UUID('123_4567812345678123456781234567'))
badvalue(lambda: self.uuid.UUID('123_4567812345678123456781_23456'))
badvalue(lambda: self.uuid.UUID('123_4567812345678123456781_23456'))
badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567'))
badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789'))
badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678'))
badvalue(lambda: self.uuid.UUID('0x123456781234567812345678z23456'))
badvalue(lambda: self.uuid.UUID('0X123456781234567812345678z23456'))
badvalue(lambda: self.uuid.UUID('+123456781234567812345678z234567'))
badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z23456 '))
badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z2345 '))

# Badly formed bytes.
badvalue(lambda: self.uuid.UUID(bytes='abc'))
Expand Down
5 changes: 3 additions & 2 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"""

import os
import re
import sys

from enum import Enum, _simple_enum
Expand Down Expand Up @@ -176,8 +177,8 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
'or int arguments must be given')
if hex is not None:
hex = hex.replace('urn:', '').replace('uuid:', '')
hex = hex.strip("{}").replace("-", "").replace("_", "")
if len(hex) != 32:
hex = hex.strip('{}').replace('-', '')
if not re.fullmatch(r'[0-9A-Fa-f]{32}', hex):
raise ValueError('badly formed hexadecimal UUID string')
int = int_(hex, 16)
if bytes_le is not None:
Expand Down

0 comments on commit cde9e36

Please sign in to comment.