Issue #13994 has been reported by tommy (Masahiro Tomita).

----------------------------------------
Bug #13994: Socket.getnameinfo が NUL終端文字列を期待している
https://bugs.ruby-lang.org/issues/13994

* Author: tommy (Masahiro Tomita)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.0dev (2017-10-10 trunk 60154) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
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 `<main>'
-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/