[libvirt] [PATCH 2/3] src: Use virStrcpyStatic() wherever possible

Andrea Bolognani abologna at redhat.com
Tue Jul 17 11:09:56 UTC 2018


This convenience macro was created for the simple cases
where the length of the source string and the size of the
destination buffer can be figued out with strlen() and
sizeof() respectively, so we should use it wherever
possible instead of open-coding parts of it.

Signed-off-by: Andrea Bolognani <abologna at redhat.com>
---
 src/conf/nwfilter_conf.c |  3 +--
 src/util/virfdstream.c   |  2 +-
 src/util/virlog.c        |  5 ++---
 src/util/virnetdev.c     |  3 +--
 src/xenconfig/xen_xl.c   | 17 ++++-------------
 5 files changed, 9 insertions(+), 21 deletions(-)

diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index 706e803a25..36a7315880 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -966,8 +966,7 @@ ipsetValidator(enum attrDatatype datatype ATTRIBUTE_UNUSED,
 {
     const char *errmsg = NULL;
 
-    if (virStrcpy(item->u.ipset.setname, val->c,
-                  sizeof(item->u.ipset.setname)) == NULL) {
+    if (virStrcpyStatic(item->u.ipset.setname, val->c) == NULL) {
         errmsg = _("ipset name is too long");
         goto arg_err_exit;
     }
diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c
index 8189559964..f4777cfd12 100644
--- a/src/util/virfdstream.c
+++ b/src/util/virfdstream.c
@@ -1183,7 +1183,7 @@ int virFDStreamConnectUNIX(virStreamPtr st,
             goto error;
         sa.sun_path[0] = '\0';
     } else {
-        if (virStrcpy(sa.sun_path, path, sizeof(sa.sun_path)) == NULL)
+        if (virStrcpyStatic(sa.sun_path, path) == NULL)
             goto error;
     }
 
diff --git a/src/util/virlog.c b/src/util/virlog.c
index e008dd9c54..9d569057ae 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -284,8 +284,7 @@ virLogOnceInit(void)
      */
     r = gethostname(virLogHostname, sizeof(virLogHostname));
     if (r == -1) {
-        ignore_value(virStrcpy(virLogHostname,
-                               "(unknown)", sizeof(virLogHostname)));
+        ignore_value(virStrcpyStatic(virLogHostname, "(unknown)"));
     } else {
         NUL_TERMINATE(virLogHostname);
     }
@@ -1027,7 +1026,7 @@ virLogOutputToJournald(virLogSourcePtr source,
 
     memset(&sa, 0, sizeof(sa));
     sa.sun_family = AF_UNIX;
-    if (!virStrcpy(sa.sun_path, "/run/systemd/journal/socket", sizeof(sa.sun_path)))
+    if (!virStrcpyStatic(sa.sun_path, "/run/systemd/journal/socket"))
         return;
 
     memset(&mh, 0, sizeof(mh));
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index c20022fbc9..57ebd0ec03 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -914,8 +914,7 @@ int virNetDevGetIndex(const char *ifname, int *ifindex)
 
     memset(&ifreq, 0, sizeof(ifreq));
 
-    if (virStrncpy(ifreq.ifr_name, ifname, strlen(ifname),
-                   sizeof(ifreq.ifr_name)) == NULL) {
+    if (virStrcpyStatic(ifreq.ifr_name, ifname) == NULL) {
         virReportSystemError(ERANGE,
                              _("invalid interface name %s"),
                              ifname);
diff --git a/src/xenconfig/xen_xl.c b/src/xenconfig/xen_xl.c
index f0d9177cec..807fe621d6 100644
--- a/src/xenconfig/xen_xl.c
+++ b/src/xenconfig/xen_xl.c
@@ -475,15 +475,12 @@ xenParseXLVnuma(virConfPtr conf,
                 data++;
 
                 if (*data) {
-                    size_t len;
                     char vtoken[64];
 
                     if (STRPREFIX(str, "pnode")) {
                         unsigned int cellid;
 
-                        len = strlen(data);
-                        if (!virStrncpy(vtoken, data,
-                                        len, sizeof(vtoken))) {
+                        if (!virStrcpyStatic(vtoken, data)) {
                             virReportError(VIR_ERR_INTERNAL_ERROR,
                                            _("vnuma vnode %zu pnode '%s' too long for destination"),
                                            vnodeCnt, data);
@@ -499,9 +496,7 @@ xenParseXLVnuma(virConfPtr conf,
                         }
                         pnode = cellid;
                     } else if (STRPREFIX(str, "size")) {
-                        len = strlen(data);
-                        if (!virStrncpy(vtoken, data,
-                                        len, sizeof(vtoken))) {
+                        if (!virStrcpyStatic(vtoken, data)) {
                             virReportError(VIR_ERR_INTERNAL_ERROR,
                                            _("vnuma vnode %zu size '%s' too long for destination"),
                                            vnodeCnt, data);
@@ -514,9 +509,7 @@ xenParseXLVnuma(virConfPtr conf,
                         virDomainNumaSetNodeMemorySize(numa, vnodeCnt, (kbsize * 1024));
 
                     } else if (STRPREFIX(str, "vcpus")) {
-                        len = strlen(data);
-                        if (!virStrncpy(vtoken, data,
-                                        len, sizeof(vtoken))) {
+                        if (!virStrcpyStatic(vtoken, data)) {
                             virReportError(VIR_ERR_INTERNAL_ERROR,
                                            _("vnuma vnode %zu vcpus '%s' too long for destination"),
                                            vnodeCnt, data);
@@ -533,9 +526,7 @@ xenParseXLVnuma(virConfPtr conf,
                         size_t i, ndistances;
                         unsigned int value;
 
-                        len = strlen(data);
-                        if (!virStrncpy(vtoken, data,
-                                        len, sizeof(vtoken))) {
+                        if (!virStrcpyStatic(vtoken, data)) {
                             virReportError(VIR_ERR_INTERNAL_ERROR,
                                            _("vnuma vnode %zu vdistances '%s' too long for destination"),
                                            vnodeCnt, data);
-- 
2.17.1




More information about the libvir-list mailing list