Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Valid number pretending to be a invalid number in ActiveModel tests

#4622

Running the ActiveModel testsuite in Rails master on ruby 1.9.2dev (2010-05-08 trunk 27665) [x86_64-linux] gives 3 failures.

2 failures are because of a number that's supposed to be bad but is actually good. I've made a patch that makes the number bad again, and will attach it in a comment.

The stacktrace:

1) Failure: test_default_validates_numericality_of(NumericalityValidationTest) [/home/rohit/remote-repos/rails_patches/working1/activemodel/test/cases/validations/numericality_validation_test.rb:166]:
"0xdeadbeef" not rejected as a number

2) Failure: test_validates_numericality_of_with_nil_allowed(NumericalityValidationTest) [/home/rohit/remote-repos/rails_patches/working1/activemodel/test/cases/validations/numericality_validation_test.rb:166]:
"0xdeadbeef" not rejected as a number

3) Failure: test_should_serialize_yaml(XmlSerializationTest) [/home/rohit/remote-repos/rails_patches/working1/activemodel/test/cases/serializeration/xml_serialization_test.rb:107]:
Expected /\n\n aaron stack\n

303 tests, 865 assertions, 3 failures, 0 errors, 0 skips

Test run options: --seed 11594
rake aborted!
Command failed with status (1): [/home/rohit/.rvm/rubies/ruby-1.9.2-head/bi...]

Failure 1 and 2 are because of the good number "0xdeadbeef" in the JUNK array in numericality_validation_test.rb Changing it to an invalid number like "0xdeadbeet" makes the test pass as expected. Patch will be provided in a comment.

P.S I don't know how to fix the 3rd failure.

Reported by Rohit Arondekar · May 17th, 2010 @ 05:09 AM

State: committed
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar

    I've attached a patch.

    P.S rubydiamond from #railsbridge has confirmed that the tests pass in 1.8.7, so it might be something that needs to be looked into more carefully.

    May 17th, 2010 @ 05:12 AM

  2. Anil Wadghule
    Anil Wadghule

    +1

    Verified patch on ruby-1.8.7-p249 and ruby-1.9.2-head.

    Still a question I have is, why 1.9.2 considers 0xdeadbeef as valid number but 1.8.7 does not.

    When inspected in irb for both rubies, it shows

    ruby-1.8.7-p249 >   0xdeadbeef.class
     => Bignum 
    ruby-1.8.7-p249 > 0xdeadbeef
     => 3735928559
    
    ruby-1.9.2-head > 0xdeadbeef.class
     => Bignum 
    ruby-1.9.2-head > 0xdeadbeef
     => 3735928559
    

    Need to know reason for putting this hex number 0xdeadbeef as not valid numerical test. Thoughts?

    May 17th, 2010 @ 09:59 AM

  3. Anil Wadghule
    Anil Wadghule

    Looks like 1.9.2 is converting hex number to float but 1.8.7 don't

    ruby-1.8.7-p249 > Kernel.Float("0xdeadbeef")
    ArgumentError: invalid value for Float(): "0xdeadbeef"
        from (irb):1:in `Float'
        from (irb):1
    
    ruby-1.9.2-head > Kernel.Float("0xdeadbeef")
     => 3735928559.0
    

    +1 for the patch

    May 17th, 2010 @ 10:10 AM

  4. Rohit Arondekar
    Rohit Arondekar

    I think I've got it! Take a look at the following code from activemodel/lib/active_model/validations/numericality.rb


      def parse_raw_value_as_a_number(raw_value)
        begin
          Kernel.Float(raw_value)
        rescue ArgumentError, TypeError
          nil
        end
      end
    
      def parse_raw_value_as_an_integer(raw_value)
        raw_value.to_i if raw_value.to_s =~ /\A[+-]?\d+\Z/
      end
    

    Note how a raw integer value is parsed. Using a regex, most probably because Kernel.Integer honors radix indicators like 0x.

    Whereas a raw float is parsed using Kernel.Float which until 1.9.2 (or maybe 1.9.1) didn't honor radix indicators. But now that it does on 1.9.2, bam! it accepts a number which was not meant to be accepted. That's my understanding so far about this issue.

    May 17th, 2010 @ 11:28 AM

  5. Rohit Arondekar
    Rohit Arondekar

    I can confirm that ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux] behaves like 1.8.7 but 1.9.2 doesn't. I believe I've found the changeset that did this => http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=26965 and it's associated feature => http://redmine.ruby-lang.org/issues/show/2969 but the discussions are in Japanese :(

    I can't make any more progress on this issue as I think I've reached a dead-end, I don't plan on digging in the Ruby source code :P. Hopefully a Core member or somebody who knows how the validator should behave can shed some light.

    May 17th, 2010 @ 11:52 AM

  6. Rizwan Reza
    Rizwan Reza
    • Milestone cleared.
    • Tag changed from rails 3.0 activemodel activerecord tests, bugmash to bugmash-review
    • State changed from new to verified

    May 17th, 2010 @ 12:08 PM

  7. José Valim
    José Valim
    • Assigned user changed from Pratik to José Valim

    Great work debugging guys!

    The fix is not changing the test to use "0xdeadbeet". I doubt our applications should allow hexadecimals entries. IMHO, we should simply change the code to check if the number does not start with "0x". If it does, it should be marked as not a number.

    May 17th, 2010 @ 12:33 PM

  8. Anil Wadghule
    Anil Wadghule

    I've attached a patch which fixes the broken numericality tests for ruby-1.9.2-head. Tests pass on ruby-1.8.7-p249 too.

    May 17th, 2010 @ 02:05 PM

  9. Repository
    Repository
    • State changed from verified to resolved

    May 17th, 2010 @ 03:59 PM

  10. Rizwan Reza
  11. Rohit Arondekar
    Rohit Arondekar
    • Tag set to bugmash

    I've attached a patch. First only a failing test to show that the commit doesn't block hex numbers of the form 0X22 and another patch to fix the code (with the test).

    May 18th, 2010 @ 01:42 AM

  12. Ryan Bigg
    Ryan Bigg
    • Tag changed from bugmash to bugmash, bugmash-review
    • State changed from resolved to open

    May 18th, 2010 @ 01:54 AM

  13. Repository
  14. Repository
  15. Ryan Bigg
    Ryan Bigg
    • State changed from open to committed

    May 19th, 2010 @ 10:21 AM

  16. Rizwan Reza
    Rizwan Reza
    • Tag changed from bugmash, bugmash-review to bugmash

    June 6th, 2010 @ 09:29 AM

  17. Rizwan Reza
  18. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM

  19. Jeff Kreeftmeijer
  20. teiddy
    teiddy

    Instructions: download and untar to /sites/all/modules

    also - Download and install 'Rules' module to /sites/all/modules

    Run /update.php

    Goto Admin>Build>Modules, enable "OA Single Group Login Redirect" module and allow Rules module to be activated when prompted.

    Log-out as admin and log-in as user with 1 group - page redirects as expected.

    Greyside Thank-you!

    Thank you for this information,I like it very much,Would you lik a pair of
    Pachuco Suits
    pack linen clothes
    pack suit into suit
    pant length
    pant length for men

    paul smith mens suits
    Welcome to our store,We have the best service team!

    November 30th, 2010 @ 05:54 AM