src: Make virStr*cpy*() functions return an int

Currently, the functions return a pointer to the
destination buffer on success or NULL on failure.

Not only does this kind of error handling look quite
alien in the context of libvirt, where most functions
return zero on success and a negative int on failure,
but it's also somewhat pointless because unless there's
been a failure the returned pointer will be the same
one passed in by the user, thus offering no additional
value.

Change the functions so that they return an int
instead.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
This commit is contained in:
Andrea Bolognani 2018-07-20 09:50:37 +02:00
parent 583bdfa65c
commit 6c0d0210cb
41 changed files with 113 additions and 115 deletions

View File

@ -1134,7 +1134,7 @@
respectively. The last argument is the number of bytes
available in the destination string; if a copy of the source
string (including a \0) will not fit into the destination, no
bytes are copied and the routine returns NULL. Otherwise, n
bytes are copied and the routine returns &lt;0. Otherwise, n
bytes from the source are copied into the destination and a
trailing \0 is appended.
</p>

View File

@ -1262,7 +1262,7 @@ virCapabilitiesGetNodeInfo(virNodeInfoPtr nodeinfo)
memset(nodeinfo, 0, sizeof(*nodeinfo));
if (virStrcpyStatic(nodeinfo->model, virArchToString(hostarch)) == NULL)
if (virStrcpyStatic(nodeinfo->model, virArchToString(hostarch)) < 0)
return -1;
if (virHostMemGetInfo(&memorybytes, NULL) < 0)

View File

@ -134,7 +134,7 @@ virNetDevVPortProfileParse(xmlNodePtr node, unsigned int flags)
}
if (virtPortProfileID &&
!virStrcpyStatic(virtPort->profileID, virtPortProfileID)) {
virStrcpyStatic(virtPort->profileID, virtPortProfileID) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("profileid parameter too long"));
goto error;

View File

@ -966,7 +966,7 @@ ipsetValidator(enum attrDatatype datatype ATTRIBUTE_UNUSED,
{
const char *errmsg = NULL;
if (virStrcpyStatic(item->u.ipset.setname, val->c) == NULL) {
if (virStrcpyStatic(item->u.ipset.setname, val->c) < 0) {
errmsg = _("ipset name is too long");
goto arg_err_exit;
}

View File

@ -914,8 +914,8 @@ esxConnectOpen(virConnectPtr conn, virConnectAuthPtr auth,
goto cleanup;
}
if (!virStrcpyStatic(vCenterIPAddress,
potentialVCenterIPAddress)) {
if (virStrcpyStatic(vCenterIPAddress,
potentialVCenterIPAddress) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("vCenter IP address %s too big for destination"),
potentialVCenterIPAddress);
@ -1317,7 +1317,7 @@ esxNodeGetInfo(virConnectPtr conn, virNodeInfoPtr nodeinfo)
++ptr;
}
if (!virStrcpyStatic(nodeinfo->model, dynamicProperty->val->string)) {
if (virStrcpyStatic(nodeinfo->model, dynamicProperty->val->string) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("CPU Model %s too long for destination"),
dynamicProperty->val->string);

View File

@ -185,7 +185,7 @@ esxVI_CURL_Debug(CURL *curl ATTRIBUTE_UNUSED, curl_infotype type,
if (VIR_ALLOC_N(buffer, size + 1) < 0)
return 0;
if (!virStrncpy(buffer, info, size, size + 1)) {
if (virStrncpy(buffer, info, size, size + 1) < 0) {
VIR_FREE(buffer);
return 0;
}

View File

@ -1491,7 +1491,7 @@ esxVI_DateTime_ConvertToCalendarTime(esxVI_DateTime *dateTime,
return -1;
}
if (!virStrcpyStatic(value, dateTime->value)) {
if (virStrcpyStatic(value, dateTime->value) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("xsd:dateTime value '%s' too long for destination"),
dateTime->value);

View File

@ -307,7 +307,7 @@ hypervNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
}
/* Fill struct */
if (virStrcpyStatic(info->model, processorList->data.common->Name) == NULL) {
if (virStrcpyStatic(info->model, processorList->data.common->Name) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("CPU model %s too long for destination"),
processorList->data.common->Name);

View File

@ -2365,7 +2365,7 @@ libxlDriverNodeGetInfo(libxlDriverPrivatePtr driver, virNodeInfoPtr info)
goto cleanup;
}
if (virStrcpyStatic(info->model, virArchToString(hostarch)) == NULL) {
if (virStrcpyStatic(info->model, virArchToString(hostarch)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("machine type %s too big for destination"),
virArchToString(hostarch));

View File

@ -221,8 +221,8 @@ virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE) < 0)
goto error;
if (!virStrcpyStatic(ls.name,
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE)) {
if (virStrcpyStatic(ls.name,
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Lockspace path '%s' exceeded %d characters"),
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE,
@ -231,7 +231,7 @@ virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
}
ls.host_id = 0; /* Doesn't matter for initialization */
ls.flags = 0;
if (!virStrcpy(ls.host_id_disk.path, path, SANLK_PATH_LEN)) {
if (virStrcpy(ls.host_id_disk.path, path, SANLK_PATH_LEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Lockspace path '%s' exceeded %d characters"),
path, SANLK_PATH_LEN);
@ -583,7 +583,7 @@ static int virLockManagerSanlockAddLease(virLockManagerPtr lock,
res->flags = shared ? SANLK_RES_SHARED : 0;
res->num_disks = 1;
if (!virStrcpy(res->name, name, SANLK_NAME_LEN)) {
if (virStrcpy(res->name, name, SANLK_NAME_LEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Resource name '%s' exceeds %d characters"),
name, SANLK_NAME_LEN);
@ -592,7 +592,7 @@ static int virLockManagerSanlockAddLease(virLockManagerPtr lock,
for (i = 0; i < nparams; i++) {
if (STREQ(params[i].key, "path")) {
if (!virStrcpy(res->disks[0].path, params[i].value.str, SANLK_PATH_LEN)) {
if (virStrcpy(res->disks[0].path, params[i].value.str, SANLK_PATH_LEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Lease path '%s' exceeds %d characters"),
params[i].value.str, SANLK_PATH_LEN);
@ -601,7 +601,7 @@ static int virLockManagerSanlockAddLease(virLockManagerPtr lock,
} else if (STREQ(params[i].key, "offset")) {
res->disks[0].offset = params[i].value.ul;
} else if (STREQ(params[i].key, "lockspace")) {
if (!virStrcpy(res->lockspace_name, params[i].value.str, SANLK_NAME_LEN)) {
if (virStrcpy(res->lockspace_name, params[i].value.str, SANLK_NAME_LEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Resource lockspace '%s' exceeds %d characters"),
params[i].value.str, SANLK_NAME_LEN);
@ -651,7 +651,7 @@ virLockManagerSanlockAddDisk(virLockManagerSanlockDriverPtr driver,
res->num_disks = 1;
if (virCryptoHashString(VIR_CRYPTO_HASH_MD5, name, &hash) < 0)
goto cleanup;
if (!virStrcpy(res->name, hash, SANLK_NAME_LEN)) {
if (virStrcpy(res->name, hash, SANLK_NAME_LEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("MD5 hash '%s' unexpectedly larger than %d characters"),
hash, (SANLK_NAME_LEN - 1));
@ -661,16 +661,16 @@ virLockManagerSanlockAddDisk(virLockManagerSanlockDriverPtr driver,
if (virAsprintf(&path, "%s/%s",
driver->autoDiskLeasePath, res->name) < 0)
goto cleanup;
if (!virStrcpy(res->disks[0].path, path, SANLK_PATH_LEN)) {
if (virStrcpy(res->disks[0].path, path, SANLK_PATH_LEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Lease path '%s' exceeds %d characters"),
path, SANLK_PATH_LEN);
goto cleanup;
}
if (!virStrcpy(res->lockspace_name,
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE,
SANLK_NAME_LEN)) {
if (virStrcpy(res->lockspace_name,
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE,
SANLK_NAME_LEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Resource lockspace '%s' exceeds %d characters"),
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE, SANLK_NAME_LEN);

View File

@ -1334,8 +1334,8 @@ static int lxcNodeGetSecurityModel(virConnectPtr conn,
|| caps->host.secModels[0].model == NULL)
goto cleanup;
if (!virStrcpy(secmodel->model, caps->host.secModels[0].model,
VIR_SECURITY_MODEL_BUFLEN)) {
if (virStrcpy(secmodel->model, caps->host.secModels[0].model,
VIR_SECURITY_MODEL_BUFLEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("security model string exceeds max %d bytes"),
VIR_SECURITY_MODEL_BUFLEN - 1);
@ -1343,8 +1343,8 @@ static int lxcNodeGetSecurityModel(virConnectPtr conn,
goto cleanup;
}
if (!virStrcpy(secmodel->doi, caps->host.secModels[0].doi,
VIR_SECURITY_DOI_BUFLEN)) {
if (virStrcpy(secmodel->doi, caps->host.secModels[0].doi,
VIR_SECURITY_DOI_BUFLEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("security DOI string exceeds max %d bytes"),
VIR_SECURITY_DOI_BUFLEN-1);

View File

@ -595,7 +595,7 @@ virNWFilterSnoopReqNew(const char *ifkey)
req->threadStatus = THREAD_STATUS_NONE;
if (virStrcpyStatic(req->ifkey, ifkey) == NULL ||
if (virStrcpyStatic(req->ifkey, ifkey) < 0||
virMutexInitRecursive(&req->lock) < 0)
goto err_free_req;

View File

@ -168,7 +168,7 @@ printVar(virNWFilterVarCombIterPtr vars,
return -1;
}
if (!virStrcpy(buf, val, bufsize)) {
if (virStrcpy(buf, val, bufsize) < 0) {
const char *varName;
varName = virNWFilterVarAccessGetVarName(item->varAccess);
@ -282,7 +282,7 @@ _printDataType(virNWFilterVarCombIterPtr vars,
break;
case DATATYPE_IPSETNAME:
if (virStrcpy(buf, item->u.ipset.setname, bufsize) == NULL) {
if (virStrcpy(buf, item->u.ipset.setname, bufsize) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Buffer to small for ipset name"));
return -1;
@ -311,7 +311,7 @@ _printDataType(virNWFilterVarCombIterPtr vars,
flags = virBufferContentAndReset(&vb);
if (virStrcpy(buf, flags, bufsize) == NULL) {
if (virStrcpy(buf, flags, bufsize) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Buffer too small for IPSETFLAGS type"));
VIR_FREE(flags);

View File

@ -170,7 +170,7 @@ virNWFilterLockIface(const char *ifname)
goto err_exit;
}
if (virStrcpyStatic(ifaceLock->ifname, ifname) == NULL) {
if (virStrcpyStatic(ifaceLock->ifname, ifname) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("interface name %s does not fit into "
"buffer "),

View File

@ -274,7 +274,7 @@ openvzReadNetworkConf(virDomainDefPtr def,
if (VIR_ALLOC_N(net->ifname, len+1) < 0)
goto error;
if (virStrncpy(net->ifname, p, len, len+1) == NULL) {
if (virStrncpy(net->ifname, p, len, len+1) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Network ifname %s too long for destination"), p);
goto error;
@ -291,7 +291,7 @@ openvzReadNetworkConf(virDomainDefPtr def,
if (VIR_ALLOC_N(net->data.bridge.brname, len+1) < 0)
goto error;
if (virStrncpy(net->data.bridge.brname, p, len, len+1) == NULL) {
if (virStrncpy(net->data.bridge.brname, p, len, len+1) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Bridge name %s too long for destination"), p);
goto error;
@ -304,7 +304,7 @@ openvzReadNetworkConf(virDomainDefPtr def,
_("Wrong length MAC address"));
goto error;
}
if (virStrncpy(cpy_temp, p, len, sizeof(cpy_temp)) == NULL) {
if (virStrncpy(cpy_temp, p, len, sizeof(cpy_temp)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("MAC address %s too long for destination"), p);
goto error;
@ -956,7 +956,7 @@ openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len)
uuidbuf = strtok_r(NULL, "\n", &saveptr);
if (iden != NULL && uuidbuf != NULL && STREQ(iden, "#UUID:")) {
if (virStrcpy(uuidstr, uuidbuf, len) == NULL) {
if (virStrcpy(uuidstr, uuidbuf, len) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid uuid %s"), uuidbuf);
goto cleanup;

View File

@ -207,7 +207,7 @@ qemuAgentOpenUnix(const char *monitor)
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
if (virStrcpyStatic(addr.sun_path, monitor) == NULL) {
if (virStrcpyStatic(addr.sun_path, monitor) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Agent path %s too big for destination"), monitor);
goto error;

View File

@ -4941,7 +4941,7 @@ qemuOpenChrChardevUNIXSocket(const virDomainChrSourceDef *dev)
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
if (virStrcpyStatic(addr.sun_path, dev->data.nix.path) == NULL) {
if (virStrcpyStatic(addr.sun_path, dev->data.nix.path) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("UNIX socket path '%s' too long"),
dev->data.nix.path);

View File

@ -353,7 +353,7 @@ qemuMonitorOpenUnix(const char *monitor,
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
if (virStrcpyStatic(addr.sun_path, monitor) == NULL) {
if (virStrcpyStatic(addr.sun_path, monitor) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Monitor path %s too big for destination"), monitor);
goto error;

View File

@ -1608,7 +1608,7 @@ remoteNodeGetCPUStats(virConnectPtr conn,
/* Deserialise the result. */
for (i = 0; i < *nparams; ++i) {
if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) == NULL) {
if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Stats %s too big for destination"),
ret.params.params_val[i].field);
@ -1672,7 +1672,7 @@ remoteNodeGetMemoryStats(virConnectPtr conn,
/* Deserialise the result. */
for (i = 0; i < *nparams; ++i) {
if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) == NULL) {
if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Stats %s too big for destination"),
ret.params.params_val[i].field);

View File

@ -424,7 +424,7 @@ virNetLibsshAuthenticatePrivkeyCb(const char *prompt,
virConnectCredential retr_passphrase;
int cred_type;
char *actual_prompt = NULL;
char *p;
int p;
/* request user's key password */
if (!sess->cred || !sess->cred->cb) {
@ -459,7 +459,7 @@ virNetLibsshAuthenticatePrivkeyCb(const char *prompt,
p = virStrncpy(buf, retr_passphrase.result,
retr_passphrase.resultlen, len);
VIR_DISPOSE_STRING(retr_passphrase.result);
if (!p) {
if (p < 0) {
virReportError(VIR_ERR_LIBSSH, "%s",
_("passphrase is too long for the buffer"));
goto error;

View File

@ -459,7 +459,7 @@ int virNetSocketNewListenUNIX(const char *path,
}
addr.data.un.sun_family = AF_UNIX;
if (virStrcpyStatic(addr.data.un.sun_path, path) == NULL) {
if (virStrcpyStatic(addr.data.un.sun_path, path) < 0) {
virReportSystemError(ENAMETOOLONG,
_("Path %s too long for unix socket"), path);
goto error;
@ -690,7 +690,7 @@ int virNetSocketNewConnectUNIX(const char *path,
}
remoteAddr.data.un.sun_family = AF_UNIX;
if (virStrcpyStatic(remoteAddr.data.un.sun_path, path) == NULL) {
if (virStrcpyStatic(remoteAddr.data.un.sun_path, path) < 0) {
virReportSystemError(ENOMEM, _("Path %s too long for unix socket"), path);
goto cleanup;
}

View File

@ -530,7 +530,7 @@ AppArmorGetSecurityProcessLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
}
if (virStrcpy(sec->label, profile_name,
VIR_SECURITY_LABEL_BUFLEN) == NULL) {
VIR_SECURITY_LABEL_BUFLEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("error copying profile name"));
goto cleanup;

View File

@ -1317,7 +1317,7 @@ vahParseArgv(vahControl * ctl, int argc, char **argv)
if (strlen(optarg) > PROFILE_NAME_SIZE - 1)
vah_error(ctl, 1, _("invalid UUID"));
if (virStrcpy((char *)ctl->uuid, optarg,
PROFILE_NAME_SIZE) == NULL)
PROFILE_NAME_SIZE) < 0)
vah_error(ctl, 1, _("error copying UUID"));
break;
default:

View File

@ -690,7 +690,7 @@ static char *testBuildFilename(const char *relativeTo,
int totalLen = baseLen + strlen(filename) + 1;
if (VIR_ALLOC_N(absFile, totalLen) < 0)
return NULL;
if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
if (virStrncpy(absFile, relativeTo, baseLen, totalLen) < 0) {
VIR_FREE(absFile);
return NULL;
}
@ -804,7 +804,7 @@ testParseNodeInfo(virNodeInfoPtr nodeInfo, xmlXPathContextPtr ctxt)
str = virXPathString("string(/node/cpu/model[1])", ctxt);
if (str != NULL) {
if (virStrcpyStatic(nodeInfo->model, str) == NULL) {
if (virStrcpyStatic(nodeInfo->model, str) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Model %s too big for destination"), str);
VIR_FREE(str);

View File

@ -850,7 +850,7 @@ static int umlMonitorAddress(const struct uml_driver *driver,
memset(addr, 0, sizeof(*addr));
addr->sun_family = AF_UNIX;
if (virStrcpyStatic(addr->sun_path, sockname) == NULL) {
if (virStrcpyStatic(addr->sun_path, sockname) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unix path %s too long for destination"), sockname);
retval = -1;
@ -951,7 +951,7 @@ static int umlMonitorCommand(const struct uml_driver *driver,
cmd, req.length);
return -1;
}
if (virStrcpyStatic(req.data, cmd) == NULL) {
if (virStrcpyStatic(req.data, cmd) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Command %s too long for destination"), cmd);
return -1;

View File

@ -1179,11 +1179,11 @@ int virFDStreamConnectUNIX(virStreamPtr st,
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
if (abstract) {
if (virStrcpy(sa.sun_path+1, path, sizeof(sa.sun_path)-1) == NULL)
if (virStrcpy(sa.sun_path+1, path, sizeof(sa.sun_path)-1) < 0)
goto error;
sa.sun_path[0] = '\0';
} else {
if (virStrcpyStatic(sa.sun_path, path) == NULL)
if (virStrcpyStatic(sa.sun_path, path) < 0)
goto error;
}

View File

@ -166,7 +166,7 @@ virHostCPUGetStatsFreeBSD(int cpuNum,
for (i = 0; cpu_map[i].field != NULL; i++) {
virNodeCPUStatsPtr param = &params[i];
if (virStrcpyStatic(param->field, cpu_map[i].field) == NULL) {
if (virStrcpyStatic(param->field, cpu_map[i].field) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Field '%s' too long for destination"),
cpu_map[i].field);
@ -933,7 +933,7 @@ virHostCPUStatsAssign(virNodeCPUStatsPtr param,
const char *name,
unsigned long long value)
{
if (virStrcpyStatic(param->field, name) == NULL) {
if (virStrcpyStatic(param->field, name) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("kernel cpu time field is too long"
" for the destination"));

View File

@ -104,7 +104,7 @@ virHostMemGetStatsFreeBSD(virNodeMemoryStatsPtr params,
}
param = &params[j++];
if (virStrcpyStatic(param->field, sysctl_map[i].field) == NULL) {
if (virStrcpyStatic(param->field, sysctl_map[i].field) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Field '%s' too long for destination"),
sysctl_map[i].field);
@ -122,7 +122,7 @@ virHostMemGetStatsFreeBSD(virNodeMemoryStatsPtr params,
"vfs.bufspace");
return -1;
}
if (virStrcpyStatic(param->field, VIR_NODE_MEMORY_STATS_BUFFERS) == NULL) {
if (virStrcpyStatic(param->field, VIR_NODE_MEMORY_STATS_BUFFERS) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Field '%s' too long for destination"),
VIR_NODE_MEMORY_STATS_BUFFERS);
@ -224,7 +224,7 @@ virHostMemGetStatsLinux(FILE *meminfo,
if (STREQ(meminfo_hdr, convp->meminfo_hdr)) {
virNodeMemoryStatsPtr param = &params[k++];
if (virStrcpyStatic(param->field, convp->field) == NULL) {
if (virStrcpyStatic(param->field, convp->field) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Field kernel memory too long for destination"));
goto cleanup;

View File

@ -1026,7 +1026,7 @@ virLogOutputToJournald(virLogSourcePtr source,
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
if (!virStrcpyStatic(sa.sun_path, "/run/systemd/journal/socket"))
if (virStrcpyStatic(sa.sun_path, "/run/systemd/journal/socket") < 0)
return;
memset(&mh, 0, sizeof(mh));

View File

@ -138,7 +138,7 @@ static int virNetDevSetupControlFull(const char *ifname,
if (ifr && ifname) {
memset(ifr, 0, sizeof(*ifr));
if (virStrcpyStatic(ifr->ifr_name, ifname) == NULL) {
if (virStrcpyStatic(ifr->ifr_name, ifname) < 0) {
virReportSystemError(ERANGE,
_("Network interface name '%s' is too long"),
ifname);
@ -593,7 +593,7 @@ int virNetDevSetName(const char* ifname, const char *newifname)
return -1;
# ifdef HAVE_STRUCT_IFREQ_IFR_NEWNAME
if (virStrcpyStatic(ifr.ifr_newname, newifname) == NULL) {
if (virStrcpyStatic(ifr.ifr_newname, newifname) < 0) {
virReportSystemError(ERANGE,
_("Network interface name '%s' is too long"),
newifname);
@ -914,7 +914,7 @@ int virNetDevGetIndex(const char *ifname, int *ifindex)
memset(&ifreq, 0, sizeof(ifreq));
if (virStrcpyStatic(ifreq.ifr_name, ifname) == NULL) {
if (virStrcpyStatic(ifreq.ifr_name, ifname) < 0) {
virReportSystemError(ERANGE,
_("invalid interface name %s"),
ifname);
@ -1015,7 +1015,7 @@ int virNetDevGetVLanID(const char *ifname, int *vlanid)
return -1;
}
if (virStrcpyStatic(vlanargs.device1, ifname) == NULL) {
if (virStrcpyStatic(vlanargs.device1, ifname) < 0) {
virReportSystemError(ERANGE,
_("invalid interface name %s"),
ifname);
@ -2763,7 +2763,7 @@ static int virNetDevParseMcast(char *buf, virNetDevMcastEntryPtr mcast)
mcast->idx = num;
break;
case VIR_MCAST_TYPE_NAME_TOKEN:
if (virStrcpy(mcast->name, token, VIR_MCAST_NAME_LEN) == NULL) {
if (virStrcpy(mcast->name, token, VIR_MCAST_NAME_LEN) < 0) {
virReportSystemError(EINVAL,
_("Failed to parse network device name from '%s'"),
buf);

View File

@ -93,7 +93,7 @@ static int virNetDevBridgeCmd(const char *brname,
return -1;
}
if (virStrcpyStatic(ifd.ifd_name, brname) == NULL) {
if (virStrcpyStatic(ifd.ifd_name, brname) < 0) {
virReportSystemError(ERANGE,
_("Network interface name '%s' is too long"),
brname);
@ -686,7 +686,7 @@ int virNetDevBridgeAddPort(const char *brname,
struct ifbreq req;
memset(&req, 0, sizeof(req));
if (virStrcpyStatic(req.ifbr_ifsname, ifname) == NULL) {
if (virStrcpyStatic(req.ifbr_ifsname, ifname) < 0) {
virReportSystemError(ERANGE,
_("Network interface name '%s' is too long"),
ifname);
@ -756,7 +756,7 @@ int virNetDevBridgeRemovePort(const char *brname,
struct ifbreq req;
memset(&req, 0, sizeof(req));
if (virStrcpyStatic(req.ifbr_ifsname, ifname) == NULL) {
if (virStrcpyStatic(req.ifbr_ifsname, ifname) < 0) {
virReportSystemError(ERANGE,
_("Network interface name '%s' is too long"),
ifname);

View File

@ -270,7 +270,7 @@ int virNetDevTapCreate(char **ifname,
ifr.ifr_flags |= IFF_VNET_HDR;
# endif
if (virStrcpyStatic(ifr.ifr_name, *ifname) == NULL) {
if (virStrcpyStatic(ifr.ifr_name, *ifname) < 0) {
virReportSystemError(ERANGE,
_("Network interface name '%s' is too long"),
*ifname);
@ -336,7 +336,7 @@ int virNetDevTapDelete(const char *ifname,
memset(&try, 0, sizeof(struct ifreq));
try.ifr_flags = IFF_TAP|IFF_NO_PI;
if (virStrcpyStatic(try.ifr_name, ifname) == NULL) {
if (virStrcpyStatic(try.ifr_name, ifname) < 0) {
virReportSystemError(ERANGE,
_("Network interface name '%s' is too long"),
ifname);

View File

@ -389,7 +389,7 @@ virNetDevVPortProfileMerge(virNetDevVPortProfilePtr orig,
orig->profileID, mods->profileID);
return -1;
}
if (virStrcpyStatic(orig->profileID, mods->profileID) == NULL) {
if (virStrcpyStatic(orig->profileID, mods->profileID) < 0) {
/* this should never happen - it indicates mods->profileID
* isn't properly null terminated. */
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -885,8 +885,8 @@ virNetDevVPortProfileGetNthParent(const char *ifname, int ifindex, unsigned int
break;
if (tb[IFLA_IFNAME]) {
if (!virStrcpy(parent_ifname, (char*)RTA_DATA(tb[IFLA_IFNAME]),
IFNAMSIZ)) {
if (virStrcpy(parent_ifname, (char*)RTA_DATA(tb[IFLA_IFNAME]),
IFNAMSIZ) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("buffer for root interface name is too small"));
rc = -1;

View File

@ -774,25 +774,23 @@ virAsprintfInternal(bool report,
* A safe version of strncpy. The last parameter is the number of bytes
* available in the destination string, *not* the number of bytes you want
* to copy. If the destination is not large enough to hold all n of the
* src string bytes plus a \0, NULL is returned and no data is copied.
* src string bytes plus a \0, <0 is returned and no data is copied.
* If the destination is large enough to hold the n bytes plus \0, then the
* string is copied and a pointer to the destination string is returned.
* string is copied and 0 is returned.
*/
char *
int
virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
{
char *ret;
if (n > (destbytes - 1))
return NULL;
return -1;
ret = strncpy(dest, src, n);
strncpy(dest, src, n);
/* strncpy NULL terminates iff the last character is \0. Therefore
* force the last byte to be \0
*/
dest[n] = '\0';
return ret;
return 0;
}
/**
@ -801,11 +799,11 @@ virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
* A safe version of strcpy. The last parameter is the number of bytes
* available in the destination string, *not* the number of bytes you want
* to copy. If the destination is not large enough to hold all n of the
* src string bytes plus a \0, NULL is returned and no data is copied.
* src string bytes plus a \0, <0 is returned and no data is copied.
* If the destination is large enough to hold the source plus \0, then the
* string is copied and a pointer to the destination string is returned.
* string is copied and 0 is returned.
*/
char *
int
virStrcpy(char *dest, const char *src, size_t destbytes)
{
return virStrncpy(dest, src, strlen(src), destbytes);

View File

@ -127,9 +127,9 @@ void virSkipSpacesBackwards(const char *str, char **endp)
bool virStringIsEmpty(const char *str);
char *virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
int virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
ATTRIBUTE_RETURN_CHECK;
char *virStrcpy(char *dest, const char *src, size_t destbytes)
int virStrcpy(char *dest, const char *src, size_t destbytes)
ATTRIBUTE_RETURN_CHECK;
# define virStrcpyStatic(dest, src) virStrcpy((dest), (src), sizeof(dest))

View File

@ -86,7 +86,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
if (VIR_RESIZE_N(keys, nkeysalloc, nkeys, 1) < 0)
goto cleanup;
if (virStrcpyStatic(keys[nkeys].field, name) == NULL) {
if (virStrcpyStatic(keys[nkeys].field, name) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Field name '%s' too long"), name);
goto cleanup;
@ -222,7 +222,7 @@ virTypedParameterAssign(virTypedParameterPtr param, const char *name,
va_start(ap, type);
if (virStrcpyStatic(param->field, name) == NULL) {
if (virStrcpyStatic(param->field, name) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
name);
goto cleanup;
@ -279,7 +279,7 @@ virTypedParameterAssignFromStr(virTypedParameterPtr param, const char *name,
goto cleanup;
}
if (virStrcpyStatic(param->field, name) == NULL) {
if (virStrcpyStatic(param->field, name) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
name);
goto cleanup;
@ -1413,7 +1413,7 @@ virTypedParamsDeserialize(virTypedParameterRemotePtr remote_params,
virTypedParameterRemotePtr remote_param = remote_params + i;
if (virStrcpyStatic(param->field,
remote_param->field) == NULL) {
remote_param->field) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("parameter %s too big for destination"),
remote_param->field);

View File

@ -430,9 +430,9 @@ xenapiNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
if (xen_host_cpu_get_all(session, &host_cpu_set)) {
host_cpu = host_cpu_set->contents[0];
xen_host_cpu_get_modelname(session, &modelname, host_cpu);
if (!virStrncpy(info->model, modelname,
MIN(strlen(modelname), LIBVIRT_MODELNAME_LEN - 1),
LIBVIRT_MODELNAME_LEN)) {
if (virStrncpy(info->model, modelname,
MIN(strlen(modelname), LIBVIRT_MODELNAME_LEN - 1),
LIBVIRT_MODELNAME_LEN) < 0) {
virReportOOMError();
xen_host_cpu_set_free(host_cpu_set);
VIR_FREE(modelname);

View File

@ -410,7 +410,7 @@ xenParsePCI(char *entry)
return NULL;
if (!(nextkey = strchr(key, ':')))
return NULL;
if (virStrncpy(domain, key, (nextkey - key), sizeof(domain)) == NULL) {
if (virStrncpy(domain, key, (nextkey - key), sizeof(domain)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Domain %s too big for destination"), key);
return NULL;
@ -419,7 +419,7 @@ xenParsePCI(char *entry)
key = nextkey + 1;
if (!(nextkey = strchr(key, ':')))
return NULL;
if (virStrncpy(bus, key, (nextkey - key), sizeof(bus)) == NULL) {
if (virStrncpy(bus, key, (nextkey - key), sizeof(bus)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Bus %s too big for destination"), key);
return NULL;
@ -428,7 +428,7 @@ xenParsePCI(char *entry)
key = nextkey + 1;
if (!(nextkey = strchr(key, '.')))
return NULL;
if (virStrncpy(slot, key, (nextkey - key), sizeof(slot)) == NULL) {
if (virStrncpy(slot, key, (nextkey - key), sizeof(slot)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Slot %s too big for destination"), key);
return NULL;
@ -437,7 +437,7 @@ xenParsePCI(char *entry)
key = nextkey + 1;
if (strlen(key) != 1)
return NULL;
if (virStrncpy(func, key, 1, sizeof(func)) == NULL) {
if (virStrncpy(func, key, 1, sizeof(func)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Function %s too big for destination"), key);
return NULL;
@ -669,7 +669,7 @@ xenParseVfb(virConfPtr conf, virDomainDefPtr def)
char vfb[MAX_VFB];
char *key = vfb;
if (virStrcpyStatic(vfb, list->list->str) == NULL) {
if (virStrcpyStatic(vfb, list->list->str) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("VFB %s too big for destination"),
list->list->str);
@ -880,7 +880,7 @@ xenParseVif(char *entry, const char *vif_typename)
if (STRPREFIX(key, "mac=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(mac, data, len, sizeof(mac)) == NULL) {
if (virStrncpy(mac, data, len, sizeof(mac)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("MAC address %s too big for destination"),
data);
@ -888,7 +888,7 @@ xenParseVif(char *entry, const char *vif_typename)
}
} else if (STRPREFIX(key, "bridge=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(bridge, data, len, sizeof(bridge)) == NULL) {
if (virStrncpy(bridge, data, len, sizeof(bridge)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Bridge %s too big for destination"),
data);
@ -901,7 +901,7 @@ xenParseVif(char *entry, const char *vif_typename)
return NULL;
} else if (STRPREFIX(key, "model=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(model, data, len, sizeof(model)) == NULL) {
if (virStrncpy(model, data, len, sizeof(model)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Model %s too big for destination"),
data);
@ -909,7 +909,7 @@ xenParseVif(char *entry, const char *vif_typename)
}
} else if (STRPREFIX(key, "type=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(type, data, len, sizeof(type)) == NULL) {
if (virStrncpy(type, data, len, sizeof(type)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Type %s too big for destination"),
data);
@ -917,7 +917,7 @@ xenParseVif(char *entry, const char *vif_typename)
}
} else if (STRPREFIX(key, "vifname=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(vifname, data, len, sizeof(vifname)) == NULL) {
if (virStrncpy(vifname, data, len, sizeof(vifname)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Vifname %s too big for destination"),
data);
@ -925,14 +925,14 @@ xenParseVif(char *entry, const char *vif_typename)
}
} else if (STRPREFIX(key, "ip=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(ip, data, len, sizeof(ip)) == NULL) {
if (virStrncpy(ip, data, len, sizeof(ip)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("IP %s too big for destination"), data);
return NULL;
}
} else if (STRPREFIX(key, "rate=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(rate, data, len, sizeof(rate)) == NULL) {
if (virStrncpy(rate, data, len, sizeof(rate)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("rate %s too big for destination"), data);
return NULL;

View File

@ -740,7 +740,7 @@ xenParseSxprSound(virDomainDefPtr def,
len = (offset2 - offset);
else
len = strlen(offset);
if (virStrncpy(model, offset, len, sizeof(model)) == NULL) {
if (virStrncpy(model, offset, len, sizeof(model)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Sound model %s too big for destination"),
offset);

View File

@ -480,7 +480,7 @@ xenParseXLVnuma(virConfPtr conf,
if (STRPREFIX(str, "pnode")) {
unsigned int cellid;
if (!virStrcpyStatic(vtoken, data)) {
if (virStrcpyStatic(vtoken, data) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("vnuma vnode %zu pnode '%s' too long for destination"),
vnodeCnt, data);
@ -496,7 +496,7 @@ xenParseXLVnuma(virConfPtr conf,
}
pnode = cellid;
} else if (STRPREFIX(str, "size")) {
if (!virStrcpyStatic(vtoken, data)) {
if (virStrcpyStatic(vtoken, data) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("vnuma vnode %zu size '%s' too long for destination"),
vnodeCnt, data);
@ -509,7 +509,7 @@ xenParseXLVnuma(virConfPtr conf,
virDomainNumaSetNodeMemorySize(numa, vnodeCnt, (kbsize * 1024));
} else if (STRPREFIX(str, "vcpus")) {
if (!virStrcpyStatic(vtoken, data)) {
if (virStrcpyStatic(vtoken, data) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("vnuma vnode %zu vcpus '%s' too long for destination"),
vnodeCnt, data);
@ -526,7 +526,7 @@ xenParseXLVnuma(virConfPtr conf,
size_t i, ndistances;
unsigned int value;
if (!virStrcpyStatic(vtoken, data)) {
if (virStrcpyStatic(vtoken, data) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("vnuma vnode %zu vdistances '%s' too long for destination"),
vnodeCnt, data);
@ -900,7 +900,7 @@ xenParseXLUSBController(virConfPtr conf, virDomainDefPtr def)
if (STRPREFIX(key, "type=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(type, data, len, sizeof(type)) == NULL) {
if (virStrncpy(type, data, len, sizeof(type)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("type %s invalid"),
data);
@ -908,7 +908,7 @@ xenParseXLUSBController(virConfPtr conf, virDomainDefPtr def)
}
} else if (STRPREFIX(key, "version=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(version, data, len, sizeof(version)) == NULL) {
if (virStrncpy(version, data, len, sizeof(version)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("version %s invalid"),
data);
@ -918,7 +918,7 @@ xenParseXLUSBController(virConfPtr conf, virDomainDefPtr def)
goto skipusbctrl;
} else if (STRPREFIX(key, "ports=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(ports, data, len, sizeof(ports)) == NULL) {
if (virStrncpy(ports, data, len, sizeof(ports)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("version %s invalid"),
data);
@ -1002,7 +1002,7 @@ xenParseXLUSB(virConfPtr conf, virDomainDefPtr def)
if (STRPREFIX(key, "hostbus=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(bus, data, len, sizeof(bus)) == NULL) {
if (virStrncpy(bus, data, len, sizeof(bus)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("bus %s too big for destination"),
data);
@ -1010,7 +1010,7 @@ xenParseXLUSB(virConfPtr conf, virDomainDefPtr def)
}
} else if (STRPREFIX(key, "hostaddr=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(device, data, len, sizeof(device)) == NULL) {
if (virStrncpy(device, data, len, sizeof(device)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("device %s too big for destination"),
data);
@ -1078,7 +1078,7 @@ xenParseXLChannel(virConfPtr conf, virDomainDefPtr def)
if (STRPREFIX(key, "connection=")) {
int len = nextkey ? (nextkey - data) : strlen(data);
if (virStrncpy(type, data, len, sizeof(type)) == NULL) {
if (virStrncpy(type, data, len, sizeof(type)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("connection %s too big"), data);
goto skipchannel;

View File

@ -159,7 +159,7 @@ xenParseXMDisk(char *entry, int hvm)
goto error;
if (virStrncpy(disk->dst, head, offset - head,
(offset - head) + 1) == NULL) {
(offset - head) + 1) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Dest file %s too big for destination"), head);
goto error;