From: usa@... Date: 2018-03-28T05:59:48+00:00 Subject: [ruby-dev:50516] [Ruby trunk Bug#13994] Socket.getnameinfo が NUL終端文字列を期待している Issue #13994 has been updated by usa (Usaku NAKAMURA). Backport changed from 2.3: REQUIRED, 2.4: DONE to 2.3: DONE, 2.4: DONE ruby_2_3 r62948 merged revision(s) 60162. ---------------------------------------- Bug #13994: Socket.getnameinfo が NUL終端文字列を期待している https://bugs.ruby-lang.org/issues/13994#change-71275 * Author: tommy (Masahiro Tomita) * Status: Closed * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.5.0dev (2017-10-10 trunk 60154) [x86_64-linux] * Backport: 2.3: DONE, 2.4: DONE ---------------------------------------- Socket.getnameinfo が NUL終端文字列を期待していて、SHARABLE_MIDDLE_SUBSTRING=1 時におかしくなります。 ``` % grep abcdefg /etc/hosts /etc/services /etc/hosts:192.168.0.99 abcdefghijklmnopqrstuvwxyz.test /etc/services:abcdefghijklmnopqrstuvwxyz 9999/tcp % ruby -rsocket -e 'p Socket.getnameinfo(["AF_INET", "abcdefghijklmnopqrstuvwxyz!".chop, "abcdefghijklmnopqrstuvwxyz.test!".chop])' Traceback (most recent call last): 1: from -e:1:in `
' -e:1:in `getnameinfo': getaddrinfo: Servname not supported for ai_socktype (SocketError) ``` パッチ適用後は次のようになります。 ``` % ruby -rsocket -e 'p Socket.getnameinfo(["AF_INET", "abcdefghijklmnopqrstuvwxyz!".chop, "abcdefghijklmnopqrstuvwxyz.test!".chop])' ["abcdefghijklmnopqrstuvwxyz.test", "abcdefghijklmnopqrstuvwxyz"] ``` パッチ: ```diff diff --git a/ext/socket/socket.c b/ext/socket/socket.c index 14e069bb8d..9eb36def14 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -1287,7 +1287,7 @@ sock_s_getnameinfo(int argc, VALUE *argv) hptr = NULL; } else { - strncpy(hbuf, StringValuePtr(host), sizeof(hbuf)); + strncpy(hbuf, StringValueCStr(host), sizeof(hbuf)); hbuf[sizeof(hbuf) - 1] = '\0'; hptr = hbuf; } @@ -1301,7 +1301,7 @@ sock_s_getnameinfo(int argc, VALUE *argv) pptr = pbuf; } else { - strncpy(pbuf, StringValuePtr(port), sizeof(pbuf)); + strncpy(pbuf, StringValueCStr(port), sizeof(pbuf)); pbuf[sizeof(pbuf) - 1] = '\0'; pptr = pbuf; } ``` -- https://bugs.ruby-lang.org/