mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
phyp: Remove stack allocating a 4kb volume key and fix related memory leaks
Don't pre-allocate 4kb per key, make phypVolumeGetKey allocate the memory. Make phypBuildVolume return the volume key instead of using pre-allocated memory to store it. Also fix a memory leak in phypVolumeLookupByName when phypVolumeGetKey fails. Fix another memory leak in phypVolumeLookupByPath in the success path. Fix phypVolumeGetXMLDesc leaking voldef.key.
This commit is contained in:
parent
4bfab4a0d3
commit
444fd07a0a
@ -2156,8 +2156,8 @@ phypAttachDevice(virDomainPtr domain, const char *xml)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static char *
|
||||||
phypVolumeGetKey(virConnectPtr conn, char *key, const char *name)
|
phypVolumeGetKey(virConnectPtr conn, const char *name)
|
||||||
{
|
{
|
||||||
ConnectionData *connection_data = conn->networkPrivateData;
|
ConnectionData *connection_data = conn->networkPrivateData;
|
||||||
phyp_driverPtr phyp_driver = conn->privateData;
|
phyp_driverPtr phyp_driver = conn->privateData;
|
||||||
@ -2185,10 +2185,10 @@ phypVolumeGetKey(virConnectPtr conn, char *key, const char *name)
|
|||||||
if (virBufferError(&buf)) {
|
if (virBufferError(&buf)) {
|
||||||
virBufferFreeAndReset(&buf);
|
virBufferFreeAndReset(&buf);
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
cmd = virBufferContentAndReset(&buf);
|
|
||||||
|
|
||||||
|
cmd = virBufferContentAndReset(&buf);
|
||||||
ret = phypExec(session, cmd, &exit_status, conn);
|
ret = phypExec(session, cmd, &exit_status, conn);
|
||||||
|
|
||||||
if (exit_status < 0 || ret == NULL)
|
if (exit_status < 0 || ret == NULL)
|
||||||
@ -2199,17 +2199,13 @@ phypVolumeGetKey(virConnectPtr conn, char *key, const char *name)
|
|||||||
if (char_ptr)
|
if (char_ptr)
|
||||||
*char_ptr = '\0';
|
*char_ptr = '\0';
|
||||||
|
|
||||||
if (memcpy(key, ret, MAX_KEY_SIZE) == NULL)
|
|
||||||
goto err;
|
|
||||||
|
|
||||||
VIR_FREE(cmd);
|
VIR_FREE(cmd);
|
||||||
VIR_FREE(ret);
|
return ret;
|
||||||
return 0;
|
|
||||||
|
|
||||||
err:
|
err:
|
||||||
VIR_FREE(cmd);
|
VIR_FREE(cmd);
|
||||||
VIR_FREE(ret);
|
VIR_FREE(ret);
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
@ -2316,9 +2312,9 @@ phypGetStoragePoolSize(virConnectPtr conn, char *name)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static char *
|
||||||
phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
|
phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
|
||||||
unsigned int capacity, char *key)
|
unsigned int capacity)
|
||||||
{
|
{
|
||||||
ConnectionData *connection_data = conn->networkPrivateData;
|
ConnectionData *connection_data = conn->networkPrivateData;
|
||||||
phyp_driverPtr phyp_driver = conn->privateData;
|
phyp_driverPtr phyp_driver = conn->privateData;
|
||||||
@ -2330,6 +2326,7 @@ phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
|
|||||||
char *ret = NULL;
|
char *ret = NULL;
|
||||||
int exit_status = 0;
|
int exit_status = 0;
|
||||||
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
char *key;
|
||||||
|
|
||||||
if (system_type == HMC)
|
if (system_type == HMC)
|
||||||
virBufferVSprintf(&buf, "viosvrcmd -m %s --id %d -c '",
|
virBufferVSprintf(&buf, "viosvrcmd -m %s --id %d -c '",
|
||||||
@ -2343,10 +2340,10 @@ phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
|
|||||||
if (virBufferError(&buf)) {
|
if (virBufferError(&buf)) {
|
||||||
virBufferFreeAndReset(&buf);
|
virBufferFreeAndReset(&buf);
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
cmd = virBufferContentAndReset(&buf);
|
|
||||||
|
|
||||||
|
cmd = virBufferContentAndReset(&buf);
|
||||||
ret = phypExec(session, cmd, &exit_status, conn);
|
ret = phypExec(session, cmd, &exit_status, conn);
|
||||||
|
|
||||||
if (exit_status < 0) {
|
if (exit_status < 0) {
|
||||||
@ -2354,29 +2351,37 @@ phypBuildVolume(virConnectPtr conn, const char *lvname, const char *spname,
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (phypVolumeGetKey(conn, key, lvname) == -1)
|
key = phypVolumeGetKey(conn, lvname);
|
||||||
goto err;;
|
|
||||||
|
if (key == NULL)
|
||||||
|
goto err;
|
||||||
|
|
||||||
VIR_FREE(cmd);
|
VIR_FREE(cmd);
|
||||||
VIR_FREE(ret);
|
VIR_FREE(ret);
|
||||||
return 0;
|
return key;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
VIR_FREE(cmd);
|
VIR_FREE(cmd);
|
||||||
VIR_FREE(ret);
|
VIR_FREE(ret);
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static virStorageVolPtr
|
static virStorageVolPtr
|
||||||
phypVolumeLookupByName(virStoragePoolPtr pool, const char *volname)
|
phypVolumeLookupByName(virStoragePoolPtr pool, const char *volname)
|
||||||
{
|
{
|
||||||
|
char *key;
|
||||||
|
virStorageVolPtr vol;
|
||||||
|
|
||||||
char key[MAX_KEY_SIZE];
|
key = phypVolumeGetKey(pool->conn, volname);
|
||||||
|
|
||||||
if (phypVolumeGetKey(pool->conn, key, volname) == -1)
|
if (key == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return virGetStorageVol(pool->conn, pool->name, volname, key);
|
vol = virGetStorageVol(pool->conn, pool->name, volname, key);
|
||||||
|
|
||||||
|
VIR_FREE(key);
|
||||||
|
|
||||||
|
return vol;
|
||||||
}
|
}
|
||||||
|
|
||||||
static virStorageVolPtr
|
static virStorageVolPtr
|
||||||
@ -2395,11 +2400,6 @@ phypStorageVolCreateXML(virStoragePoolPtr pool,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (VIR_ALLOC_N(key, MAX_KEY_SIZE) < 0) {
|
|
||||||
virReportOOMError();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Filling spdef manually
|
/* Filling spdef manually
|
||||||
* */
|
* */
|
||||||
if (pool->name != NULL) {
|
if (pool->name != NULL) {
|
||||||
@ -2457,9 +2457,10 @@ phypStorageVolCreateXML(virStoragePoolPtr pool,
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (phypBuildVolume
|
key = phypBuildVolume(pool->conn, voldef->name, spdef->name,
|
||||||
(pool->conn, voldef->name, spdef->name, voldef->capacity,
|
voldef->capacity);
|
||||||
key) == -1)
|
|
||||||
|
if (key == NULL)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
if ((vol =
|
if ((vol =
|
||||||
@ -2467,9 +2468,12 @@ phypStorageVolCreateXML(virStoragePoolPtr pool,
|
|||||||
key)) == NULL)
|
key)) == NULL)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
|
VIR_FREE(key);
|
||||||
|
|
||||||
return vol;
|
return vol;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
|
VIR_FREE(key);
|
||||||
virStorageVolDefFree(voldef);
|
virStorageVolDefFree(voldef);
|
||||||
virStoragePoolDefFree(spdef);
|
virStoragePoolDefFree(spdef);
|
||||||
if (vol)
|
if (vol)
|
||||||
@ -2543,8 +2547,10 @@ phypVolumeLookupByPath(virConnectPtr conn, const char *volname)
|
|||||||
int exit_status = 0;
|
int exit_status = 0;
|
||||||
char *cmd = NULL;
|
char *cmd = NULL;
|
||||||
char *spname = NULL;
|
char *spname = NULL;
|
||||||
|
char *char_ptr;
|
||||||
char *key = NULL;
|
char *key = NULL;
|
||||||
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
virStorageVolPtr vol;
|
||||||
|
|
||||||
if (system_type == HMC)
|
if (system_type == HMC)
|
||||||
virBufferVSprintf(&buf, "viosvrcmd -m %s --id %d -c '",
|
virBufferVSprintf(&buf, "viosvrcmd -m %s --id %d -c '",
|
||||||
@ -2569,20 +2575,21 @@ phypVolumeLookupByPath(virConnectPtr conn, const char *volname)
|
|||||||
if (exit_status < 0 || spname == NULL)
|
if (exit_status < 0 || spname == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
char *char_ptr = strchr(spname, '\n');
|
char_ptr = strchr(spname, '\n');
|
||||||
|
|
||||||
if (char_ptr)
|
if (char_ptr)
|
||||||
*char_ptr = '\0';
|
*char_ptr = '\0';
|
||||||
|
|
||||||
if (VIR_ALLOC_N(key, MAX_KEY_SIZE) < 0) {
|
key = phypVolumeGetKey(conn, volname);
|
||||||
virReportOOMError();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (phypVolumeGetKey(conn, key, volname) == -1)
|
if (key == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return virGetStorageVol(conn, spname, volname, key);
|
vol = virGetStorageVol(conn, spname, volname, key);
|
||||||
|
|
||||||
|
VIR_FREE(key);
|
||||||
|
|
||||||
|
return vol;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@ -2650,6 +2657,8 @@ phypStoragePoolLookupByName(virConnectPtr conn, const char *name)
|
|||||||
static char *
|
static char *
|
||||||
phypVolumeGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
|
phypVolumeGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
|
||||||
{
|
{
|
||||||
|
char *xml;
|
||||||
|
|
||||||
virCheckFlags(0, NULL);
|
virCheckFlags(0, NULL);
|
||||||
|
|
||||||
virStorageVolDef voldef;
|
virStorageVolDef voldef;
|
||||||
@ -2664,11 +2673,6 @@ phypVolumeGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
|
|||||||
virStoragePoolDef pool;
|
virStoragePoolDef pool;
|
||||||
memset(&pool, 0, sizeof(virStoragePoolDef));
|
memset(&pool, 0, sizeof(virStoragePoolDef));
|
||||||
|
|
||||||
if (VIR_ALLOC_N(voldef.key, MAX_KEY_SIZE) < 0) {
|
|
||||||
virReportOOMError();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sp->name != NULL) {
|
if (sp->name != NULL) {
|
||||||
pool.name = sp->name;
|
pool.name = sp->name;
|
||||||
} else {
|
} else {
|
||||||
@ -2705,14 +2709,20 @@ phypVolumeGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (memmove(voldef.key, vol->key, PATH_MAX) == NULL) {
|
voldef.key = strdup(vol->key);
|
||||||
VIR_ERROR0(_("Unable to determine volume's key."));
|
|
||||||
|
if (voldef.key == NULL) {
|
||||||
|
virReportOOMError();
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
voldef.type = VIR_STORAGE_POOL_LOGICAL;
|
voldef.type = VIR_STORAGE_POOL_LOGICAL;
|
||||||
|
|
||||||
return virStorageVolDefFormat(&pool, &voldef);
|
xml = virStorageVolDefFormat(&pool, &voldef);
|
||||||
|
|
||||||
|
VIR_FREE(voldef.key);
|
||||||
|
|
||||||
|
return xml;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -30,7 +30,6 @@
|
|||||||
# include <config.h>
|
# include <config.h>
|
||||||
# include <libssh2.h>
|
# include <libssh2.h>
|
||||||
|
|
||||||
# define MAX_KEY_SIZE (1024*4)
|
|
||||||
# define LPAR_EXEC_ERR -1
|
# define LPAR_EXEC_ERR -1
|
||||||
# define SSH_CONN_ERR -2 /* error while trying to connect to remote host */
|
# define SSH_CONN_ERR -2 /* error while trying to connect to remote host */
|
||||||
# define SSH_CMD_ERR -3 /* error while trying to execute the remote cmd */
|
# define SSH_CMD_ERR -3 /* error while trying to execute the remote cmd */
|
||||||
|
Loading…
Reference in New Issue
Block a user