From 54521b22e01f52e6e112b33bbca8b2255915dd40 Mon Sep 17 00:00:00 2001 From: Jan Lamecki Date: Fri, 30 Sep 2022 18:34:05 +0200 Subject: [PATCH] client: remove trailing null from unix abstract socket address (#5678) --- examples/features/unix_abstract/server/main.go | 2 +- internal/resolver/unix/unix.go | 5 +++-- test/authority_test.go | 8 +++----- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/features/unix_abstract/server/main.go b/examples/features/unix_abstract/server/main.go index 4ef4ff5b7637..7013466b4917 100644 --- a/examples/features/unix_abstract/server/main.go +++ b/examples/features/unix_abstract/server/main.go @@ -51,7 +51,7 @@ func (s *ecServer) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.Echo func main() { flag.Parse() netw := "unix" - socketAddr := fmt.Sprintf("\x00%v", *addr) + socketAddr := fmt.Sprintf("@%v", *addr) lis, err := net.Listen(netw, socketAddr) if err != nil { log.Fatalf("net.Listen(%q, %q) failed: %v", netw, socketAddr, err) diff --git a/internal/resolver/unix/unix.go b/internal/resolver/unix/unix.go index 20852e59df29..7f1a702cacbe 100644 --- a/internal/resolver/unix/unix.go +++ b/internal/resolver/unix/unix.go @@ -49,8 +49,9 @@ func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, _ resolv } addr := resolver.Address{Addr: endpoint} if b.scheme == unixAbstractScheme { - // prepend "\x00" to address for unix-abstract - addr.Addr = "\x00" + addr.Addr + // We can not prepend \0 as c++ gRPC does, as in Golang '@' is used to signify we do + // not want trailing \0 in address. + addr.Addr = "@" + addr.Addr } cc.UpdateState(resolver.State{Addresses: []resolver.Address{networktype.Set(addr, "unix")}}) return &nopResolver{}, nil diff --git a/test/authority_test.go b/test/authority_test.go index 452b896eebf9..8148cd2347ea 100644 --- a/test/authority_test.go +++ b/test/authority_test.go @@ -125,10 +125,10 @@ var authorityTests = []authorityTest{ }, { name: "UnixAbstract", - address: "\x00abc efg", + address: "@abc efg", target: "unix-abstract:abc efg", authority: "localhost", - dialTargetWant: "\x00abc efg", + dialTargetWant: "unix:@abc efg", }, } @@ -155,9 +155,7 @@ func (s) TestUnixCustomDialer(t *testing.T) { if address != test.dialTargetWant { return nil, fmt.Errorf("expected target %v in custom dialer, instead got %v", test.dialTargetWant, address) } - if !strings.HasPrefix(test.target, "unix-abstract:") { - address = address[len("unix:"):] - } + address = address[len("unix:"):] return (&net.Dialer{}).DialContext(ctx, "unix", address) } runUnixTest(t, test.address, test.target, test.authority, dialer)