提交 23e938ee 编写于 作者: J Ján Tomko

virAsprintf: correctly check return value

When virAsprintf was changed from a function to a macro
reporting OOM error in dc6f2dad, it was documented as returning
0 on success. This is incorrect, it returns the number of bytes
written as asprintf does.

Some of the functions were converted to use virAsprintf's return
value directly, changing the return value on success from 0 to >= 0.

For most of these, this is not a problem, but the change in
virPCIDriverDir breaks PCI passthrough.

The return value check in virhashtest pre-dates virAsprintf OOM
conversion.

vmwareMakePath seems to be unused.
上级 040d9963
......@@ -797,7 +797,9 @@ qemuAssignDeviceNetAlias(virDomainDefPtr def, virDomainNetDefPtr net, int idx)
}
}
return virAsprintf(&net->info.alias, "net%d", idx);
if (virAsprintf(&net->info.alias, "net%d", idx) < 0)
return -1;
return 0;
}
......@@ -852,7 +854,9 @@ qemuAssignDeviceRedirdevAlias(virDomainDefPtr def, virDomainRedirdevDefPtr redir
}
}
return virAsprintf(&redirdev->info.alias, "redir%d", idx);
if (virAsprintf(&redirdev->info.alias, "redir%d", idx) < 0)
return -1;
return 0;
}
......@@ -861,7 +865,10 @@ qemuAssignDeviceControllerAlias(virDomainControllerDefPtr controller)
{
const char *prefix = virDomainControllerTypeToString(controller->type);
return virAsprintf(&controller->info.alias, "%s%d", prefix, controller->idx);
if (virAsprintf(&controller->info.alias, "%s%d", prefix,
controller->idx) < 0)
return -1;
return 0;
}
static ssize_t
......
......@@ -2562,8 +2562,10 @@ qemuProcessPrepareMonitorChr(virQEMUDriverConfigPtr cfg,
monConfig->type = VIR_DOMAIN_CHR_TYPE_UNIX;
monConfig->data.nix.listen = true;
return virAsprintf(&monConfig->data.nix.path, "%s/%s.monitor",
cfg->libDir, vm);
if (virAsprintf(&monConfig->data.nix.path, "%s/%s.monitor",
cfg->libDir, vm) < 0)
return -1;
return 0;
}
......
......@@ -1050,7 +1050,9 @@ virNetDevSysfsFile(char **pf_sysfs_device_link, const char *ifname,
const char *file)
{
return virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/%s", ifname, file);
if (virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/%s", ifname, file) < 0)
return -1;
return 0;
}
static int
......@@ -1058,8 +1060,10 @@ virNetDevSysfsDeviceFile(char **pf_sysfs_device_link, const char *ifname,
const char *file)
{
return virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/device/%s",
ifname, file);
if (virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/device/%s", ifname,
file) < 0)
return -1;
return 0;
}
/**
......
......@@ -194,7 +194,9 @@ virPCIDriverDir(char **buffer, const char *driver)
{
VIR_FREE(*buffer);
return virAsprintf(buffer, PCI_SYSFS "drivers/%s", driver);
if (virAsprintf(buffer, PCI_SYSFS "drivers/%s", driver) < 0)
return -1;
return 0;
}
......@@ -203,7 +205,9 @@ virPCIDriverFile(char **buffer, const char *driver, const char *file)
{
VIR_FREE(*buffer);
return virAsprintf(buffer, PCI_SYSFS "drivers/%s/%s", driver, file);
if (virAsprintf(buffer, PCI_SYSFS "drivers/%s/%s", driver, file) < 0)
return -1;
return 0;
}
......@@ -212,7 +216,9 @@ virPCIFile(char **buffer, const char *device, const char *file)
{
VIR_FREE(*buffer);
return virAsprintf(buffer, PCI_SYSFS "devices/%s/%s", device, file);
if (virAsprintf(buffer, PCI_SYSFS "devices/%s/%s", device, file) < 0)
return -1;
return 0;
}
......@@ -2529,17 +2535,21 @@ out:
int
virPCIGetSysfsFile(char *virPCIDeviceName, char **pci_sysfs_device_link)
{
return virAsprintf(pci_sysfs_device_link, PCI_SYSFS "devices/%s",
virPCIDeviceName);
if (virAsprintf(pci_sysfs_device_link, PCI_SYSFS "devices/%s",
virPCIDeviceName) < 0)
return -1;
return 0;
}
int
virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr dev,
char **pci_sysfs_device_link)
{
return virAsprintf(pci_sysfs_device_link,
PCI_SYSFS "devices/%04x:%02x:%02x.%x", dev->domain,
dev->bus, dev->slot, dev->function);
if (virAsprintf(pci_sysfs_device_link,
PCI_SYSFS "devices/%04x:%02x:%02x.%x", dev->domain,
dev->bus, dev->slot, dev->function) < 0)
return -1;
return 0;
}
/*
......
......@@ -178,6 +178,8 @@ virRandomGenerateWWN(char **wwn,
return -1;
}
return virAsprintf(wwn, "5" "%s%09llx", oui,
(unsigned long long)virRandomBits(36));
if (virAsprintf(wwn, "5" "%s%09llx", oui,
(unsigned long long)virRandomBits(36)) < 0)
return -1;
return 0;
}
......@@ -176,7 +176,8 @@ size_t virStringListLength(char **strings);
* Like glibc's vasprintf but makes sure *strp == NULL on failure, in which
* case the OOM error is reported too.
*
* Returns -1 on failure (with OOM error reported), 0 on success.
* Returns -1 on failure (with OOM error reported), number of bytes printed
* on success.
*/
# define virVasprintf(strp, fmt, list) \
virVasprintfInternal(true, VIR_FROM_THIS, __FILE__, __FUNCTION__, \
......@@ -187,7 +188,7 @@ size_t virStringListLength(char **strings);
*
* Like glibc's vasprintf but makes sure *strp == NULL on failure.
*
* Returns -1 on failure, 0 on success.
* Returns -1 on failure, number of bytes printed on success.
*/
# define virVasprintfQuiet(strp, fmt, list) \
virVasprintfInternal(false, 0, NULL, NULL, 0, strp, fmt, list)
......@@ -200,7 +201,8 @@ size_t virStringListLength(char **strings);
* Like glibc's_asprintf but makes sure *strp == NULL on failure, in which case
* the OOM error is reported too.
*
* Returns -1 on failure (with OOM error reported), 0 on success.
* Returns -1 on failure (with OOM error reported), number of bytes printed
* on success.
*/
# define virAsprintf(strp, ...) \
......@@ -214,7 +216,7 @@ size_t virStringListLength(char **strings);
*
* Like glibc's_asprintf but makes sure *strp == NULL on failure.
*
* Returns -1 on failure, 0 on success.
* Returns -1 on failure, number of bytes printed on success.
*/
# define virAsprintfQuiet(strp, ...) \
......
......@@ -313,9 +313,16 @@ error:
int
vmwareConstructVmxPath(char *directoryName, char *name, char **vmxPath)
{
int ret;
if (directoryName != NULL)
return virAsprintf(vmxPath, "%s/%s.vmx", directoryName, name);
return virAsprintf(vmxPath, "%s.vmx", name);
ret = virAsprintf(vmxPath, "%s/%s.vmx", directoryName, name);
else
ret = virAsprintf(vmxPath, "%s.vmx", name);
if (ret < 0)
return -1;
return 0;
}
int
......@@ -414,7 +421,9 @@ vmwareMoveFile(char *srcFile, char *dstFile)
int
vmwareMakePath(char *srcDir, char *srcName, char *srcExt, char **outpath)
{
return virAsprintf(outpath, "%s/%s.%s", srcDir, srcName, srcExt);
if (virAsprintf(outpath, "%s/%s.%s", srcDir, srcName, srcExt) < 0)
return -1;
return 0;
}
int
......
......@@ -18,7 +18,7 @@
#define testError(...) \
do { \
char *str; \
if (virAsprintfQuiet(&str, __VA_ARGS__) == 0) { \
if (virAsprintfQuiet(&str, __VA_ARGS__) >= 0) { \
fprintf(stderr, "%s", str); \
VIR_FREE(str); \
} \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册