8000 Using count() instead of finditer() for max_num_fields check · python/cpython@1fa59e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fa59e4

Browse files
Using count() instead of finditer() for max_num_fields check
1 parent 60aee01 commit 1fa59e4

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

Lib/urllib/parse.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,6 @@ def parse_qs(qs, keep_blank_values=False, strict_parsing=False,
666666
return parsed_result
667667

668668

669-
# Used for checking parse_qsl() with max_num_fields. Both & and ; are valid query
670-
# string delimiters.
671-
_QS_DELIMITER_RE = re.compile(r'[&;]')
672-
673-
674669
def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
675670
encoding='utf-8', errors='replace', max_num_fields=None):
676671
"""Parse a query given as a string argument.
@@ -703,9 +698,9 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
703698
# is less than max_num_fields. This prevents a memory exhaustion DOS
704699
# attack via post bodies with many fields.
705700
if max_num_fields is not None:
706-
for num_fields, _ in enumerate(_QS_DELIMITER_RE.finditer(qs), 2):
707-
if max_num_fields < num_fields:
708-
raise ValueError('Max number of fields exceeded')
701+
num_fields = 1 + qs.count('&') + qs.count(';')
702+
if max_num_fields < num_fields:
703+
raise ValueError('Max number of fields exceeded')
709704

710705
pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
711706
r = []

0 commit comments

Comments
 (0)
0