Skip to content

Fix 14611 - socket.localAddress fails on Unix sockets with longer path (> 13 characters) #3300

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

Merged
merged 4 commits into from
May 22, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions std/socket.d
Original file line number Diff line number Diff line change
Expand Up @@ -1921,45 +1921,47 @@ static if (is(sockaddr_un))
class UnixAddress: Address
{
protected:
sockaddr_un* sun;
socklen_t len;

struct
{
align (1):
sockaddr_un sun;
char unused = '\0'; // placeholder for a terminating '\0'
}

this() pure nothrow @nogc
{
sun.sun_family = AF_UNIX;
sun.sun_path = '?';
}


public:
override @property sockaddr* name()
{
return cast(sockaddr*)sun;
return cast(sockaddr*)&sun;
}

override @property const(sockaddr)* name() const
{
return cast(const(sockaddr)*)sun;
return cast(const(sockaddr)*)&sun;
}


override @property socklen_t nameLen() const
override @property socklen_t nameLen() @trusted const
{
return len;
return cast(socklen_t) (sockaddr_un.init.sun_path.offsetof +
strlen(cast(const(char*)) sun.sun_path.ptr) + 1);
}


this(in char[] path) @trusted pure nothrow
this(in char[] path) @trusted pure
{
len = cast(socklen_t)(sockaddr_un.init.sun_path.offsetof + path.length + 1);
sun = cast(sockaddr_un*) (new ubyte[len]).ptr;
enforce(path.length <= sun.sun_path.sizeof, new SocketParameterException("Path too long"));
sun.sun_family = AF_UNIX;
sun.sun_path.ptr[0..path.length] = (cast(byte[]) path)[];
sun.sun_path.ptr[path.length] = 0;
}

@property string path() const pure
@property string path() @trusted const pure
{
return to!string(sun.sun_path.ptr);
return to!string(cast(const(char)*)sun.sun_path.ptr);
}

override string toString() const pure
Expand All @@ -1984,6 +1986,7 @@ static if (is(sockaddr_un))

listener.bind(address);
scope(exit) () @trusted { remove(name.tempCString()); } ();
assert(listener.localAddress.toString == name);

listener.listen(1);

Expand Down Expand Up @@ -3405,6 +3408,13 @@ public:
Address result;
switch(_family)
{
static if (is(sockaddr_un))
{
case AddressFamily.UNIX:
result = new UnixAddress;
break;
}

case AddressFamily.INET:
result = new InternetAddress;
break;
Expand Down