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

Conversation

PhilippMeissner
Copy link

@PhilippMeissner PhilippMeissner commented Mar 9, 2018

Hi folks!
We've lately had a few users trying to search for values that were not meant to be searched but still caused datatables to try and search for it. This lead to this error

9056216999123 is out of range for ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer with limit 4

This issue only applies to :eq searches and only if it's an integer value that's stored in this very column.

Maybe this PR is of use. If not, have it removed please 👍

@n-rodriguez
Copy link
Member

Hi! Thanks for your contribution! There are some changes to do before I can merge it. Also can you please rebase on master? Thank you!

@n-rodriguez n-rodriguez self-assigned this Apr 18, 2018
@PhilippMeissner
Copy link
Author

Hey @n-rodriguez - What do you mean by rebase? Do you want me to squash the three commits of mine so that it is only one? That should be available in the dropdown when you merge the PR through the UI. But if you mean something else, hit me up. What changes do you need to be done? Maybe I can spend some more time.

@n-rodriguez
Copy link
Member

n-rodriguez commented Apr 21, 2018

Hi! I mean merging the last commits I've made into your branch. But since it's a rebase it's gonna take my commits first then apply yours. It works like that :

git remote add upstream https://github.com/jbox-web/ajax-datatables-rails.git
git fefch upstream/master # or 'upstream master' don't remember
git checkout <your branch>
git rebase upstream/master

You'll have to do git push -f -u origin <your branch>to push your changes.

@PhilippMeissner PhilippMeissner force-pushed the fix-integer-out-of-range branch from 4fcc4a2 to c7f3c6f Compare April 22, 2018 19:26
@PhilippMeissner
Copy link
Author

Hey @n-rodriguez. Thanks for the small tour on rebasing - I've followed it and pushed everything :)

Copy link
Member

@n-rodriguez n-rodriguez left a comment

Choose a reason for hiding this comment

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

There are some changes to do before I can merge it. Thank you?

@@ -49,12 +52,20 @@ def regex_search
end
end

def empty_search
casted_column.matches("")
Copy link
Member

Choose a reason for hiding this comment

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

Use of simple quote is recommanded

empty_search
else
numeric_search
end
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be more readable if you invert the logic :

  if is_searchable_integer?
    numeric_search
  else
    empty_search
  end

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

def 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.

Add parenthesis around arguments

@@ -82,6 +93,18 @@ def numeric_search
end
end

def is_searchable_integer?
return false unless table.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.

Invert the last test to match with the previous recommandation :

  def is_searchable_integer?
    return false unless table.respond_to?(:engine)
    table.engine.columns_hash[field.to_s].sql_type == 'integer' && is_integer?(search.value) && !is_out_of_range?(search.value)
  end

@PhilippMeissner
Copy link
Author

All change requests have been added and the tests are still running fine (on my end: bundle exec rspec).
Thanks for your suggestions and time 👍

@n-rodriguez n-rodriguez force-pushed the master branch 6 times, most recently from 881e955 to fc1c987 Compare May 6, 2018 03:17
@n-rodriguez
Copy link
Member

Hi! Tests are failing for Rails < 5.x. Can you please take a look?

@n-rodriguez
Copy link
Member

Hi! There is something wrong with your implementation.

@@ -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

@@ -82,6 +89,18 @@ def numeric_search
end
end

def is_searchable_integer?
return true unless table.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

@n-rodriguez
Copy link
Member

Actually your PR does nothing.

@n-rodriguez n-rodriguez force-pushed the master branch 4 times, most recently from e975b07 to bd0393b Compare May 10, 2018 21:50
@n-rodriguez
Copy link
Member

n-rodriguez commented May 10, 2018

Ok, I think you forgot to create a DB field with a limited int size. By default Rails use int(11) on 4.2 and bigint on 5.x so no error is triggered with your tests and the default config.

@n-rodriguez
Copy link
Member

n-rodriguez commented May 10, 2018

Ok, it turns out you're using the wrong datatable in tests so it's doesn't use :eq condition but :like ;)

@@ -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) }

@PhilippMeissner PhilippMeissner deleted the fix-integer-out-of-range branch May 11, 2018 06:28
@PhilippMeissner
Copy link
Author

Hey. Thank you for the effort. I am sorry I did not reply, was quite busy these days (plus there were a lot of holidays in Germany this month ;)).
Thanks for fixing and merging!

@n-rodriguez
Copy link
Member

Thanks for fixing and merging!

Thank you too, you found it 🎉 I realized latter I could have merged your PR and make the fixes right after... sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants