[lxc-devel] [lxcfs/master] bindings: initialize safely

brauner on Github lxc-bot at linuxcontainers.org
Wed Feb 26 15:25:37 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 365 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200226/d45f3877/attachment.bin>
-------------- next part --------------
From 1e5d03fed00c80cc644406bf41f71437fcbeb0d0 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Wed, 26 Feb 2020 16:24:44 +0100
Subject: [PATCH] bindings: initialize safely

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 bindings.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/bindings.c b/bindings.c
index f5a6415..8a84a5d 100644
--- a/bindings.c
+++ b/bindings.c
@@ -235,7 +235,7 @@ static void prune_initpid_store(void)
 /* Must be called under store_lock */
 static void save_initpid(struct stat *sb, pid_t pid)
 {
-	__do_free struct pidns_init_store *e = NULL;
+	__do_free struct pidns_init_store *entry = NULL;
 	__do_close_prot_errno int pidfd = -EBADF;
 	char path[LXCFS_PROC_PID_LEN];
 	struct lxcfs_opts *opts = fuse_get_context()->private_data;
@@ -252,18 +252,20 @@ static void save_initpid(struct stat *sb, pid_t pid)
 	if (stat(path, &st))
 		return;
 
-	e = malloc(sizeof(*e));
-	if (!e)
+	entry = malloc(sizeof(*entry));
+	if (entry)
 		return;
 
-	e->ino = sb->st_ino;
-	e->initpid = pid;
-	e->ctime = st.st_ctime;
-	ino_hash = HASH(e->ino);
-	e->next = pidns_hash_table[ino_hash];
-	e->lastcheck = time(NULL);
-	e->init_pidfd = move_fd(pidfd);
-	pidns_hash_table[ino_hash] = move_ptr(e);
+	ino_hash = HASH(entry->ino);
+	*entry = (struct pidns_init_store){
+		.ino		= sb->st_ino,
+		.initpid	= pid,
+		.ctime		= st.st_ctime,
+		.next		= pidns_hash_table[ino_hash],
+		.lastcheck	= time(NULL),
+		.init_pidfd	= move_fd(pidfd),
+	};
+	pidns_hash_table[ino_hash] = move_ptr(entry);
 
 	lxcfs_debug("Added cache entry %d for pid %d to init pid cache", ino_hash, pid);
 }


More information about the lxc-devel mailing list