Skip to content

Fix integer out of range #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/ajax-datatables-rails/datatable/column/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module Datatable
class Column
module Search

SMALLEST_PQ_INTEGER = -2147483648
LARGEST_PQ_INTEGER = 2147483647

def searchable?
@view_column.fetch(:searchable, true)
end
Expand Down Expand Up @@ -49,12 +52,16 @@ def regex_search
end
end

def empty_search
casted_column.matches('')
end

def non_regex_search
case cond
when Proc
filter
when :eq, :not_eq, :lt, :gt, :lteq, :gteq, :in
numeric_search
is_searchable_integer? ? numeric_search : empty_search
when :null_value
null_value_search
when :start_with
Expand Down Expand Up @@ -82,6 +89,18 @@ def numeric_search
end
end

def is_searchable_integer?
return true unless table.respond_to?(:engine)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This always return true on Rails 5.x because table doesn't respond to :engine

table.engine.columns_hash[field.to_s].sql_type == 'integer' && is_integer?(search.value) && !is_out_of_range?(search.value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You get to this point on Rails 4 but is_out_of_range? always return false because it (can) receives an array of integer and is_integer?(string) shadows the casting error by rescuing to false.

  1) AjaxDatatablesRails::ORM::ActiveRecord filter conditions it can filter records with condition :in should filter records matching
     Failure/Error: Integer(search_value) > LARGEST_PQ_INTEGER || Integer(search_value) < SMALLEST_PQ_INTEGER
     
     TypeError:
       can't convert Array into Integer

end

def is_out_of_range?(search_value)
Integer(search_value) > LARGEST_PQ_INTEGER || Integer(search_value) < SMALLEST_PQ_INTEGER
end

def is_integer?(string)
true if Integer(string) rescue false
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,32 @@
end
end

describe 'Integer overflows' do
let(:largest_postgresql_integer_value) { 2147483647 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It misses let(:datatable) { DatatableCondEq.new(view) }

let(:smallest_postgresql_integer_value) { -2147483648 }

before(:each) do
create(:user, first_name: 'john', post_id: 1)
create(:user, first_name: 'mary', post_id: 2)
create(:user, first_name: 'phil', post_id: largest_postgresql_integer_value)
end

it 'Returns an empty result if input value is too large' do
datatable.params[:columns]['4'][:search][:value] = largest_postgresql_integer_value + 1
expect(datatable.data.size).to eq 0
end

it 'Returns an empty result if input value is too small' do
datatable.params[:columns]['4'][:search][:value] = smallest_postgresql_integer_value - 1
expect(datatable.data.size).to eq 0
end

it 'returns the matching user' do
datatable.params[:columns]['4'][:search][:value] = largest_postgresql_integer_value
expect(datatable.data.size).to eq 1
end
end

describe 'it can filter records with condition :eq' do
let(:datatable) { DatatableCondEq.new(view) }

Expand Down