From 3a5501c1595dd32c25c9df6acb377aa92037b49f Mon Sep 17 00:00:00 2001
From: Sebastian Lackner <sebastian@fds-team.de>
Date: Mon, 17 Aug 2015 01:11:47 +0200
Subject: server: Store a reference to the parent object for pipe servers. (v2)

---
 server/named_pipe.c | 11 +++++++----
 server/object.c     | 31 ++++++++++++++++++++++---------
 2 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/server/named_pipe.c b/server/named_pipe.c
index 49b3f62..36a5c78 100644
--- a/server/named_pipe.c
+++ b/server/named_pipe.c
@@ -765,9 +765,10 @@ static struct pipe_server *get_pipe_server_obj( struct process *process,
 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
                                                unsigned int pipe_flags )
 {
+    static const struct unicode_str str = { NULL, 0 };
     struct pipe_server *server;
 
-    server = alloc_object( &pipe_server_ops );
+    server = create_object( NULL, &pipe_server_ops, &str, &pipe->obj );
     if (!server)
         return NULL;
 
@@ -789,11 +790,13 @@ static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned
     return server;
 }
 
-static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags )
+static struct pipe_client *create_pipe_client( struct named_pipe *pipe, unsigned int flags,
+                                               unsigned int pipe_flags )
 {
+    static const struct unicode_str str = { NULL, 0 };
     struct pipe_client *client;
 
-    client = alloc_object( &pipe_client_ops );
+    client = create_object( NULL, &pipe_client_ops, &str, &pipe->obj );
     if (!client)
         return NULL;
 
@@ -879,7 +882,7 @@ static struct object *named_pipe_open_file( struct object *obj, unsigned int acc
         return NULL;
     }
 
-    if ((client = create_pipe_client( options, pipe->flags )))
+    if ((client = create_pipe_client( pipe, options, pipe->flags )))
     {
         type = ((pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && is_messagemode_supported()) ?
                SOCK_SEQPACKET : SOCK_STREAM;
diff --git a/server/object.c b/server/object.c
index a1b1184..fd24722 100644
--- a/server/object.c
+++ b/server/object.c
@@ -55,6 +55,7 @@ struct namespace
     struct list         names[1];        /* array of hash entry lists */
 };
 
+static struct list dummy_namespace = LIST_INIT(dummy_namespace);
 
 #ifdef DEBUG_OBJECTS
 static struct list object_list = LIST_INIT(object_list);
@@ -157,9 +158,15 @@ static void free_name( struct object *obj )
 static void set_object_name( struct namespace *namespace,
                              struct object *obj, struct object_name *ptr )
 {
-    int hash = get_name_hash( namespace, ptr->name, ptr->len );
-
-    list_add_head( &namespace->names[hash], &ptr->entry );
+    if (namespace)
+    {
+        int hash = get_name_hash( namespace, ptr->name, ptr->len );
+        list_add_head( &namespace->names[hash], &ptr->entry );
+    }
+    else
+    {
+        list_add_tail( &dummy_namespace, &ptr->entry );
+    }
     ptr->obj = obj;
     obj->name = ptr;
 }
@@ -184,7 +191,7 @@ WCHAR *get_object_full_name( struct object *obj, data_size_t *ret_len )
     while (ptr && ptr->name)
     {
         struct object_name *name = ptr->name;
-        len += name->len + sizeof(WCHAR);
+        if (name->len) len += name->len + sizeof(WCHAR);
         ptr = name->parent;
     }
     if (!len) return NULL;
@@ -194,9 +201,12 @@ WCHAR *get_object_full_name( struct object *obj, data_size_t *ret_len )
     while (obj && obj->name)
     {
         struct object_name *name = obj->name;
-        memcpy( ret + len - name->len, name->name, name->len );
-        len -= name->len + sizeof(WCHAR);
-        memcpy( ret + len, &backslash, sizeof(WCHAR) );
+        if (name->len)
+        {
+            memcpy( ret + len - name->len, name->name, name->len );
+            len -= name->len + sizeof(WCHAR);
+            memcpy( ret + len, &backslash, sizeof(WCHAR) );
+        }
         obj = name->parent;
     }
     return (WCHAR *)ret;
@@ -276,8 +286,11 @@ static void dump_name( struct object *obj )
 
     if (!name) return;
     if (name->parent) dump_name( name->parent );
-    fputs( "\\\\", stderr );
-    dump_strW( name->name, name->len / sizeof(WCHAR), stderr, "[]" );
+    if (name->len)
+    {
+        fputs( "\\\\", stderr );
+        dump_strW( name->name, name->len / sizeof(WCHAR), stderr, "[]" );
+    }
 }
 
 /* dump the name of an object to stderr */
-- 
2.6.4

