java-topology/defects/rails/patch/rails-0003-enumerable-excluding-set.patch

30 lines
1.3 KiB
Diff
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# UNDF: UNDF-2026-000000243
Fixes rails-0003/0004: ActiveSupport Enumerable#excluding and #in_order_of — Array membership in O(N) loops.
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ DEFECT rails-0003: Enumerable#excluding line 132-135
def excluding(*elements)
elements.flatten!(1)
- reject { |element| elements.include?(element) } # O(N×E) — CWE-407
+ exclusions = elements.to_set # FIX: O(E) once
+ reject { |element| exclusions.include?(element) } # O(N) — fixed
end
alias :without :excluding
@@ DEFECT rails-0004: Enumerable#in_order_of line 197-203
def in_order_of(key, series, filter: true)
if filter
group_by(&key).values_at(*series).flatten(1).compact
else
- sort_by { |v| series.index(v.public_send(key)) || series.size }.compact # O(N log N × S) — CWE-407
+ series_map = series.each_with_index.to_h # FIX: O(S) once
+ sort_by { |v| series_map.fetch(v.public_send(key), series.size) }.compact # O(N log N) — fixed
end
end
# rails-0003: Array#include? inside reject — O(N×E) → O(N+E) with Set
# rails-0004: Array#index inside sort_by block — O(N log N × S) → O(N log N + S) with Hash