Reproducable bug

Hello,

So here’s the code:

----- BEGIN CODE -----

require ‘socket’

sock = TCPSocket.new (SERVER_ADDRESS, SERVER_PORT)

a = 'a’
a[0…0] = ''
sock.puts(String.new(a)) # CRASH!

puts ‘We never get here.’

----- END CODE -----

Just fill in your favorite ip address and port.

I don’t know if this is a bug in Array[]=, or in String.new (which I
doubt, but if you just use ‘a’ instead of ‘String.new(a)’, it doesn’t crash)
or a TCPSocket problem, but you have to use all of these for it to crash.

If it matters, I am using the 1.7.2 windows build from rubycentral.com:

ruby 1.7.2 (2002-07-02) [i386-mswin32]

So… any takers?

Chris

Hi –

Hello,

So here’s the code:

----- BEGIN CODE -----

require ‘socket’

sock = TCPSocket.new (SERVER_ADDRESS, SERVER_PORT)

a = ‘a’
a[0…0] = ‘’
sock.puts(String.new(a)) # CRASH!

puts ‘We never get here.’

String.new wants a string (literal or otherwise) as its argument.
a is an Array, so it doesn’t work:

irb(main):003:0> a = [0…0]
[0…0]
irb(main):004:0> puts String.new(a)
TypeError: failed to convert Array into String
from (irb):4:in initialize' from (irb):4:in new’
from (irb):4

This is a case where, if you wanted, you could use #to_str, which
is called in cases where objects need to masquerade as strings:

irb(main):005:0> def a.to_str; to_s; end
nil
irb(main):006:0> puts String.new(a)
0…0

Or you could just do:

puts String.new(a.to_s)

David

···

On Wed, 9 Oct 2002, HelloTree, Inc. wrote:


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com