[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt PATCH 3/3] rpc: use g_new0 instead of VIR_ALLOC



Signed-off-by: Ján Tomko <jtomko redhat com>
---
 src/rpc/virnetclient.c        |  6 ++----
 src/rpc/virnetclientprogram.c | 10 ++++------
 src/rpc/virnetdaemon.c        |  3 +--
 src/rpc/virnetlibsshsession.c |  3 +--
 src/rpc/virnetmessage.c       | 23 +++++++++++++++--------
 src/rpc/virnetserver.c        |  3 +--
 src/rpc/virnetserverclient.c  | 24 ++++++------------------
 src/rpc/virnetserverprogram.c |  6 ++----
 src/rpc/virnetserverservice.c |  9 +++------
 src/rpc/virnetsocket.c        |  3 +--
 src/rpc/virnetsshsession.c    |  8 ++------
 src/rpc/virnettlscontext.c    |  4 +---
 12 files changed, 39 insertions(+), 63 deletions(-)

diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index 8c7c7a0f0a..c6591ecdfc 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -1435,8 +1435,7 @@ virNetClientIOReadMessage(virNetClientPtr client)
     /* Start by reading length word */
     if (client->msg.bufferLength == 0) {
         client->msg.bufferLength = 4;
-        if (VIR_ALLOC_N(client->msg.buffer, client->msg.bufferLength) < 0)
-            return -ENOMEM;
+        client->msg.buffer = g_new0(char, client->msg.bufferLength);
     }
 
     wantData = client->msg.bufferLength - client->msg.bufferOffset;
@@ -2107,8 +2106,7 @@ virNetClientCallNew(virNetMessagePtr msg,
         goto error;
     }
 
-    if (VIR_ALLOC(call) < 0)
-        goto error;
+    call = g_new0(virNetClientCall, 1);
 
     if (virCondInit(&call->cond) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
diff --git a/src/rpc/virnetclientprogram.c b/src/rpc/virnetclientprogram.c
index 4d1a2bc28b..0269f3e6dd 100644
--- a/src/rpc/virnetclientprogram.c
+++ b/src/rpc/virnetclientprogram.c
@@ -247,8 +247,7 @@ int virNetClientProgramDispatch(virNetClientProgramPtr prog,
         return -1;
     }
 
-    if (VIR_ALLOC_N(evdata, event->msg_len) < 0)
-        return -1;
+    evdata = g_new0(char, event->msg_len);
 
     if (virNetMessageDecodePayload(msg, event->msg_filter, evdata) < 0)
         goto cleanup;
@@ -291,8 +290,7 @@ int virNetClientProgramCall(virNetClientProgramPtr prog,
     msg->header.type = noutfds ? VIR_NET_CALL_WITH_FDS : VIR_NET_CALL;
     msg->header.serial = serial;
     msg->header.proc = proc;
-    if (VIR_ALLOC_N(msg->fds, noutfds) < 0)
-        goto error;
+    msg->fds = g_new0(int, noutfds);
     msg->nfds = noutfds;
     for (i = 0; i < msg->nfds; i++)
         msg->fds[i] = -1;
@@ -351,8 +349,8 @@ int virNetClientProgramCall(virNetClientProgramPtr prog,
     case VIR_NET_OK:
         if (infds && ninfds) {
             *ninfds = msg->nfds;
-            if (VIR_ALLOC_N(*infds, *ninfds) < 0)
-                goto error;
+            *infds = g_new0(int, *ninfds);
+
             for (i = 0; i < *ninfds; i++)
                 (*infds)[i] = -1;
             for (i = 0; i < *ninfds; i++) {
diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c
index 2e01244f74..37a5662e04 100644
--- a/src/rpc/virnetdaemon.c
+++ b/src/rpc/virnetdaemon.c
@@ -673,8 +673,7 @@ virNetDaemonAddSignalHandler(virNetDaemonPtr dmn,
     if (VIR_EXPAND_N(dmn->signals, dmn->nsignals, 1) < 0)
         goto error;
 
-    if (VIR_ALLOC(sigdata) < 0)
-        goto error;
+    sigdata = g_new0(virNetDaemonSignal, 1);
 
     sigdata->signum = signum;
     sigdata->func = func;
diff --git a/src/rpc/virnetlibsshsession.c b/src/rpc/virnetlibsshsession.c
index 81646a360a..959a16a6a9 100644
--- a/src/rpc/virnetlibsshsession.c
+++ b/src/rpc/virnetlibsshsession.c
@@ -185,8 +185,7 @@ virNetLibsshSessionAuthMethodNew(virNetLibsshSessionPtr sess)
 {
     virNetLibsshAuthMethodPtr auth;
 
-    if (VIR_ALLOC(auth) < 0)
-        goto error;
+    auth = g_new0(virNetLibsshAuthMethod, 1);
 
     if (VIR_EXPAND_N(sess->auths, sess->nauths, 1) < 0)
         goto error;
diff --git a/src/rpc/virnetmessage.c b/src/rpc/virnetmessage.c
index 6423ce67a3..b728e73f4f 100644
--- a/src/rpc/virnetmessage.c
+++ b/src/rpc/virnetmessage.c
@@ -38,8 +38,7 @@ virNetMessagePtr virNetMessageNew(bool tracked)
 {
     virNetMessagePtr msg;
 
-    if (VIR_ALLOC(msg) < 0)
-        return NULL;
+    msg = g_new0(virNetMessage, 1);
 
     msg->tracked = tracked;
     VIR_DEBUG("msg=%p tracked=%d", msg, tracked);
@@ -328,8 +327,8 @@ int virNetMessageDecodeNumFDs(virNetMessagePtr msg)
 
     if (msg->nfds == 0) {
         msg->nfds = numFDs;
-        if (VIR_ALLOC_N(msg->fds, msg->nfds) < 0)
-            goto cleanup;
+        msg->fds = g_new0(int, msg->nfds);
+
         for (i = 0; i < msg->nfds; i++)
             msg->fds[i] = -1;
     }
@@ -525,15 +524,23 @@ void virNetMessageSaveError(virNetMessageErrorPtr rerr)
     if (verr) {
         rerr->code = verr->code;
         rerr->domain = verr->domain;
-        if (verr->message && VIR_ALLOC(rerr->message) == 0)
+        if (verr->message) {
+            rerr->message = g_new0(char *, 1);
             *rerr->message = g_strdup(verr->message);
+        }
         rerr->level = verr->level;
-        if (verr->str1 && VIR_ALLOC(rerr->str1) == 0)
+        if (verr->str1) {
+            rerr->str1 = g_new0(char *, 1);
             *rerr->str1 = g_strdup(verr->str1);
-        if (verr->str2 && VIR_ALLOC(rerr->str2) == 0)
+        }
+        if (verr->str2) {
+            rerr->str2 = g_new0(char *, 1);
             *rerr->str2 = g_strdup(verr->str2);
-        if (verr->str3 && VIR_ALLOC(rerr->str3) == 0)
+        }
+        if (verr->str3) {
+            rerr->str3 = g_new0(char *, 1);
             *rerr->str3 = g_strdup(verr->str3);
+        }
         rerr->int1 = verr->int1;
         rerr->int2 = verr->int2;
     } else {
diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c
index 79ea9f68a0..fa63acbb09 100644
--- a/src/rpc/virnetserver.c
+++ b/src/rpc/virnetserver.c
@@ -211,8 +211,7 @@ virNetServerDispatchNewMessage(virNetServerClientPtr client,
     if (virThreadPoolGetMaxWorkers(srv->workers) > 0)  {
         virNetServerJobPtr job;
 
-        if (VIR_ALLOC(job) < 0)
-            goto error;
+        job = g_new0(virNetServerJob, 1);
 
         job->client = virObjectRef(client);
         job->msg = msg;
diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c
index 756adcbb4f..4d01e87e21 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -240,8 +240,7 @@ int virNetServerClientAddFilter(virNetServerClientPtr client,
     virNetServerClientFilterPtr *place;
     int ret;
 
-    if (VIR_ALLOC(filter) < 0)
-        return -1;
+    filter = g_new0(virNetServerClientFilter, 1);
 
     virObjectLock(client);
 
@@ -311,10 +310,7 @@ virNetServerClientCheckAccess(virNetServerClientPtr client)
      * (NB. The '\1' byte is sent in an encrypted record).
      */
     confirm->bufferLength = 1;
-    if (VIR_ALLOC_N(confirm->buffer, confirm->bufferLength) < 0) {
-        virNetMessageFree(confirm);
-        return -1;
-    }
+    confirm->buffer = g_new0(char, confirm->bufferLength);
     confirm->bufferOffset = 0;
     confirm->buffer[0] = '\1';
 
@@ -414,8 +410,7 @@ virNetServerClientNewInternal(unsigned long long id,
     if (!(client->rx = virNetMessageNew(true)))
         goto error;
     client->rx->bufferLength = VIR_NET_MESSAGE_LEN_MAX;
-    if (VIR_ALLOC_N(client->rx->buffer, client->rx->bufferLength) < 0)
-        goto error;
+    client->rx->buffer = g_new0(char, client->rx->bufferLength);
     client->nrequests = 1;
 
     PROBE(RPC_SERVER_CLIENT_NEW,
@@ -1306,12 +1301,8 @@ static virNetMessagePtr virNetServerClientDispatchRead(virNetServerClientPtr cli
                 client->wantClose = true;
             } else {
                 client->rx->bufferLength = VIR_NET_MESSAGE_LEN_MAX;
-                if (VIR_ALLOC_N(client->rx->buffer,
-                                client->rx->bufferLength) < 0) {
-                    client->wantClose = true;
-                } else {
-                    client->nrequests++;
-                }
+                client->rx->buffer = g_new0(char, client->rx->bufferLength);
+                client->nrequests++;
             }
         }
         virNetServerClientUpdateEvent(client);
@@ -1411,10 +1402,7 @@ virNetServerClientDispatchWrite(virNetServerClientPtr client)
                     /* Ready to recv more messages */
                     virNetMessageClear(msg);
                     msg->bufferLength = VIR_NET_MESSAGE_LEN_MAX;
-                    if (VIR_ALLOC_N(msg->buffer, msg->bufferLength) < 0) {
-                        virNetMessageFree(msg);
-                        return;
-                    }
+                    msg->buffer = g_new0(char, msg->bufferLength);
                     client->rx = msg;
                     msg = NULL;
                     client->nrequests++;
diff --git a/src/rpc/virnetserverprogram.c b/src/rpc/virnetserverprogram.c
index f8d7fe85ed..3884d62fd8 100644
--- a/src/rpc/virnetserverprogram.c
+++ b/src/rpc/virnetserverprogram.c
@@ -403,10 +403,8 @@ virNetServerProgramDispatchCall(virNetServerProgramPtr prog,
         goto error;
     }
 
-    if (VIR_ALLOC_N(arg, dispatcher->arg_len) < 0)
-        goto error;
-    if (VIR_ALLOC_N(ret, dispatcher->ret_len) < 0)
-        goto error;
+    arg = g_new0(char, dispatcher->arg_len);
+    ret = g_new0(char, dispatcher->ret_len);
 
     if (virNetMessageDecodePayload(msg, dispatcher->arg_filter, arg) < 0)
         goto error;
diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c
index e4165eacb7..317cacf25b 100644
--- a/src/rpc/virnetserverservice.c
+++ b/src/rpc/virnetserverservice.c
@@ -106,8 +106,7 @@ virNetServerServiceNewSocket(virNetSocketPtr *socks,
     if (!(svc = virObjectNew(virNetServerServiceClass)))
         return NULL;
 
-    if (VIR_ALLOC_N(svc->socks, nsocks) < 0)
-        goto error;
+    svc->socks = g_new0(virNetSocketPtr, nsocks);
     svc->nsocks = nsocks;
     for (i = 0; i < svc->nsocks; i++) {
         svc->socks[i] = socks[i];
@@ -230,8 +229,7 @@ virNetServerServicePtr virNetServerServiceNewFDs(int *fds,
     virNetSocketPtr *socks;
     size_t i;
 
-    if (VIR_ALLOC_N(socks, nfds) < 0)
-        goto cleanup;
+    socks = g_new0(virNetSocketPtr, nfds);
 
     for (i = 0; i < nfds; i++) {
         if (virNetSocketNewListenFD(fds[i],
@@ -301,8 +299,7 @@ virNetServerServicePtr virNetServerServiceNewPostExecRestart(virJSONValuePtr obj
     }
 
     n = virJSONValueArraySize(socks);
-    if (VIR_ALLOC_N(svc->socks, n) < 0)
-        goto error;
+    svc->socks = g_new0(virNetSocketPtr, n);
     svc->nsocks = n;
 
     for (i = 0; i < svc->nsocks; i++) {
diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index f79a638775..fc9dd023da 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -1876,8 +1876,7 @@ static ssize_t virNetSocketReadSASL(virNetSocketPtr sock, char *buf, size_t len)
     if (sock->saslDecoded == NULL) {
         ssize_t encodedLen = virNetSASLSessionGetMaxBufSize(sock->saslSession);
         char *encoded;
-        if (VIR_ALLOC_N(encoded, encodedLen) < 0)
-            return -1;
+        encoded = g_new0(char, encodedLen);
         encodedLen = virNetSocketReadWire(sock, encoded, encodedLen);
 
         if (encodedLen <= 0) {
diff --git a/src/rpc/virnetsshsession.c b/src/rpc/virnetsshsession.c
index bb6e137939..4b56363fa0 100644
--- a/src/rpc/virnetsshsession.c
+++ b/src/rpc/virnetsshsession.c
@@ -173,8 +173,7 @@ virNetSSHSessionAuthMethodNew(virNetSSHSessionPtr sess)
 {
     virNetSSHAuthMethodPtr auth;
 
-    if (VIR_ALLOC(auth) < 0)
-        goto error;
+    auth = g_new0(virNetSSHAuthMethod, 1);
 
     if (VIR_EXPAND_N(sess->auths, sess->nauths, 1) < 0)
         goto error;
@@ -223,10 +222,7 @@ virNetSSHKbIntCb(const char *name G_GNUC_UNUSED,
         return;
     }
 
-    if (VIR_ALLOC_N(askcred, num_prompts) < 0) {
-        priv->authCbErr = VIR_NET_SSH_AUTHCB_OOM;
-        return;
-    }
+    askcred = g_new0(virConnectCredential, num_prompts);
 
     /* fill data structures for auth callback */
     for (i = 0; i < num_prompts; i++) {
diff --git a/src/rpc/virnettlscontext.c b/src/rpc/virnettlscontext.c
index 37564db14e..84f7e63e4d 100644
--- a/src/rpc/virnettlscontext.c
+++ b/src/rpc/virnettlscontext.c
@@ -299,9 +299,7 @@ static int virNetTLSContextCheckCertKeyPurpose(gnutls_x509_crt_t cert,
             return -1;
         }
 
-        if (VIR_ALLOC_N(buffer, size) < 0)
-            return -1;
-
+        buffer = g_new0(char, size);
         status = gnutls_x509_crt_get_key_purpose_oid(cert, i, buffer, &size, &purposeCritical);
         if (status < 0) {
             VIR_FREE(buffer);
-- 
2.26.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]