Skip to content

Commit 61d6728

Browse files
committed
Fix integer out of range
1 parent fc61d69 commit 61d6728

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

lib/ajax-datatables-rails/datatable/column/search.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module Datatable
55
class Column
66
module Search
77

8+
SMALLEST_PQ_INTEGER = -2147483648
9+
LARGEST_PQ_INTEGER = 2147483647
10+
811
def searchable?
912
@view_column.fetch(:searchable, true)
1013
end
@@ -54,7 +57,7 @@ def non_regex_search
5457
when Proc
5558
filter
5659
when :eq, :not_eq, :lt, :gt, :lteq, :gteq, :in
57-
numeric_search
60+
is_searchable_integer? ? numeric_search : empty_search
5861
when :null_value
5962
null_value_search
6063
when :start_with
@@ -82,6 +85,27 @@ def numeric_search
8285
end
8386
end
8487

88+
def empty_search
89+
casted_column.matches('')
90+
end
91+
92+
def is_searchable_integer?
93+
if search.value.is_a?(Array)
94+
valids = search.value.map { |v| is_integer?(v) && !is_out_of_range?(v) }
95+
!valids.include?(false)
96+
else
97+
is_integer?(search.value) && !is_out_of_range?(search.value)
98+
end
99+
end
100+
101+
def is_out_of_range?(search_value)
102+
Integer(search_value) > LARGEST_PQ_INTEGER || Integer(search_value) < SMALLEST_PQ_INTEGER
103+
end
104+
105+
def is_integer?(string)
106+
true if Integer(string) rescue false
107+
end
108+
85109
end
86110
end
87111
end

spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,5 +494,32 @@
494494
expect(item[:first_name]).to eq 'john'
495495
end
496496
end
497+
498+
describe 'Integer overflows' do
499+
let(:datatable) { DatatableCondEq.new(view) }
500+
let(:largest_postgresql_integer_value) { 2147483647 }
501+
let(:smallest_postgresql_integer_value) { -2147483648 }
502+
503+
before(:each) do
504+
create(:user, first_name: 'john', post_id: 1)
505+
create(:user, first_name: 'mary', post_id: 2)
506+
create(:user, first_name: 'phil', post_id: largest_postgresql_integer_value)
507+
end
508+
509+
it 'Returns an empty result if input value is too large' do
510+
datatable.params[:columns]['4'][:search][:value] = largest_postgresql_integer_value + 1
511+
expect(datatable.data.size).to eq 0
512+
end
513+
514+
it 'Returns an empty result if input value is too small' do
515+
datatable.params[:columns]['4'][:search][:value] = smallest_postgresql_integer_value - 1
516+
expect(datatable.data.size).to eq 0
517+
end
518+
519+
it 'returns the matching user' do
520+
datatable.params[:columns]['4'][:search][:value] = largest_postgresql_integer_value
521+
expect(datatable.data.size).to eq 1
522+
end
523+
end
497524
end
498525
end

0 commit comments

Comments
 (0)