java-topology/defects/rails/patch/rails-0007-lazy-load-hooks-set.patch

42 lines
1.9 KiB
Diff

Fixes rails-0007/0008: ActiveSupport lazy_load_hooks + ActiveRecord enum — Array#include? in boot-time loops.
--- a/activesupport/lib/active_support/lazy_load_hooks.rb
+++ b/activesupport/lib/active_support/lazy_load_hooks.rb
@@ DEFECT rails-0007: line 48, 84
- @run_once = Hash.new { |h, k| h[k] = [] } # Array — O(R) include?
+ @run_once = Hash.new { |h, k| h[k] = Set.new } # FIX: Set — O(1) include?
def with_execution_control(name, block, once)
- unless @run_once[name].include?(block) # O(R) Array scan — CWE-407
+ unless @run_once[name].include?(block) # O(1) Set lookup — fixed
@run_once[name] << block if once # Set#<< is O(1)
yield
end
end
# require 'set' already present in activesupport/lib/active_support/lazy_load_hooks.rb
--- a/activerecord/lib/active_record/enum.rb
+++ b/activerecord/lib/active_record/enum.rb
@@ DEFECT rails-0008: lines 262-279
- value_method_names = [] # Array — O(E) include? in loop
+ value_method_names = Set.new # FIX: Set — O(1) include?
pairs.each do |label, value|
# ...
value_method_names << value_method_name # O(1) for both Array and Set
if value_method_alias != value_method_name && !value_method_names.include?(value_method_alias)
# Array: O(E) scan per iteration → O(E²)
# Set: O(1) per iteration → O(E)
value_method_names << value_method_alias
end
end
- detect_negative_enum_conditions!(value_method_names) if scopes # receives Array
+ detect_negative_enum_conditions!(value_method_names.to_a) if scopes # method expects respond_to?(:include?)
# detect_negative_enum_conditions! also calls method_names.include? in O(E) loop → O(E²)
# Fixed by passing Set which has O(1) include? — or convert method_names to Set internally