Skip to content

Commit 628fef8

Browse files
committed
Use valid_string_length() for early oversized-input check
1 parent 1e26c7f commit 628fef8

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

idna/core.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -547,12 +547,9 @@ def encode(
547547
if uts46:
548548
s = uts46_remap(s, std3_rules, transitional)
549549

550-
# Reject inputs that exceed the maximum DNS domain length up-front.
551-
# Each codepoint in a U-label contributes at least one octet to its
552-
# A-label form, so any input longer than the domain limit cannot
553-
# produce a valid A-domain. Short-circuiting here prevents per-label
554-
# validation from being driven into quadratic time
555-
if len(s) > 254:
550+
# Reject inputs that exceed the maximum DNS domain length up-front
551+
# to avoid expensive computation on long inputs.
552+
if not valid_string_length(s, trailing_dot=True):
556553
raise IDNAError("Domain too long")
557554

558555
trailing_dot = False
@@ -610,10 +607,9 @@ def decode(
610607
raise IDNAError("Invalid ASCII in A-label")
611608
if uts46:
612609
s = uts46_remap(s, std3_rules, False)
613-
# See encode() for rationale; the same bound applies because every
614-
# legal A-domain is at most 254 octets and every codepoint of a
615-
# legal U-domain contributes at least one octet to its A-form.
616-
if len(s) > 254:
610+
# Reject inputs that exceed the maximum DNS domain length up-front
611+
# to avoid expensive computation on long inputs.
612+
if not valid_string_length(s, trailing_dot=True):
617613
raise IDNAError("Domain too long")
618614
trailing_dot = False
619615
result = []

0 commit comments

Comments
 (0)