Bug #604: Socket.pack_sockaddr_in() fails for Bignum instances of size 4
http://redmine.ruby-lang.org/issues/show/604

Author: Aravind Srinivasan
Status: Open, Priority: Normal

ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux]

irb(main):001:0> require 'socket'
=> true
irb(main):002:0> Socket.pack_sockaddr_in(0, '192.168.1.1')
=> "\002\000\000\000\300\250\001\001\000\000\000\000\000\000\000\000"
irb(main):003:0> Socket.pack_sockaddr_in(0, 0xc0a80101)
RangeError: bignum too big to convert into `long'
	from (irb):3:in `pack_sockaddr_in'
	from (irb):3
	from :0
irb(main):004:0> 0xc0a80101.class
=> Bignum
irb(main):005:0> 0xc0a80101.size
=> 4

The issue might be due to implementation of ext/socket/socket.c:866: host_str() function.
The function goes like this ...
...
static char*
host_str(VALUE host, char *hbuf, size_t len)
{
    if (NIL_P(host)) {
        return NULL;
    }
    else if (rb_obj_is_kind_of(host, rb_cInteger)) {
        long i = NUM2LONG(host);

        make_inetaddr(htonl(i), hbuf, len);
        return hbuf;
...

In the above snippet, i feel instead of 
        long i = NUM2LONG(host);
it should be
        unsigned long i = NUM2ULONG(host);
as htonl accepts uint32_t.


----------------------------------------
http://redmine.ruby-lang.org