In 1.8.0 nil.to_s is not the same as ""

Hi,

There is a curious bug in 1.8.0:
Run the following:

class Awk < String; end
a = "a"
c = nil.to_s
#c = ""
b = Awk.new©
a.send(b)

and get:

bug.rb:7: [BUG] Segmentation fault
ruby 1.8.0 (2003-03-03) [i686-linux]
Aborted

Now uncomment the fourth line:

bug.rb:7:in send': undefined method’ for “a”:String (NoMethodError)
from bug.rb:7

Ruby 1.6.8 is fine.

Cheers,

Han Holl

Hi,

···

At Fri, 20 Jun 2003 18:13:30 +0900, Han Holl wrote:

bug.rb:7: [BUG] Segmentation fault
ruby 1.8.0 (2003-03-03) [i686-linux]
Aborted

Try this patch.

Index: object.c

RCS file: /cvs/ruby/src/ruby/object.c,v
retrieving revision 1.119
diff -u -2 -p -r1.119 object.c
— object.c 26 May 2003 08:22:28 -0000 1.119
+++ object.c 20 Jun 2003 10:06:56 -0000
@@ -789,5 +789,5 @@ rb_to_id(name)
switch (TYPE(name)) {
case T_STRING:

  • return rb_intern(RSTRING(name)->ptr);
  • return rb_str_intern(name);
    case T_FIXNUM:
    rb_warn(“do not use Fixnums as Symbols”);


Nobu Nakada

Try this patch.

You have the same with

class A
   def to_str
      String.new(nil.to_s)
   end
end
a = "a"
b = A.new
a.send(b)

Guy Decoux

p.s. : plruby don't like this case, and it's protected against it ...

nobu.nokada@softhome.net wrote in message news:200306201009.h5KA9ekR009612@sharui.nakada.kanuma.tochigi.jp

Hi,

bug.rb:7: [BUG] Segmentation fault
ruby 1.8.0 (2003-03-03) [i686-linux]
Aborted

Try this patch.

Did that, and now it works properly, but the following no longer functions:

module Pql
%w{ TRACE DEBUG REQREADER }.each do |el|
const_set(el, nil) unless const_defined?(el)
end

I get:
pql_env.rb:5:in `const_defined?': wrong constant name (null) (NameError)

For the time being the earlier problem is easier to work around.

Cheers,

Han Holl

···

At Fri, 20 Jun 2003 18:13:30 +0900, > Han Holl wrote:

Hi,

···

At Fri, 20 Jun 2003 19:25:57 +0900, ts wrote:

You have the same with

class A
def to_str
String.new(nil.to_s)
end
end
a = “a”
b = A.new
a.send(b)

I missed the same code just below.

And I was complained about the message by unak.

Index: object.c

RCS file: /cvs/ruby/src/ruby/object.c,v
retrieving revision 1.119
diff -u -2 -p -r1.119 object.c
— object.c 26 May 2003 08:22:28 -0000 1.119
+++ object.c 20 Jun 2003 10:42:33 -0000
@@ -789,5 +789,5 @@ rb_to_id(name)
switch (TYPE(name)) {
case T_STRING:

  • return rb_intern(RSTRING(name)->ptr);
  • return rb_str_intern(name);
    case T_FIXNUM:
    rb_warn(“do not use Fixnums as Symbols”);
    @@ -803,5 +803,5 @@ rb_to_id(name)
    tmp = rb_check_string_type(name);
    if (!NIL_P(tmp)) {
  •   return rb_intern(RSTRING(tmp)->ptr);
    
  •   return rb_str_intern(tmp);
    

    }
    rb_raise(rb_eTypeError, “%s is not a symbol”, RSTRING(rb_inspect(name))->ptr);
    Index: string.c
    ===================================================================
    RCS file: /cvs/ruby/src/ruby/string.c,v
    retrieving revision 1.156
    diff -u -2 -p -r1.156 string.c
    — string.c 26 May 2003 08:22:28 -0000 1.156
    +++ string.c 20 Jun 2003 10:29:32 -0000
    @@ -3073,5 +3073,5 @@ rb_str_intern(str)

    if (!RSTRING(str)->ptr || RSTRING(str)->len == 0) {

  • rb_raise(rb_eArgError, “interning empty string”);
  • rb_raise(rb_eArgError, “symbol string may not be empty”);
    }
    if (strlen(RSTRING(str)->ptr) != RSTRING(str)->len)


Nobu Nakada

Hi,

···

At Fri, 20 Jun 2003 23:16:01 +0900, Han Holl wrote:

Did that, and now it works properly, but the following no longer functions:

module Pql
%w{ TRACE DEBUG REQREADER }.each do |el|
const_set(el, nil) unless const_defined?(el)
end

I get:
pql_env.rb:5:in `const_defined?': wrong constant name (null) (NameError)

Sorry, try the patch in [ruby-talk:74014] instead.


Nobu Nakada

Hi,

···

At Fri, 20 Jun 2003 19:52:07 +0900, nobu.nokada@softhome.net wrote:

Index: object.c

RCS file: /cvs/ruby/src/ruby/object.c,v
retrieving revision 1.119
diff -u -2 -p -r1.119 object.c

Sorry, this was wrong. rb_to_id() must return ID instead of
Symbol.

Index: object.c

RCS file: /cvs/ruby/src/ruby/object.c,v
retrieving revision 1.119
diff -u -2 -p -r1.119 object.c
— object.c 26 May 2003 08:22:28 -0000 1.119
+++ object.c 20 Jun 2003 11:14:57 -0000
@@ -789,5 +789,6 @@ rb_to_id(name)
switch (TYPE(name)) {
case T_STRING:

  • return rb_intern(RSTRING(name)->ptr);
  • tmp = rb_str_intern(name);
  • return SYM2ID(tmp);
    case T_FIXNUM:
    rb_warn(“do not use Fixnums as Symbols”);
    @@ -803,5 +804,6 @@ rb_to_id(name)
    tmp = rb_check_string_type(name);
    if (!NIL_P(tmp)) {
  •   return rb_intern(RSTRING(tmp)->ptr);
    
  •   tmp = rb_str_intern(tmp);
    
  •   return SYM2ID(tmp);
    
    }
    rb_raise(rb_eTypeError, “%s is not a symbol”, RSTRING(rb_inspect(name))->ptr);


Nobu Nakada

Hi,

···

In message “Re: In 1.8.0 nil.to_s is not the same as “”” on 03/06/20, nobu.nokada@softhome.net nobu.nokada@softhome.net writes:

Sorry, try the patch in [ruby-talk:74014] instead.

Commit the fix, please. I will release preview3 afterwards.

						matz.

nobu.nokada@softhome.net wrote in message news:200306201425.h5KEPBkR011951@sharui.nakada.kanuma.tochigi.jp

Hi,

Sorry, try the patch in [ruby-talk:74014] instead.

Yes, thanks. Works fine now, but there is an incompatibilty with 1.6.8:
obj.send(“”) used to be OK, (dispatched to method_missing), and now gives an error:
in `send’: interning empty string (ArgumentError)

I don’t have a problem with that, but maybe it should be on a caveat list somewhere.

Cheers,

Han Holl

Hi,

···

At Sat, 21 Jun 2003 13:35:26 +0900, Yukihiro Matsumoto wrote:

Sorry, try the patch in [ruby-talk:74014] instead.

Commit the fix, please. I will release preview3 afterwards.

What about the message mentioned at [ruby-talk:74012]? Since
that function is also used from String#to_sym not only #intern,
so the current message doesn’t feel appropriate.


Nobu Nakada

Hi,

Yes, thanks. Works fine now, but there is an incompatibilty with 1.6.8:
obj.send(“”) used to be OK, (dispatched to method_missing), and now gives an error:
in `send’: interning empty string (ArgumentError)

Well, I guess it is caused from:

  • there’s an incompatiblity for #intern already, which empty
    string is not allowed as symbol in 1.8

    $ ruby-1.6 -e ‘p “”.intern’
    :
    $ ruby-1.8 -e ‘p “”.intern’
    -e:1:in `intern’: interning empty string (ArgumentError)
    from -e:1

  • and method name should be a symbol

    $ ruby-1.6 -e ‘send(Object.new)’
    -e:1:in `send’: #Object:0x402b8a90 is not a symbol (TypeError)
    from -e:1

IMHO, these just hadn’t been reflected on Kernel#send yet.

···

At Sat, 21 Jun 2003 21:41:56 +0900, Han Holl wrote:


Nobu Nakada

Hi,

···

In message “Re: In 1.8.0 nil.to_s is not the same as “”” on 03/06/21, nobu.nokada@softhome.net nobu.nokada@softhome.net writes:

What about the message mentioned at [ruby-talk:74012]? Since
that function is also used from String#to_sym not only #intern,
so the current message doesn’t feel appropriate.

“intern” is a canonical name for string to symbol operation in the
lisp world. I see no problem.

						matz.