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

String to Integer conversion incompatibility with CRuby #3043

Closed
tsahara opened this Issue Dec 10, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@tsahara
Member

tsahara commented Dec 10, 2015

Kernel#sprintf behaves differently from CRuby for some inputs.
(I found these incompatibilities while applying 0701236 to our fork of mruby repository.)

% bin/mruby -e 'sprintf("%u", "0x")'
trace:
        [0] -e:1
-e:1: string contains null byte (ArgumentError)

% ruby -e 'sprintf("%u", "0x")'
-e:1:in `sprintf': invalid value for Integer(): "0x" (ArgumentError)
    from -e:1:in `<main>'

The message of the exception mruby raises is wrong ("0x" does not contain any nul character). "0x" can be "-", "+", or "".

% bin/mruby -e 'p sprintf("%u", "0_1")'
trace:
    [0] -e:1
-e:1: invalid string for number("0_1") (ArgumentError)

% ruby -e 'p sprintf("%u", "0_1")'
"1"

It should be "1".

% bin/mruby -e 's="0x0"+"0"*100; t=s[0,2]; p t, sprintf("%u", t)'
"0x"
"0"

% ruby -e 's="0x0"+"0"*100; t=s[0,2]; p t, sprintf("%u", t)'
-e:1:in `sprintf': invalid value for Integer(): "0x" (ArgumentError)
    from -e:1:in `<main>'

I don't know which one is right but they behave differently.

% bin/mruby -e 's="1_"+"0"*100; t=s[0,2]; p t, sprintf("%u", t)'
"1_"
"10"

% ruby -e 's="1_"+"0"*100; t=s[0,2]; p t, sprintf("%u", t)'
-e:1:in `sprintf': invalid value for Integer(): "1_" (ArgumentError)
    from -e:1:in `<main>'

It should raise an exception, or return "1" (not "10").

@matz matz closed this in 26a25e1 Dec 12, 2015

@tsahara

This comment has been minimized.

Show comment
Hide comment
@tsahara

tsahara Dec 14, 2015

Member

Thank you for fixing the first issue. But others are not changed. Especially, 3rd one looks strange to me because it returns a string when it takes a shared string as an argument but raises an exception when it takes a non-shared string. For example:

a.rb:

s = "0x0" + "0" * 100

a = "0x"
b = s[0, 2]

puts "a=#{a}"
puts "b=#{b}"
puts "(a==b) == #{a==b}"

begin
  printf "'%%u' %% a => "
  printf "%u\n", a
rescue => e
  printf "(#{e})\n"
end

begin
  printf "'%%u' %% b => "
  printf "%u\n", b
rescue => e
  printf "(#{e})\n"
end
% bin/mruby a.rb
a=0x
b=0x
(a==b) == true
'%u' % a => (invalid string for number("0x"))
'%u' % b => 0
Member

tsahara commented Dec 14, 2015

Thank you for fixing the first issue. But others are not changed. Especially, 3rd one looks strange to me because it returns a string when it takes a shared string as an argument but raises an exception when it takes a non-shared string. For example:

a.rb:

s = "0x0" + "0" * 100

a = "0x"
b = s[0, 2]

puts "a=#{a}"
puts "b=#{b}"
puts "(a==b) == #{a==b}"

begin
  printf "'%%u' %% a => "
  printf "%u\n", a
rescue => e
  printf "(#{e})\n"
end

begin
  printf "'%%u' %% b => "
  printf "%u\n", b
rescue => e
  printf "(#{e})\n"
end
% bin/mruby a.rb
a=0x
b=0x
(a==b) == true
'%u' % a => (invalid string for number("0x"))
'%u' % b => 0
@matz

This comment has been minimized.

Show comment
Hide comment
@matz

matz Dec 14, 2015

Member

Sorry for the partial fix. I will fix the rest of the problems.

Member

matz commented Dec 14, 2015

Sorry for the partial fix. I will fix the rest of the problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment