Skip to content
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

UNIXSocket#path does not handle null path correctly #8092

Open
headius opened this issue Feb 7, 2024 · 0 comments
Open

UNIXSocket#path does not handle null path correctly #8092

headius opened this issue Feb 7, 2024 · 0 comments

Comments

@headius
Copy link
Member

headius commented Feb 7, 2024

Found during spec update in #8087.

When the path for a UNIXSocket (or UNIXServer) is null, attempting to retrieve the path will raise a Java error. The error is due to us not checking for null before we attempt to create the string, but the proper handling would be to use getsockname syscall against the file descriptor to get the original path.

This breaks the UNIXServer#for_fd spec, because the newly-created socket does not inherit whatever path the original file descriptor was opened against.

This probably should be fixed in jnr-unixsocket, but a workaround in JRuby would be to bind getsockname and use it like CRuby does in the implementation of UNIXSocket#path.

A rough patch shows the general change needed, but without sockaddr_in and getsockname it can't be completed currently:

diff --git a/core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java b/core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java
index a936898a4d..6c288cf4ce 100644
--- a/core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java
+++ b/core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java
@@ -156,11 +156,6 @@ public class RubyUNIXServer extends RubyUNIXSocket {
         return context.runtime.newFixnum(((UnixSocketChannel) socket.getChannel()).getFD());
     }
 
-    @JRubyMethod
-    public IRubyObject path(ThreadContext context) {
-        return context.runtime.newString(openFile.getPath());
-    }
-
     @JRubyMethod
     public IRubyObject addr(ThreadContext context) {
         Ruby runtime = context.runtime;
diff --git a/core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java b/core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java
index 0c8577739c..0b26d8f51f 100644
--- a/core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java
+++ b/core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java
@@ -103,7 +103,13 @@ public class RubyUNIXSocket extends RubyBasicSocket {
 
     @JRubyMethod
     public IRubyObject path(ThreadContext context) {
-        return RubyString.newEmptyString(context.runtime);
+        String path = openFile.getPath();
+
+        if (path == null) {
+            // cruby does getsockname here to get the name from fileno
+        }
+
+        return context.runtime.newString(path);
     }
 
     @JRubyMethod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant