Forum: Ruby-core [ruby-trunk - Bug #9173][Open] rb_sprintf %li format specifier does not work correctly with long val

9f807d82873e243bc91853e46142fea0?d=identicon&s=25 bladenkerst (Brad Sumersford) (Guest)
on 2013-11-28 19:09
(Received via mailing list)
Issue #9173 has been reported by bladenkerst (Brad Sumersford).

----------------------------------------
Bug #9173: rb_sprintf %li format specifier does not work correctly with
long values and can cause Ruby to crash
https://bugs.ruby-lang.org/issues/9173

Author: bladenkerst (Brad Sumersford)
Status: Open
Priority: Low
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p195
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


There is additional handling of %i versus %d in BSD_vfprintf (>= 2.0).
When l (long) is specified as the length, %li, the additional handling
is triggered.
Note: %ld works fine, %li and %ld work in Ruby 1.9.3

Sample Code:

## sprintf_test.c

#include "ruby.h"

static VALUE
sprintfTest(VALUE module, VALUE fix) {
  Check_Type(fix, T_FIXNUM);

  long long_value = FIX2LONG(fix);
  VALUE back_to_ruby = LONG2FIX(long_value);

  printf("This should show the correct value: %li\n", long_value);
  rb_funcall(rb_mKernel, rb_intern("puts"), 2, rb_str_new2("This should
also show the correct value: "), back_to_ruby);

  return rb_sprintf("This will return the wrong value, or crash: %li",
long_value);
}

void Init_sprintf() {
  rb_define_method(rb_mKernel, "sprintf_test", sprintfTest, 1);
}

## irb

require './sprintf'

irb(main):004:0> sprintf_test 3
This should show the correct value: 3
This should also show the correct value:
3
=> "This will return the wrong value, or crash: 1"

irb(main):004:0> sprintf_test 3
This should show the correct value: 3
This should also show the correct value:
3

irb(main):004:0> sprintf_test 4
This should show the correct value: 4
This should also show the correct value:
4
(irb):4: [BUG] Segmentation fault
ruby 2.0.0p195 (2013-05-14) [x86_64-darwin10.8.0]
7301191d6dc8728ccbabfdd7feab66c4?d=identicon&s=25 jeremyevans0 (Jeremy Evans) (Guest)
on 2013-11-28 21:00
(Received via mailing list)
Issue #9173 has been updated by jeremyevans0 (Jeremy Evans).


This is now expected behavior.  %i starting in 2.0 means you need to
provide a ruby object (VALUE) argument, not an integer.  You need to
switch from %li to %ld.
----------------------------------------
Bug #9173: rb_sprintf %li format specifier does not work correctly with
long values and can cause Ruby to crash
https://bugs.ruby-lang.org/issues/9173#change-43235

Author: bladenkerst (Brad Sumersford)
Status: Open
Priority: Low
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p195
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


There is additional handling of %i versus %d in BSD_vfprintf (>= 2.0).
When l (long) is specified as the length, %li, the additional handling
is triggered.
Note: %ld works fine, %li and %ld work in Ruby 1.9.3

Sample Code:

## sprintf_test.c

#include "ruby.h"

static VALUE
sprintfTest(VALUE module, VALUE fix) {
  Check_Type(fix, T_FIXNUM);

  long long_value = FIX2LONG(fix);
  VALUE back_to_ruby = LONG2FIX(long_value);

  printf("This should show the correct value: %li\n", long_value);
  rb_funcall(rb_mKernel, rb_intern("puts"), 2, rb_str_new2("This should
also show the correct value: "), back_to_ruby);

  return rb_sprintf("This will return the wrong value, or crash: %li",
long_value);
}

void Init_sprintf() {
  rb_define_method(rb_mKernel, "sprintf_test", sprintfTest, 1);
}

## irb

require './sprintf'

irb(main):004:0> sprintf_test 3
This should show the correct value: 3
This should also show the correct value:
3
=> "This will return the wrong value, or crash: 1"

irb(main):004:0> sprintf_test 3
This should show the correct value: 3
This should also show the correct value:
3

irb(main):004:0> sprintf_test 4
This should show the correct value: 4
This should also show the correct value:
4
(irb):4: [BUG] Segmentation fault
ruby 2.0.0p195 (2013-05-14) [x86_64-darwin10.8.0]
9f807d82873e243bc91853e46142fea0?d=identicon&s=25 bladenkerst (Brad Sumersford) (Guest)
on 2013-12-02 15:20
(Received via mailing list)
Issue #9173 has been updated by bladenkerst (Brad Sumersford).


I was trying to find confirmation of the change before posting this
issue but I was unable to find a reference.  Thank-you for clarifying,
this can be closed.
----------------------------------------
Bug #9173: rb_sprintf %li format specifier does not work correctly with
long values and can cause Ruby to crash
https://bugs.ruby-lang.org/issues/9173#change-43364

Author: bladenkerst (Brad Sumersford)
Status: Open
Priority: Low
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p195
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


There is additional handling of %i versus %d in BSD_vfprintf (>= 2.0).
When l (long) is specified as the length, %li, the additional handling
is triggered.
Note: %ld works fine, %li and %ld work in Ruby 1.9.3

Sample Code:

## sprintf_test.c

#include "ruby.h"

static VALUE
sprintfTest(VALUE module, VALUE fix) {
  Check_Type(fix, T_FIXNUM);

  long long_value = FIX2LONG(fix);
  VALUE back_to_ruby = LONG2FIX(long_value);

  printf("This should show the correct value: %li\n", long_value);
  rb_funcall(rb_mKernel, rb_intern("puts"), 2, rb_str_new2("This should
also show the correct value: "), back_to_ruby);

  return rb_sprintf("This will return the wrong value, or crash: %li",
long_value);
}

void Init_sprintf() {
  rb_define_method(rb_mKernel, "sprintf_test", sprintfTest, 1);
}

## irb

require './sprintf'

irb(main):004:0> sprintf_test 3
This should show the correct value: 3
This should also show the correct value:
3
=> "This will return the wrong value, or crash: 1"

irb(main):004:0> sprintf_test 3
This should show the correct value: 3
This should also show the correct value:
3

irb(main):004:0> sprintf_test 4
This should show the correct value: 4
This should also show the correct value:
4
(irb):4: [BUG] Segmentation fault
ruby 2.0.0p195 (2013-05-14) [x86_64-darwin10.8.0]
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2013-12-14 02:28
(Received via mailing list)
Issue #9173 has been updated by nobu (Nobuyoshi Nakada).

Status changed from Open to Rejected


----------------------------------------
Bug #9173: rb_sprintf %li format specifier does not work correctly with
long values and can cause Ruby to crash
https://bugs.ruby-lang.org/issues/9173#change-43658

Author: bladenkerst (Brad Sumersford)
Status: Rejected
Priority: Low
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p195
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


There is additional handling of %i versus %d in BSD_vfprintf (>= 2.0).
When l (long) is specified as the length, %li, the additional handling
is triggered.
Note: %ld works fine, %li and %ld work in Ruby 1.9.3

Sample Code:

## sprintf_test.c

#include "ruby.h"

static VALUE
sprintfTest(VALUE module, VALUE fix) {
  Check_Type(fix, T_FIXNUM);

  long long_value = FIX2LONG(fix);
  VALUE back_to_ruby = LONG2FIX(long_value);

  printf("This should show the correct value: %li\n", long_value);
  rb_funcall(rb_mKernel, rb_intern("puts"), 2, rb_str_new2("This should
also show the correct value: "), back_to_ruby);

  return rb_sprintf("This will return the wrong value, or crash: %li",
long_value);
}

void Init_sprintf() {
  rb_define_method(rb_mKernel, "sprintf_test", sprintfTest, 1);
}

## irb

require './sprintf'

irb(main):004:0> sprintf_test 3
This should show the correct value: 3
This should also show the correct value:
3
=> "This will return the wrong value, or crash: 1"

irb(main):004:0> sprintf_test 3
This should show the correct value: 3
This should also show the correct value:
3

irb(main):004:0> sprintf_test 4
This should show the correct value: 4
This should also show the correct value:
4
(irb):4: [BUG] Segmentation fault
ruby 2.0.0p195 (2013-05-14) [x86_64-darwin10.8.0]
This topic is locked and can not be replied to.