From 31e1e6b30dc15901c8208780ebc2565f97d81bdb Mon Sep 17 00:00:00 2001 From: Ivan Levanov Date: Fri, 13 Nov 2026 17:03:33 +0300 Subject: [PATCH] Avoid quadratic backtracking in numeric prefix validation [1-9]+ and \d* overlap in the numeric-prefix branch of IPAddr#mask!, so an invalid prefix such as "1" * 32000 + "x" can be split between them in O(n) ways and the engine backtracks through all of them before giving up. Both IPAddr.new and IPAddr#prefix= go through mask!. [1-9]\d* accepts the same strings without giving the engine a choice of where to split. --- lib/ipaddr.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index c68ccea..2bfc2ea 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -693,7 +693,7 @@ def mask!(mask) case mask when String case mask - when /\A(0|[1-9]+\d*)\z/ + when /\A(0|[1-9]\d*)\z/ prefixlen = mask.to_i when /\A\d+\z/ raise InvalidPrefixError, "leading zeros in prefix"