New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket.gethostname ignores encoding settings #2558

Closed
jsvd opened this Issue Feb 4, 2015 · 6 comments

Comments

Projects
None yet
5 participants
@jsvd

jsvd commented Feb 4, 2015

tested on windows but also happens on macosx. I can't make Socket.gethostname return a UTF-8 string even when fiddling with Encoding.default_internal and external settings.

Example:

C:\Users\Jsvd>irb
io/console not supported; tty will not be manipulated
irb(main):001:0>  puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
Windows-1252
Windows-1252
=> nil
irb(main):002:0>

C:\Users\Jsvd>irb -EUTF-8
io/console not supported; tty will not be manipulated
irb(main):001:0> puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
UTF-8
Windows-1252
=> nil
irb(main):002:0>

C:\Users\Jsvd>jruby -v
jruby 1.7.17 (1.9.3p392) 2014-12-09 fafd1a7 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_31-b13 +jit [Windows 7-amd64]
@jordansissel

This comment has been minimized.

Show comment
Hide comment
@jordansissel

jordansissel Feb 5, 2015

Contributor

MRI 2.1.4 also has this bug.

Contributor

jordansissel commented Feb 5, 2015

MRI 2.1.4 also has this bug.

@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Feb 24, 2015

Member

I believe this would fix it. I noticed gethostbyname and others are not using this path either.

If someone can confirm this fix (I'm on Windows and struggling with dev env at the moment), we can get it merged in.

diff --git a/core/src/main/java/org/jruby/ext/socket/SocketUtils.java b/core/src/main/java/org/jruby/ext/socket/SocketUtils.java        
index b1a457e..65110a7 100644                                                                                                           
--- a/core/src/main/java/org/jruby/ext/socket/SocketUtils.java                                                                          
+++ b/core/src/main/java/org/jruby/ext/socket/SocketUtils.java                                                                          
@@ -74,12 +74,12 @@ public class SocketUtils {                                                                                          
         Ruby runtime = context.runtime;                                                                                                

         try {                                                                                                                          
-            return runtime.newString(InetAddress.getLocalHost().getHostName());                                                        
+            return RubyString.newInternalFromJavaExternal(runtime, InetAddress.getLocalHost().getHostName());                          

         } catch(UnknownHostException e) {                                                                                              

             try {                                                                                                                      
-                return runtime.newString(InetAddress.getByAddress(new byte[]{0,0,0,0}).getHostName());                                 
+                return RubyString.newInternalFromJavaExternal(runtime, InetAddress.getByAddress(new byte[]{0, 0, 0, 0}).getHostName());

             } catch(UnknownHostException e2) {                                                                                         
                 throw sockerr(runtime, "gethostname: name or service not known");                                                      

I'm confused why it would be Windows-1252, though. At least on 9k, the logic for runtime.newString above will always use UTF-8 as the string encoding by default.

Member

headius commented Feb 24, 2015

I believe this would fix it. I noticed gethostbyname and others are not using this path either.

If someone can confirm this fix (I'm on Windows and struggling with dev env at the moment), we can get it merged in.

diff --git a/core/src/main/java/org/jruby/ext/socket/SocketUtils.java b/core/src/main/java/org/jruby/ext/socket/SocketUtils.java        
index b1a457e..65110a7 100644                                                                                                           
--- a/core/src/main/java/org/jruby/ext/socket/SocketUtils.java                                                                          
+++ b/core/src/main/java/org/jruby/ext/socket/SocketUtils.java                                                                          
@@ -74,12 +74,12 @@ public class SocketUtils {                                                                                          
         Ruby runtime = context.runtime;                                                                                                

         try {                                                                                                                          
-            return runtime.newString(InetAddress.getLocalHost().getHostName());                                                        
+            return RubyString.newInternalFromJavaExternal(runtime, InetAddress.getLocalHost().getHostName());                          

         } catch(UnknownHostException e) {                                                                                              

             try {                                                                                                                      
-                return runtime.newString(InetAddress.getByAddress(new byte[]{0,0,0,0}).getHostName());                                 
+                return RubyString.newInternalFromJavaExternal(runtime, InetAddress.getByAddress(new byte[]{0, 0, 0, 0}).getHostName());

             } catch(UnknownHostException e2) {                                                                                         
                 throw sockerr(runtime, "gethostname: name or service not known");                                                      

I'm confused why it would be Windows-1252, though. At least on 9k, the logic for runtime.newString above will always use UTF-8 as the string encoding by default.

@headius headius added this to the 9.0.0.0.pre2 milestone Feb 24, 2015

@headius headius modified the milestones: 9.0.0.0.rc1, 9.0.0.0.pre2 Apr 17, 2015

@headius headius closed this in 0429799 Jul 2, 2015

@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Jul 2, 2015

Member

Fixed in 9k.rc2 and 1.7.21.

Member

headius commented Jul 2, 2015

Fixed in 9k.rc2 and 1.7.21.

@jsvd

This comment has been minimized.

Show comment
Hide comment
@jsvd

jsvd Oct 26, 2015

This is still an issue on 1.7.22

C:\Users\Jsvd>jruby -v
jruby 1.7.22 (1.9.3p551) 2015-08-20 c28f492 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_31-b13 +jit [Windows 7-amd64]

C:\Users\Jsvd>jruby -S irb -EUTF-8
io/console not supported; tty will not be manipulated
irb(main):001:0> puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
UTF-8
Windows-1252
=> nil
irb(main):003:0> s = TCPServer.new(3335)
=> #<TCPServer:fd 15>
irb(main):005:0> c = TCPSocket.new("localhost", 3335)
=> #<TCPSocket:fd 16>
irb(main):006:0> c.peeraddr
=> ["AF_INET", 3335, "127.0.0.1", "127.0.0.1"]
irb(main):007:0> c.peeraddr.first.encoding
=> #<Encoding:Windows-1252>
irb(main):008:0> c.peeraddr.last.encoding
=> #<Encoding:Windows-1252>
irb(main):009:0> "".encoding
=> #<Encoding:UTF-8>
irb(main):010:0>

jsvd commented Oct 26, 2015

This is still an issue on 1.7.22

C:\Users\Jsvd>jruby -v
jruby 1.7.22 (1.9.3p551) 2015-08-20 c28f492 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_31-b13 +jit [Windows 7-amd64]

C:\Users\Jsvd>jruby -S irb -EUTF-8
io/console not supported; tty will not be manipulated
irb(main):001:0> puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
UTF-8
Windows-1252
=> nil
irb(main):003:0> s = TCPServer.new(3335)
=> #<TCPServer:fd 15>
irb(main):005:0> c = TCPSocket.new("localhost", 3335)
=> #<TCPSocket:fd 16>
irb(main):006:0> c.peeraddr
=> ["AF_INET", 3335, "127.0.0.1", "127.0.0.1"]
irb(main):007:0> c.peeraddr.first.encoding
=> #<Encoding:Windows-1252>
irb(main):008:0> c.peeraddr.last.encoding
=> #<Encoding:Windows-1252>
irb(main):009:0> "".encoding
=> #<Encoding:UTF-8>
irb(main):010:0>
@jsvd

This comment has been minimized.

Show comment
Hide comment
@jsvd

jsvd Apr 12, 2016

a shout out that this is still an issue in 1.7.25-snapshot as of 12th of april 2016:

C:\projects\logstash>irb -EUTF-8
irb(main):001:0> RUBY_DESCRIPTION
=> "jruby 1.7.25-SNAPSHOT (1.9.3p551) 2016-04-12 5693183 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_65-b17 +jit [Windows 7-amd64]"
irb(main):002:0> puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
UTF-8
Windows-1252
=> nil
irb(main):003:0> s = TCPServer.new(3335); c = TCPSocket.new("localhost", 3335)
=> #<TCPSocket:fd 14>
irb(main):004:0> c.peeraddr
=> ["AF_INET", 3335, "127.0.0.1", "127.0.0.1"]
irb(main):005:0> c.peeraddr.first.encoding
=> #<Encoding:Windows-1252>
irb(main):006:0> c.peeraddr.last.encoding
=> #<Encoding:Windows-1252>
irb(main):007:0> "".encoding
=> #<Encoding:UTF-8>

jsvd commented Apr 12, 2016

a shout out that this is still an issue in 1.7.25-snapshot as of 12th of april 2016:

C:\projects\logstash>irb -EUTF-8
irb(main):001:0> RUBY_DESCRIPTION
=> "jruby 1.7.25-SNAPSHOT (1.9.3p551) 2016-04-12 5693183 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_65-b17 +jit [Windows 7-amd64]"
irb(main):002:0> puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
UTF-8
Windows-1252
=> nil
irb(main):003:0> s = TCPServer.new(3335); c = TCPSocket.new("localhost", 3335)
=> #<TCPSocket:fd 14>
irb(main):004:0> c.peeraddr
=> ["AF_INET", 3335, "127.0.0.1", "127.0.0.1"]
irb(main):005:0> c.peeraddr.first.encoding
=> #<Encoding:Windows-1252>
irb(main):006:0> c.peeraddr.last.encoding
=> #<Encoding:Windows-1252>
irb(main):007:0> "".encoding
=> #<Encoding:UTF-8>
@nirvdrum

This comment has been minimized.

Show comment
Hide comment
@nirvdrum

nirvdrum Apr 13, 2016

Contributor

While I agree that it's a bit confusing, neither Encoding.default_internal nor Encoding.default_external are documented to do anything with sockets. MRI also appears to ignore them for the same socket calls. Changing that behavior in JRuby would then break compatibility with MRI, as far as I can tell.

Contributor

nirvdrum commented Apr 13, 2016

While I agree that it's a bit confusing, neither Encoding.default_internal nor Encoding.default_external are documented to do anything with sockets. MRI also appears to ignore them for the same socket calls. Changing that behavior in JRuby would then break compatibility with MRI, as far as I can tell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment