[libvirt] [PATCH 2/5] Introduce VIR_STRDUP to replace strdup

Michal Privoznik mprivozn at redhat.com
Tue Apr 2 14:22:55 UTC 2013


---

WARNING: THIS PATCH IS NOT COMPLETE !!!

For full patch see the cover letter. I've trimmed the patch and left only
interesting parts.

 HACKING                                   |   5 ++
 cfg.mk                                    |   8 ++
 daemon/libvirtd-config.c                  |  24 ++---
 daemon/libvirtd.c                         |  14 +--
 daemon/remote.c                           |  72 +++++++--------
 docs/hacking.html.in                      |   8 ++
 src/conf/capabilities.c                   |  28 +++---
 src/conf/cpu_conf.c                       |  12 +--
 src/conf/domain_conf.c                    |  52 +++++------
 src/conf/domain_event.c                   |  35 ++++----
 src/conf/node_device_conf.c               |  11 +--
 src/conf/nwfilter_conf.c                  |   4 +-
 src/conf/nwfilter_params.c                |   8 +-
 src/conf/snapshot_conf.c                  |   6 +-
 src/conf/storage_conf.c                   |   6 +-
 src/conf/virchrdev.c                      |   4 +-
 src/cpu/cpu_generic.c                     |  10 +--
 src/cpu/cpu_map.c                         |   7 +-
 src/cpu/cpu_powerpc.c                     |  16 ++--
 src/cpu/cpu_x86.c                         |   6 +-
 src/datatypes.c                           |  27 +++---
 src/esx/esx_device_monitor.c              |   1 -
 src/esx/esx_device_monitor.h              |   1 -
 src/esx/esx_driver.c                      |  15 ++--
 src/esx/esx_driver.h                      |   1 -
 src/esx/esx_interface_driver.c            |  20 ++---
 src/esx/esx_interface_driver.h            |   1 -
 src/esx/esx_network_driver.c              |  24 ++---
 src/esx/esx_network_driver.h              |   1 -
 src/esx/esx_nwfilter_driver.c             |   1 -
 src/esx/esx_nwfilter_driver.h             |   1 -
 src/esx/esx_private.h                     |   1 -
 src/esx/esx_secret_driver.c               |   1 -
 src/esx/esx_secret_driver.h               |   1 -
 src/esx/esx_storage_backend_iscsi.c       |   8 +-
 src/esx/esx_storage_backend_iscsi.h       |   1 -
 src/esx/esx_storage_backend_vmfs.c        |   5 +-
 src/esx/esx_storage_backend_vmfs.h        |   1 -
 src/esx/esx_storage_driver.c              |   1 -
 src/esx/esx_storage_driver.h              |   1 -
 src/esx/esx_util.c                        |  13 ++-
 src/esx/esx_vi.c                          |  25 +++---
 src/esx/esx_vi.h                          |   1 -
 src/esx/esx_vi_methods.c                  |   1 -
 src/esx/esx_vi_methods.h                  |   1 -
 src/esx/esx_vi_types.c                    |   9 +-
 src/esx/esx_vi_types.h                    |   1 -
 src/hyperv/hyperv_device_monitor.c        |   3 -
 src/hyperv/hyperv_device_monitor.h        |   1 -
 src/hyperv/hyperv_driver.c                |  17 ++--
 src/hyperv/hyperv_driver.h                |   1 -
 src/hyperv/hyperv_interface_driver.c      |   3 -
 src/hyperv/hyperv_interface_driver.h      |   1 -
 src/hyperv/hyperv_network_driver.c        |   3 -
 src/hyperv/hyperv_network_driver.h        |   1 -
 src/hyperv/hyperv_nwfilter_driver.c       |   3 -
 src/hyperv/hyperv_nwfilter_driver.h       |   1 -
 src/hyperv/hyperv_private.h               |   1 -
 src/hyperv/hyperv_secret_driver.c         |   3 -
 src/hyperv/hyperv_secret_driver.h         |   1 -
 src/hyperv/hyperv_storage_driver.c        |   3 -
 src/hyperv/hyperv_storage_driver.h        |   1 -
 src/hyperv/hyperv_util.c                  |  14 ++-
 src/hyperv/hyperv_util.h                  |   1 -
 src/hyperv/hyperv_wmi.c                   |   1 -
 src/hyperv/hyperv_wmi.h                   |   3 -
 src/hyperv/hyperv_wmi_classes.c           |   1 -
 src/hyperv/hyperv_wmi_classes.h           |   1 -
 src/hyperv/openwsman.h                    |   1 -
 src/interface/interface_backend_udev.c    |  14 +--
 src/libvirt.c                             |   6 +-
 src/libvirt_private.syms                  |   1 +
 src/libxl/libxl_conf.c                    |  36 ++++----
 src/libxl/libxl_driver.c                  |  10 +--
 src/locking/lock_daemon.c                 |  12 +--
 src/locking/lock_daemon_config.c          |   8 +-
 src/locking/lock_daemon_dispatch.c        |   3 +-
 src/locking/lock_driver_lockd.c           |  22 ++---
 src/locking/lock_driver_sanlock.c         |   8 +-
 src/locking/lock_manager.c                |   2 +-
 src/lxc/lxc_conf.c                        |  28 +++---
 src/lxc/lxc_container.c                   |  14 +--
 src/lxc/lxc_controller.c                  |   6 +-
 src/lxc/lxc_driver.c                      |   4 +-
 src/network/bridge_driver.c               |  14 +--
 src/node_device/node_device_driver.c      |  20 ++---
 src/node_device/node_device_hal.c         |   6 +-
 src/node_device/node_device_udev.c        |  48 +++++-----
 src/nodeinfo.c                            |   2 +-
 src/nwfilter/nwfilter_dhcpsnoop.c         |   8 +-
 src/nwfilter/nwfilter_driver.c            |   4 +-
 src/nwfilter/nwfilter_ebiptables_driver.c |   2 +-
 src/nwfilter/nwfilter_gentech_driver.c    |  18 ++--
 src/nwfilter/nwfilter_learnipaddr.c       |   2 +-
 src/openvz/openvz_conf.c                  |  16 ++--
 src/openvz/openvz_driver.c                |  10 +--
 src/parallels/parallels_driver.c          |  34 +++----
 src/parallels/parallels_network.c         |  12 +--
 src/parallels/parallels_storage.c         |  26 +++---
 src/phyp/phyp_driver.c                    |  20 ++---
 src/qemu/qemu_capabilities.c              |  22 ++---
 src/qemu/qemu_command.c                   | 142 +++++++++++++++---------------
 src/qemu/qemu_conf.c                      |  28 +++---
 src/qemu/qemu_driver.c                    |  44 ++++-----
 src/qemu/qemu_hotplug.c                   |   6 +-
 src/qemu/qemu_migration.c                 |  12 +--
 src/qemu/qemu_monitor_json.c              |  24 ++---
 src/qemu/qemu_monitor_text.c              |   4 +-
 src/qemu/qemu_process.c                   |  34 +++----
 src/remote/remote_driver.c                |  52 +++++------
 src/rpc/gendispatch.pl                    |  10 +--
 src/rpc/virnetclient.c                    |   7 +-
 src/rpc/virnetmessage.c                   |  11 +--
 src/rpc/virnetsaslcontext.c               |   7 +-
 src/rpc/virnetserver.c                    |   3 +-
 src/rpc/virnetserverclient.c              |   4 +-
 src/rpc/virnetservermdns.c                |   4 +-
 src/rpc/virnetsocket.c                    |   4 +-
 src/rpc/virnetsshsession.c                |  22 ++---
 src/rpc/virnettlscontext.c                |  12 +--
 src/secret/secret_driver.c                |   2 +-
 src/security/security_apparmor.c          |   4 +-
 src/security/security_dac.c               |   4 +-
 src/security/security_nop.c               |   4 +-
 src/security/security_selinux.c           |  32 +++----
 src/security/virt-aa-helper.c             |   4 +-
 src/storage/storage_backend.c             |  16 ++--
 src/storage/storage_backend_disk.c        |  12 +--
 src/storage/storage_backend_fs.c          |  16 ++--
 src/storage/storage_backend_iscsi.c       |   4 +-
 src/storage/storage_backend_logical.c     |  10 +--
 src/storage/storage_backend_mpath.c       |   2 +-
 src/storage/storage_backend_rbd.c         |   2 +-
 src/storage/storage_backend_scsi.c        |   6 +-
 src/storage/storage_driver.c              |  10 +--
 src/test/test_driver.c                    |  54 ++++++------
 src/uml/uml_conf.c                        |   4 +-
 src/uml/uml_driver.c                      |   6 +-
 src/util/virauth.c                        |   8 +-
 src/util/virauthconfig.c                  |   4 +-
 src/util/virbitmap.c                      |   3 +-
 src/util/vircgroup.c                      |   8 +-
 src/util/vircommand.c                     |  20 ++---
 src/util/virconf.c                        |   3 +-
 src/util/virdnsmasq.c                     |   4 +-
 src/util/virebtables.c                    |  27 +++---
 src/util/virhash.c                        |   7 +-
 src/util/viridentity.c                    |   5 +-
 src/util/virinitctl.c                     |   2 +-
 src/util/viriptables.c                    |   4 +-
 src/util/virjson.c                        |   8 +-
 src/util/virlockspace.c                   |  12 +--
 src/util/virlog.c                         |   6 +-
 src/util/virnetdevmacvlan.c               |  13 +--
 src/util/virnetdevtap.c                   |   2 +-
 src/util/virnetdevvportprofile.c          |   2 +-
 src/util/virobject.c                      |   7 +-
 src/util/virpci.c                         |   4 +-
 src/util/virsexpr.c                       |   6 +-
 src/util/virsocketaddr.c                  |   4 +-
 src/util/virstoragefile.c                 |   6 +-
 src/util/virstring.c                      |  20 ++++-
 src/util/virstring.h                      |   8 ++
 src/util/virsysinfo.c                     |   2 +-
 src/util/virtypedparam.c                  |   6 +-
 src/util/viruri.c                         |  18 ++--
 src/util/virutil.c                        |  34 +++----
 src/util/virxml.c                         |   2 +-
 src/vbox/vbox_XPCOMCGlue.c                |   2 +-
 src/vbox/vbox_tmpl.c                      | 106 +++++++++++-----------
 src/vmware/vmware_conf.c                  |  10 +--
 src/vmware/vmware_driver.c                |  15 ++--
 src/vmx/vmx.c                             |  10 +--
 src/xen/xen_driver.c                      |   2 +-
 src/xen/xen_hypervisor.c                  |   8 +-
 src/xen/xen_inotify.c                     |  19 ++--
 src/xen/xend_internal.c                   |  30 +++----
 src/xen/xm_internal.c                     |   6 +-
 src/xen/xs_internal.c                     |  17 ++--
 src/xenapi/xenapi_driver.c                |  30 +++----
 src/xenapi/xenapi_utils.c                 |  20 ++---
 src/xenxs/xen_sxpr.c                      |  68 +++++++-------
 src/xenxs/xen_xm.c                        |  38 ++++----
 183 files changed, 1133 insertions(+), 1128 deletions(-)

diff --git a/HACKING b/HACKING
index c8833c0..6230ffd 100644
--- a/HACKING
+++ b/HACKING
@@ -719,6 +719,11 @@ sizeof(dest) returns something meaningful). Note that this is a macro, so
 arguments could be evaluated more than once. This is equivalent to
 virStrncpy(dest, src, strlen(src), sizeof(dest)).
 
+  VIR_STRDUP(char *c)
+
+You should avoid using strdup directly as it does not report OOM error. Use
+VIR_STRDUP(c) macro instead (@c is type of char *).
+
 
 Variable length string buffer
 =============================
diff --git a/cfg.mk b/cfg.mk
index 7a2c69f..8cc67a5 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -383,6 +383,11 @@ sc_prohibit_asprintf:
 	halt='use virAsprintf, not as'printf				\
 	  $(_sc_search_regexp)
 
+sc_prohibit_strdup:
+	@prohibit='\<strdup\>'							\
+    halt='use VIR_STRUP, not strdup'				\
+	  $(_sc_search_regexp)
+
 # Prefer virSetUIDGID.
 sc_prohibit_setuid:
 	@prohibit='\<set(re)?[ug]id\> *\('				\
@@ -814,6 +819,9 @@ exclude_file_name_regexp--sc_prohibit_always_true_header_tests = \
 exclude_file_name_regexp--sc_prohibit_asprintf = \
   ^(bootstrap.conf$$|src/util/virstring\.c$$|examples/domain-events/events-c/event-test\.c$$)
 
+exclude_file_name_regexp--sc_prohibit_strdup = \
+  ^(bootstrap.conf$$|cfg.mk$$|docs/|examples/|python/|src/storage/parthelper.c$$|src/util/virerror.c$$|src/util/virstring.c$$|tests/|tools/)
+
 exclude_file_name_regexp--sc_prohibit_close = \
   (\.p[yl]$$|^docs/|^(src/util/virfile\.c|src/libvirt\.c)$$)
 
diff --git a/daemon/libvirtd-config.c b/daemon/libvirtd-config.c
index cc60098..915c7f3 100644
--- a/daemon/libvirtd-config.c
+++ b/daemon/libvirtd-config.c
@@ -59,7 +59,7 @@ remoteConfigGetStringList(virConfPtr conf, const char *key, char ***list_arg,
                            key);
             return -1;
         }
-        list[0] = strdup(p->str);
+        list[0] = VIR_STRDUP(p->str);
         list[1] = NULL;
         if (list[0] == NULL) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -90,7 +90,7 @@ remoteConfigGetStringList(virConfPtr conf, const char *key, char ***list_arg,
                 VIR_FREE(list);
                 return -1;
             }
-            list[i] = strdup(pp->str);
+            list[i] = VIR_STRDUP(pp->str);
             if (list[i] == NULL) {
                 int j;
                 for (j = 0 ; j < i ; j++)
@@ -136,8 +136,8 @@ checkType(virConfValuePtr p, const char *filename,
 }
 
 /* If there is no config data for the key, #var_name, then do nothing.
-   If there is valid data of type VIR_CONF_STRING, and strdup succeeds,
-   store the result in var_name.  Otherwise, (i.e. invalid type, or strdup
+   If there is valid data of type VIR_CONF_STRING, and VIR_STRDUP succeeds,
+   store the result in var_name.  Otherwise, (i.e. invalid type, or VIR_STRDUP
    failure), give a diagnostic and "goto" the cleanup-and-fail label.  */
 #define GET_CONF_STR(conf, filename, var_name)                          \
     do {                                                                \
@@ -146,7 +146,7 @@ checkType(virConfValuePtr p, const char *filename,
             if (checkType(p, filename, #var_name, VIR_CONF_STRING) < 0) \
                 goto error;                                             \
             VIR_FREE(data->var_name);                                   \
-            if (!(data->var_name = strdup(p->str))) {                   \
+            if (!(data->var_name = VIR_STRDUP(p->str))) {                   \
                 virReportOOMError();                                    \
                 goto error;                                             \
             }                                                           \
@@ -200,7 +200,7 @@ int
 daemonConfigFilePath(bool privileged, char **configfile)
 {
     if (privileged) {
-        if (!(*configfile = strdup(SYSCONFDIR "/libvirt/libvirtd.conf")))
+        if (!(*configfile = VIR_STRDUP(SYSCONFDIR "/libvirt/libvirtd.conf")))
             goto no_memory;
     } else {
         char *configdir = NULL;
@@ -238,9 +238,9 @@ daemonConfigNew(bool privileged ATTRIBUTE_UNUSED)
     data->listen_tls = 1;
     data->listen_tcp = 0;
 
-    if (!(data->tls_port = strdup(LIBVIRTD_TLS_PORT)))
+    if (!(data->tls_port = VIR_STRDUP(LIBVIRTD_TLS_PORT)))
         goto no_memory;
-    if (!(data->tcp_port = strdup(LIBVIRTD_TCP_PORT)))
+    if (!(data->tcp_port = VIR_STRDUP(LIBVIRTD_TCP_PORT)))
         goto no_memory;
 
     /* Only default to PolicyKit if running as root */
@@ -257,10 +257,10 @@ daemonConfigNew(bool privileged ATTRIBUTE_UNUSED)
 #endif
 
     if (data->auth_unix_rw == REMOTE_AUTH_POLKIT)
-        data->unix_sock_rw_perms = strdup("0777"); /* Allow world */
+        data->unix_sock_rw_perms = VIR_STRDUP("0777"); /* Allow world */
     else
-        data->unix_sock_rw_perms = strdup("0700"); /* Allow user only */
-    data->unix_sock_ro_perms = strdup("0777"); /* Always allow world */
+        data->unix_sock_rw_perms = VIR_STRDUP("0700"); /* Allow user only */
+    data->unix_sock_ro_perms = VIR_STRDUP("0777"); /* Always allow world */
     if (!data->unix_sock_ro_perms ||
         !data->unix_sock_rw_perms)
         goto no_memory;
@@ -382,7 +382,7 @@ daemonConfigLoadOptions(struct daemonConfig *data,
      */
     if (data->auth_unix_rw == REMOTE_AUTH_POLKIT) {
         VIR_FREE(data->unix_sock_rw_perms);
-        if (!(data->unix_sock_rw_perms = strdup("0777"))) {
+        if (!(data->unix_sock_rw_perms = VIR_STRDUP("0777"))) {
             virReportOOMError();
             goto error;
         }
diff --git a/docs/hacking.html.in b/docs/hacking.html.in
index 7ef826c..0fbb500 100644
--- a/docs/hacking.html.in
+++ b/docs/hacking.html.in
@@ -853,6 +853,14 @@
       virStrncpy(dest, src, strlen(src), sizeof(dest)).
     </p>
 
+<pre>
+  VIR_STRDUP(char *c)
+</pre>
+    <p>
+      You should avoid using strdup directly as it does not report OOM error.
+      Use VIR_STRDUP(c) macro instead (@c is type of char *).
+    </p>
+
     <h2><a name="strbuf">Variable length string buffer</a></h2>
 
     <p>
-- 
1.8.1.5




More information about the libvir-list mailing list