From nobody Tue May 7 11:02:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1634301639; cv=none; d=zohomail.com; s=zohoarc; b=k/fi4WNz0t/sRSZIqvOtV2jeWCBTNaqOYnQMVVUN0D0Oz1EYtuAv06/PvMrlnmvWKwYw0xbDffB7FXMUvelfUZ1yvE5Xicdd7+AYaRBxvF55FExJ6oOoeiYSRQ8NhryKql6fag7Lyz9CWwGWma4U3EaKnl+3/eAB0PbWviUqXGE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1634301639; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=OJ9mdzoZ+4NsUhpwgSEClAPjaJiCCrbjcvEjB4+dAaY=; b=bRus1rH9nGyGkLk//2Lli6CfHGOkEmcXi3E38WfQ55c1Pu5gx7yZa7NAKQs0PpGhW458fLiGURG+fxVe0E8RZ/XAlRBqUGsMuRTEOzGFm7mCmTWesjpIXYXOULzKMbHwrJGTPvGgkehid+RlEmgc5FQDptLyAq4haiU6rriYFHA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 163430163954212.266497622734391; Fri, 15 Oct 2021 05:40:39 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-363-kW2oG0YcOeCWVvkeHA6PFg-1; Fri, 15 Oct 2021 08:40:34 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D6B7718125C0; Fri, 15 Oct 2021 12:40:29 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 15C4957CD0; Fri, 15 Oct 2021 12:40:29 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 908174A703; Fri, 15 Oct 2021 12:40:28 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 19FCWPEO016781 for ; Fri, 15 Oct 2021 08:32:25 -0400 Received: by smtp.corp.redhat.com (Postfix) id BF7CC196E5; Fri, 15 Oct 2021 12:32:25 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id 48E2219736 for ; Fri, 15 Oct 2021 12:32:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634301638; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=OJ9mdzoZ+4NsUhpwgSEClAPjaJiCCrbjcvEjB4+dAaY=; b=bO65M5j+oDv5/8t3HYXzVc7v1Gp9kIfSjPGyP9pBEVhR0Cb0t+skv5lalPIOcxXbJ6hPJO f+XMxWTxyxiq632Ggi9Hj8WtQLelwEya6JLuAJY180cY/HP+IggO6UdgV+PWp7pjemWa1J rTXZMYX1SScAiAvWsKZ73i+VbQSlBx4= X-MC-Unique: kW2oG0YcOeCWVvkeHA6PFg-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 1/2] virnetsocket: Don't free virCommand in virNetSocketNewConnectCommand() Date: Fri, 15 Oct 2021 14:32:07 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1634301641124100001 Content-Type: text/plain; charset="utf-8" The aim of virNetSocketNewConnectCommand() is to execute passed command and attach socket pair/pipe to it so that client socket can be opened (this is used for connections with alternative transports, e.g. ssh). The virCommand is created in a caller and then passed to virNetSocketNewConnectCommand() where it is freed using virCommandFree(). This approach is wrong on two levels: 1) The deallocation happens on a different level than allocation, 2) There's a WIN32 stub that just reports an error and doesn't free the command. However, with g_autoptr() trickery the command can be freed in caller. Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/rpc/virnetsocket.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 212089520d..50c0c4ecc8 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -821,8 +821,6 @@ int virNetSocketNewConnectCommand(virCommand *cmd, if (!(*retsock =3D virNetSocketNew(NULL, NULL, true, sv[0], errfd[0], = pid, false))) goto error; =20 - virCommandFree(cmd); - return 0; =20 error: @@ -832,7 +830,6 @@ int virNetSocketNewConnectCommand(virCommand *cmd, VIR_FORCE_CLOSE(errfd[1]); =20 virCommandAbort(cmd); - virCommandFree(cmd); =20 return -1; } @@ -856,7 +853,7 @@ int virNetSocketNewConnectSSH(const char *nodename, const char *command, virNetSocket **retsock) { - virCommand *cmd; + g_autoptr(virCommand) cmd =3D NULL; =20 *retsock =3D NULL; =20 @@ -1154,7 +1151,7 @@ virNetSocketNewConnectLibssh(const char *host G_GNUC_= UNUSED, int virNetSocketNewConnectExternal(const char **cmdargv, virNetSocket **retsock) { - virCommand *cmd; + g_autoptr(virCommand) cmd =3D NULL; =20 *retsock =3D NULL; =20 --=20 2.32.0 From nobody Tue May 7 11:02:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1634301644; cv=none; d=zohomail.com; s=zohoarc; b=F3208tWAQEqS26BC0X0+FSmdKmxeeUOxENFN3GhgH3G4DFqdfLMOdSIz3D3tU23KP2aFZaBUnusukh4+Af9MShXCl6M8uiwppVW4tJOQxf/PgZlyJqav01wDy3JhhMBwUZmHj9z/rC1CTmbOPT70X7F1rI2VKIKGIhfM9PVklEE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1634301644; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ran6w/Pc5owteZKf7wQGFgb1ogBlMxf6vMLVi+jOc1g=; b=aq/sGRFc1kQKtC03uyuC6v+oQeCWx16TdcVwWBHY505nrk/AP/oOLG8OBDeXW/WPYsJou2lZJjqzqW2GBj8uMbzfKitAxG1PG2Sbm+qN0ZjGnqQgNRaA3TMufj7nMIJjBlhg/rbZiP3hXGQFlglFq4IzOa8wAW0OIl7mXoes5jg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1634301644824143.46671409824683; Fri, 15 Oct 2021 05:40:44 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-293-29Fkz3ooOLOFre0KBWlHQQ-1; Fri, 15 Oct 2021 08:40:40 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4F43710B7440; Fri, 15 Oct 2021 12:40:35 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 088BA60583; Fri, 15 Oct 2021 12:40:35 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id C2073180598A; Fri, 15 Oct 2021 12:40:34 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 19FCWQAj016786 for ; Fri, 15 Oct 2021 08:32:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9119319736; Fri, 15 Oct 2021 12:32:26 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id 19006196E5 for ; Fri, 15 Oct 2021 12:32:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634301643; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=ran6w/Pc5owteZKf7wQGFgb1ogBlMxf6vMLVi+jOc1g=; b=WY7a7i8qL4FOQn51BZlZ0P3msX1eYcGBIFJ37Kg7YZT14gM0z854hJMRYU7ZkhvLM2Hizu FS2BpEnTsszsYR4kIXS8QwHI9W7a806QykrJgsGfnlVRwTHupNNzmYqVP3fl9PPBVcmd25 jn6dYJSYHAu5LuX7kB3wg0M6rnh8hqo= X-MC-Unique: 29Fkz3ooOLOFre0KBWlHQQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 2/2] virnetsocket: Use g_auto* more Date: Fri, 15 Oct 2021 14:32:08 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1634301645848100001 Content-Type: text/plain; charset="utf-8" There are few functions in virnetsocket.c where an object/memory is freed by explicit call. Use g_autoptr()/g_autofree/VIR_AUTOCLOSE to do that automatically. Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/rpc/virnetsocket.c | 57 +++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 50c0c4ecc8..bf931e5f59 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -124,10 +124,9 @@ VIR_ONCE_GLOBAL_INIT(virNetSocket); #ifndef WIN32 static int virNetSocketForkDaemon(const char *binary) { - int ret; - virCommand *cmd =3D virCommandNewArgList(binary, - "--timeout=3D120", - NULL); + g_autoptr(virCommand) cmd =3D virCommandNewArgList(binary, + "--timeout=3D120", + NULL); =20 virCommandAddEnvPassCommon(cmd); virCommandAddEnvPass(cmd, "XDG_CACHE_HOME"); @@ -135,9 +134,7 @@ static int virNetSocketForkDaemon(const char *binary) virCommandAddEnvPass(cmd, "XDG_RUNTIME_DIR"); virCommandClearCaps(cmd); virCommandDaemonize(cmd); - ret =3D virCommandRun(cmd, NULL); - virCommandFree(cmd); - return ret; + return virCommandRun(cmd, NULL); } #endif =20 @@ -234,7 +231,7 @@ virNetSocketNew(virSocketAddr *localAddr, pid_t pid, bool unlinkUNIX) { - virNetSocket *sock; + g_autoptr(virNetSocket) sock =3D NULL; int no_slow_start =3D 1; =20 if (virNetSocketInitialize() < 0) @@ -300,11 +297,10 @@ virNetSocketNew(virSocketAddr *localAddr, sock, fd, errfd, (long long)pid, NULLSTR(sock->localAddrStrSASL), NULLSTR(sock->remoteAddrStrSASL= )); =20 - return sock; + return g_steal_pointer(&sock); =20 error: sock->fd =3D sock->errfd =3D -1; /* Caller owns fd/errfd on failure */ - virObjectUnref(sock); return NULL; } =20 @@ -667,13 +663,13 @@ int virNetSocketNewConnectUNIX(const char *path, const char *spawnDaemonPath, virNetSocket **retsock) { - char *lockpath =3D NULL; - int lockfd =3D -1; + g_autofree char *lockpath =3D NULL; + VIR_AUTOCLOSE lockfd =3D -1; int fd =3D -1; int retries =3D 500; virSocketAddr localAddr; virSocketAddr remoteAddr; - char *rundir =3D NULL; + g_autofree char *rundir =3D NULL; int ret =3D -1; bool daemonLaunched =3D false; =20 @@ -762,10 +758,7 @@ int virNetSocketNewConnectUNIX(const char *path, cleanup: if (lockfd !=3D -1) { unlink(lockpath); - VIR_FORCE_CLOSE(lockfd); } - VIR_FREE(lockpath); - VIR_FREE(rundir); =20 if (ret < 0 && fd !=3D -1) closesocket(fd); @@ -898,7 +891,7 @@ virNetSocketNewConnectLibSSH2(const char *host, virURI *uri, virNetSocket **retsock) { - virNetSocket *sock =3D NULL; + g_autoptr(virNetSocket) sock =3D NULL; virNetSSHSession *sess =3D NULL; unsigned int verify; int ret =3D -1; @@ -985,12 +978,11 @@ virNetSocketNewConnectLibSSH2(const char *host, goto error; =20 sock->sshSession =3D sess; - *retsock =3D sock; + *retsock =3D g_steal_pointer(&sock); =20 return 0; =20 error: - virObjectUnref(sock); virObjectUnref(sess); return ret; } @@ -1030,7 +1022,7 @@ virNetSocketNewConnectLibssh(const char *host, virURI *uri, virNetSocket **retsock) { - virNetSocket *sock =3D NULL; + g_autoptr(virNetSocket) sock =3D NULL; virNetLibsshSession *sess =3D NULL; unsigned int verify; int ret =3D -1; @@ -1118,12 +1110,11 @@ virNetSocketNewConnectLibssh(const char *host, * trying to close an invalid FD). */ sock->ownsFd =3D false; - *retsock =3D sock; + *retsock =3D g_steal_pointer(&sock); =20 return 0; =20 error: - virObjectUnref(sock); virObjectUnref(sess); return ret; } @@ -1238,7 +1229,7 @@ virNetSocket *virNetSocketNewPostExecRestart(virJSONV= alue *object) =20 virJSONValue *virNetSocketPreExecRestart(virNetSocket *sock) { - virJSONValue *object =3D NULL; + g_autoptr(virJSONValue) object =3D NULL; =20 virObjectLock(sock); =20 @@ -1287,11 +1278,10 @@ virJSONValue *virNetSocketPreExecRestart(virNetSock= et *sock) } =20 virObjectUnlock(sock); - return object; + return g_steal_pointer(&object); =20 error: virObjectUnlock(sock); - virJSONValueFree(object); return NULL; } =20 @@ -1740,7 +1730,7 @@ bool virNetSocketHasPendingData(virNetSocket *sock G_= GNUC_UNUSED) =20 static ssize_t virNetSocketReadWire(virNetSocket *sock, char *buf, size_t = len) { - char *errout =3D NULL; + g_autofree char *errout =3D NULL; ssize_t ret; =20 #if WITH_SSH2 @@ -1804,7 +1794,6 @@ static ssize_t virNetSocketReadWire(virNetSocket *soc= k, char *buf, size_t len) } } =20 - VIR_FREE(errout); return ret; } =20 @@ -1859,22 +1848,18 @@ static ssize_t virNetSocketReadSASL(virNetSocket *s= ock, char *buf, size_t len) /* Need to read some more data off the wire */ if (sock->saslDecoded =3D=3D NULL) { ssize_t encodedLen =3D virNetSASLSessionGetMaxBufSize(sock->saslSe= ssion); - char *encoded; - encoded =3D g_new0(char, encodedLen); + g_autofree char *encoded =3D g_new0(char, encodedLen); + encodedLen =3D virNetSocketReadWire(sock, encoded, encodedLen); =20 - if (encodedLen <=3D 0) { - VIR_FREE(encoded); + if (encodedLen <=3D 0) return encodedLen; - } =20 if (virNetSASLSessionDecode(sock->saslSession, encoded, encodedLen, &sock->saslDecoded, &sock->saslDecoded= Length) < 0) { - VIR_FREE(encoded); return -1; } - VIR_FREE(encoded); =20 sock->saslDecodedOffset =3D 0; } @@ -2136,7 +2121,7 @@ static void virNetSocketEventHandle(int watch G_GNUC_= UNUSED, =20 static void virNetSocketEventFree(void *opaque) { - virNetSocket *sock =3D opaque; + g_autoptr(virNetSocket) sock =3D opaque; virFreeCallback ff; void *eopaque; =20 @@ -2149,8 +2134,6 @@ static void virNetSocketEventFree(void *opaque) =20 if (ff) ff(eopaque); - - virObjectUnref(sock); } =20 int virNetSocketAddIOCallback(virNetSocket *sock, --=20 2.32.0