diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-10-23 02:18:00 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-10-23 02:18:00 +0000 |
commit | adbc146adc69f75b2040c5c5701de864f9c77a5c (patch) | |
tree | b93bb035e043d6f77196376af9f8f5790882c031 /ext/socket/socket.c | |
parent | 03e2f19a3f3b807421ba6bdcc14b9cf0ffa7e223 (diff) | |
download | ruby-adbc146adc69f75b2040c5c5701de864f9c77a5c.tar.gz ruby-adbc146adc69f75b2040c5c5701de864f9c77a5c.tar.xz ruby-adbc146adc69f75b2040c5c5701de864f9c77a5c.zip |
* ext/socket/socket.c (sock_s_getservbyport): the port should be
converted before the proto so that the #to_int of the former cannot
alter the latter.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@19895 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/socket.c')
-rw-r--r-- | ext/socket/socket.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/ext/socket/socket.c b/ext/socket/socket.c index 06d9f337c..5788632f8 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -3249,14 +3249,16 @@ sock_s_getservbyport(int argc, VALUE *argv) { VALUE port, proto; struct servent *sp; + long portnum; + const char *protoname = "tcp"; rb_scan_args(argc, argv, "11", &port, &proto); - if (NIL_P(proto)) proto = rb_str_new2("tcp"); - StringValue(proto); + portnum = NUM2LONG(port); + if (!NIL_P(proto)) protoname = StringValueCStr(proto); - sp = getservbyport(htons((uint16_t)NUM2INT(port)), StringValueCStr(proto)); + sp = getservbyport((int)htons((uint16_t)portnum), protoname); if (!sp) { - rb_raise(rb_eSocket, "no such service for port %d/%s", NUM2INT(port), RSTRING_PTR(proto)); + rb_raise(rb_eSocket, "no such service for port %d/%s", (int)portnum, protoname); } return rb_tainted_str_new2(sp->s_name); } |