From: Nobuyoshi Nakada Date: 2008-10-23T14:33:39+09:00 Subject: [ruby-core:19460] Re: [Bug #678] Socket.getservbyport should convert the port param to network byte order Hi, At Thu, 23 Oct 2008 07:54:55 +0900, Dave Thomas wrote in [ruby-core:19454]: > getservbyname returns the port number in native byte order > (so "telnet" returns 23). > > But if you pass 23 back to getserverbyport, it fails, because > it doesn't covert the param to network byte order before > calling getservbyport(2). I think it should check if the port number fits with int16_t. $ ./ruby -v -rsocket -e 'p Socket.getservbyport(2**16+23)' ruby 1.9.0 (2008-10-23 revision 19896) [i686-linux] "telnet" Index: ext/socket/socket.c =================================================================== --- ext/socket/socket.c (revision 19896) +++ ext/socket/socket.c (working copy) @@ -3255,4 +3255,8 @@ sock_s_getservbyport(int argc, VALUE *ar rb_scan_args(argc, argv, "11", &port, &proto); portnum = NUM2LONG(port); + if (portnum != (uint16_t)portnum) { + const char *s = portnum > 0 ? "big" : "small"; + rb_raise(rb_eRangeError, "integer %ld too %s to convert into `int16_t'", portnum, s); + } if (!NIL_P(proto)) protoname = StringValueCStr(proto); -- Nobu Nakada