From ee47d23629375a5386c7eea23abe644f2799d16d Mon Sep 17 00:00:00 2001 From: Matthias Bolte Date: Thu, 10 Jan 2013 22:32:13 +0100 Subject: [PATCH] esx: Simplify esxPrivate pointer handling in esxOpen Commit 4445e16bfa8056980ac643fabf17186f9e685925 changed the signature of esxConnectToHost and esxConnectToVCenter by replacing the esxPrivate pointer with virConnectPtr. The esxPrivate pointer was then retrieved again from virConnectPtr's privateData. This resulted in a NULL pointer dereference, because the privateData pointer was not yet initialized at the point where esxConnectToHost and esxConnectToVCenter are called. This was fixed in commit b126715a48cd0cbe32ec6468c267cd8cf2961c55 that moved the initialization of privateData before the problematic calls. Simplify the logic by making the call to esxFreePrivate unconditional and changing esxConnectToHost and esxConnectToVCenter back to take a esxPrivate pointer directly. This allows to assign esxPrivate to the virConnectPtr's privateData pointer as one of the last steps in esxOpen making it more obvious that it is not initialized during the earlier steps of esxOpen. --- src/esx/esx_driver.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c index 1366c81379..77f463cbc5 100644 --- a/src/esx/esx_driver.c +++ b/src/esx/esx_driver.c @@ -3,7 +3,7 @@ * esx_driver.c: core driver functions for managing VMware ESX hosts * * Copyright (C) 2010-2012 Red Hat, Inc. - * Copyright (C) 2009-2012 Matthias Bolte + * Copyright (C) 2009-2013 Matthias Bolte * Copyright (C) 2009 Maximilian Wilhelm * * This library is free software; you can redistribute it and/or @@ -650,7 +650,8 @@ esxCapsInit(esxPrivate *priv) static int -esxConnectToHost(virConnectPtr conn, +esxConnectToHost(esxPrivate *priv, + virConnectPtr conn, virConnectAuthPtr auth, char **vCenterIpAddress) { @@ -663,7 +664,6 @@ esxConnectToHost(virConnectPtr conn, esxVI_String *propertyNameList = NULL; esxVI_ObjectContent *hostSystem = NULL; esxVI_Boolean inMaintenanceMode = esxVI_Boolean_Undefined; - esxPrivate *priv = conn->privateData; esxVI_ProductVersion expectedProductVersion = STRCASEEQ(conn->uri->scheme, "esx") ? esxVI_ProductVersion_ESX : esxVI_ProductVersion_GSX; @@ -785,7 +785,8 @@ esxConnectToHost(virConnectPtr conn, static int -esxConnectToVCenter(virConnectPtr conn, +esxConnectToVCenter(esxPrivate *priv, + virConnectPtr conn, virConnectAuthPtr auth, const char *hostname, const char *hostSystemIpAddress) @@ -796,7 +797,6 @@ esxConnectToVCenter(virConnectPtr conn, char *unescapedPassword = NULL; char *password = NULL; char *url = NULL; - esxPrivate *priv = conn->privateData; if (hostSystemIpAddress == NULL && (priv->parsedUri->path == NULL || STREQ(priv->parsedUri->path, "/"))) { @@ -1008,8 +1008,6 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, priv->supportsLongMode = esxVI_Boolean_Undefined; priv->usedCpuTimeCounterId = -1; - conn->privateData = priv; - /* * Set the port dependent on the transport protocol if no port is * specified. This allows us to rely on the port parameter being @@ -1036,7 +1034,7 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, if (STRCASEEQ(conn->uri->scheme, "esx") || STRCASEEQ(conn->uri->scheme, "gsx")) { /* Connect to host */ - if (esxConnectToHost(conn, auth, + if (esxConnectToHost(priv, conn, auth, &potentialVCenterIpAddress) < 0) { goto cleanup; } @@ -1075,7 +1073,7 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, } } - if (esxConnectToVCenter(conn, auth, + if (esxConnectToVCenter(priv, conn, auth, vCenterIpAddress, priv->host->ipAddress) < 0) { goto cleanup; @@ -1085,7 +1083,7 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, priv->primary = priv->host; } else { /* VPX */ /* Connect to vCenter */ - if (esxConnectToVCenter(conn, auth, + if (esxConnectToVCenter(priv, conn, auth, conn->uri->server, NULL) < 0) { goto cleanup; @@ -1101,13 +1099,12 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, goto cleanup; } + conn->privateData = priv; + priv = NULL; result = VIR_DRV_OPEN_SUCCESS; cleanup: - if (result == VIR_DRV_OPEN_ERROR) { - esxFreePrivate(&priv); - } - + esxFreePrivate(&priv); VIR_FREE(potentialVCenterIpAddress); return result; -- GitLab