30 lines
1.3 KiB
Diff
30 lines
1.3 KiB
Diff
# 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
|