diff mbox

[ovs-dev] util: New function nullable_xstrdup().

Message ID 1466828596-7028-1-git-send-email-blp@ovn.org
State Accepted
Headers show

Commit Message

Ben Pfaff June 25, 2016, 4:23 a.m. UTC
It's a pretty common pattern so create a function for it.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 lib/dpif-netdev.c            | 6 ++----
 lib/jsonrpc.c                | 2 +-
 lib/ovsdb-error.c            | 6 +++---
 lib/syslog-libc.c            | 4 ++--
 lib/util.c                   | 8 +++++++-
 lib/util.h                   | 1 +
 lib/vlog.c                   | 2 +-
 ofproto/ofproto-dpif-ipfix.c | 6 ------
 ofproto/ofproto-dpif-sflow.c | 6 +++---
 ofproto/ofproto.c            | 5 ++---
 ovn/utilities/ovn-nbctl.c    | 2 +-
 ovn/utilities/ovn-sbctl.c    | 2 +-
 ovsdb/replication.c          | 2 +-
 utilities/ovs-vsctl.c        | 2 +-
 vswitchd/bridge.c            | 8 ++------
 vtep/vtep-ctl.c              | 2 +-
 16 files changed, 29 insertions(+), 35 deletions(-)

Comments

William Tu June 25, 2016, 8:09 p.m. UTC | #1
Looks good to me.

Acked-by: William Tu <u9012063@gmail.com>

On Fri, Jun 24, 2016 at 9:23 PM, Ben Pfaff <blp@ovn.org> wrote:
> It's a pretty common pattern so create a function for it.
>
> Signed-off-by: Ben Pfaff <blp@ovn.org>
> ---
>  lib/dpif-netdev.c            | 6 ++----
>  lib/jsonrpc.c                | 2 +-
>  lib/ovsdb-error.c            | 6 +++---
>  lib/syslog-libc.c            | 4 ++--
>  lib/util.c                   | 8 +++++++-
>  lib/util.h                   | 1 +
>  lib/vlog.c                   | 2 +-
>  ofproto/ofproto-dpif-ipfix.c | 6 ------
>  ofproto/ofproto-dpif-sflow.c | 6 +++---
>  ofproto/ofproto.c            | 5 ++---
>  ovn/utilities/ovn-nbctl.c    | 2 +-
>  ovn/utilities/ovn-sbctl.c    | 2 +-
>  ovsdb/replication.c          | 2 +-
>  utilities/ovs-vsctl.c        | 2 +-
>  vswitchd/bridge.c            | 8 ++------
>  vtep/vtep-ctl.c              | 2 +-
>  16 files changed, 29 insertions(+), 35 deletions(-)
>
> diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
> index 70f320d..ff4227c 100644
> --- a/lib/dpif-netdev.c
> +++ b/lib/dpif-netdev.c
> @@ -2531,7 +2531,7 @@ dpif_netdev_pmd_set(struct dpif *dpif, const char *cmask)
>
>      if (!cmask_equals(dp->requested_pmd_cmask, cmask)) {
>          free(dp->requested_pmd_cmask);
> -        dp->requested_pmd_cmask = cmask ? xstrdup(cmask) : NULL;
> +        dp->requested_pmd_cmask = nullable_xstrdup(cmask);
>      }
>
>      return 0;
> @@ -2690,9 +2690,7 @@ reconfigure_pmd_threads(struct dp_netdev *dp)
>      /* Reconfigures the cpu mask. */
>      ovs_numa_set_cpu_mask(dp->requested_pmd_cmask);
>      free(dp->pmd_cmask);
> -    dp->pmd_cmask = dp->requested_pmd_cmask
> -                    ? xstrdup(dp->requested_pmd_cmask)
> -                    : NULL;
> +    dp->pmd_cmask = nullable_xstrdup(dp->requested_pmd_cmask);
>
>      /* Restores the non-pmd. */
>      dp_netdev_set_nonpmd(dp);
> diff --git a/lib/jsonrpc.c b/lib/jsonrpc.c
> index 1112b4a..aba742c 100644
> --- a/lib/jsonrpc.c
> +++ b/lib/jsonrpc.c
> @@ -512,7 +512,7 @@ jsonrpc_create(enum jsonrpc_msg_type type, const char *method,
>  {
>      struct jsonrpc_msg *msg = xmalloc(sizeof *msg);
>      msg->type = type;
> -    msg->method = method ? xstrdup(method) : NULL;
> +    msg->method = nullable_xstrdup(method);
>      msg->params = params;
>      msg->result = result;
>      msg->error = error;
> diff --git a/lib/ovsdb-error.c b/lib/ovsdb-error.c
> index d3549cb..dbe8149 100644
> --- a/lib/ovsdb-error.c
> +++ b/lib/ovsdb-error.c
> @@ -1,4 +1,4 @@
> -/* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
> +/* Copyright (c) 2009, 2010, 2011, 2012, 2016 Nicira, Inc.
>   *
>   * Licensed under the Apache License, Version 2.0 (the "License");
>   * you may not use this file except in compliance with the License.
> @@ -183,8 +183,8 @@ ovsdb_error_clone(const struct ovsdb_error *old)
>      if (old) {
>          struct ovsdb_error *new = xmalloc(sizeof *new);
>          new->tag = old->tag;
> -        new->details = old->details ? xstrdup(old->details) : NULL;
> -        new->syntax = old->syntax ? xstrdup(old->syntax) : NULL;
> +        new->details = nullable_xstrdup(old->details);
> +        new->syntax = nullable_xstrdup(old->syntax);
>          new->errno_ = old->errno_;
>          return new;
>      } else {
> diff --git a/lib/syslog-libc.c b/lib/syslog-libc.c
> index 8858c3c..b702d41 100644
> --- a/lib/syslog-libc.c
> +++ b/lib/syslog-libc.c
> @@ -1,5 +1,5 @@
>  /*
> - * Copyright (c) 2015 Nicira, Inc.
> + * Copyright (c) 2015, 2016 Nicira, Inc.
>   *
>   * Licensed under the Apache License, Version 2.0 (the "License");
>   * you may not use this file except in compliance with the License.
> @@ -64,7 +64,7 @@ syslog_libc_open(struct syslogger *this OVS_UNUSED, int facility)
>       * 'program_name', so make a private copy just for openlog().  (We keep
>       * a pointer to the private copy to suppress memory leak warnings in
>       * case openlog() does make its own copy.) */
> -    ident = program_name ? xstrdup(program_name) : NULL;
> +    ident = nullable_xstrdup(program_name);
>
>      openlog(ident, LOG_NDELAY, facility);
>  }
> diff --git a/lib/util.c b/lib/util.c
> index 6ca04ad..e1dc3d2 100644
> --- a/lib/util.c
> +++ b/lib/util.c
> @@ -1,5 +1,5 @@
>  /*
> - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
> + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
>   *
>   * Licensed under the Apache License, Version 2.0 (the "License");
>   * you may not use this file except in compliance with the License.
> @@ -151,6 +151,12 @@ xstrdup(const char *s)
>      return xmemdup0(s, strlen(s));
>  }
>
> +char * MALLOC_LIKE
> +nullable_xstrdup(const char *s)
> +{
> +    return s ? xstrdup(s) : NULL;
> +}
> +
>  char *
>  xvasprintf(const char *format, va_list args)
>  {
> diff --git a/lib/util.h b/lib/util.h
> index 7be4a30..e738c9f 100644
> --- a/lib/util.h
> +++ b/lib/util.h
> @@ -112,6 +112,7 @@ void *xrealloc(void *, size_t);
>  void *xmemdup(const void *, size_t) MALLOC_LIKE;
>  char *xmemdup0(const char *, size_t) MALLOC_LIKE;
>  char *xstrdup(const char *) MALLOC_LIKE;
> +char *nullable_xstrdup(const char *) MALLOC_LIKE;
>  char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE;
>  char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE;
>  void *x2nrealloc(void *p, size_t *n, size_t s);
> diff --git a/lib/vlog.c b/lib/vlog.c
> index 30b5bc2..333337b 100644
> --- a/lib/vlog.c
> +++ b/lib/vlog.c
> @@ -426,7 +426,7 @@ vlog_reopen_log_file(void)
>      char *fn;
>
>      ovs_mutex_lock(&log_file_mutex);
> -    fn = log_file_name ? xstrdup(log_file_name) : NULL;
> +    fn = nullable_xstrdup(log_file_name);
>      ovs_mutex_unlock(&log_file_mutex);
>
>      if (fn) {
> diff --git a/ofproto/ofproto-dpif-ipfix.c b/ofproto/ofproto-dpif-ipfix.c
> index 35f481d..5744abb 100644
> --- a/ofproto/ofproto-dpif-ipfix.c
> +++ b/ofproto/ofproto-dpif-ipfix.c
> @@ -469,12 +469,6 @@ nullable_string_is_equal(const char *a, const char *b)
>      return a ? b && !strcmp(a, b) : !b;
>  }
>
> -static char *
> -nullable_xstrdup(const char *s)
> -{
> -    return s ? xstrdup(s) : NULL;
> -}
> -
>  static bool
>  ofproto_ipfix_bridge_exporter_options_equal(
>      const struct ofproto_ipfix_bridge_exporter_options *a,
> diff --git a/ofproto/ofproto-dpif-sflow.c b/ofproto/ofproto-dpif-sflow.c
> index 5d26b7c..7d0aa36 100644
> --- a/ofproto/ofproto-dpif-sflow.c
> +++ b/ofproto/ofproto-dpif-sflow.c
> @@ -1,5 +1,5 @@
>  /*
> - * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
> + * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
>   * Copyright (c) 2009 InMon Corp.
>   *
>   * Licensed under the Apache License, Version 2.0 (the "License");
> @@ -115,8 +115,8 @@ ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
>  {
>      struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
>      sset_clone(&new->targets, &old->targets);
> -    new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
> -    new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
> +    new->agent_device = nullable_xstrdup(old->agent_device);
> +    new->control_ip = nullable_xstrdup(old->control_ip);
>      return new;
>  }
>
> diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
> index 087b321..cf3235a 100644
> --- a/ofproto/ofproto.c
> +++ b/ofproto/ofproto.c
> @@ -784,8 +784,7 @@ void
>  ofproto_set_cpu_mask(const char *cmask)
>  {
>      free(pmd_cpu_mask);
> -
> -    pmd_cpu_mask = cmask ? xstrdup(cmask) : NULL;
> +    pmd_cpu_mask = nullable_xstrdup(cmask);
>  }
>
>  void
> @@ -811,7 +810,7 @@ void
>  ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
>  {
>      free(p->dp_desc);
> -    p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
> +    p->dp_desc = nullable_xstrdup(dp_desc);
>  }
>
>  int
> diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
> index 7789cdd..345647a 100644
> --- a/ovn/utilities/ovn-nbctl.c
> +++ b/ovn/utilities/ovn-nbctl.c
> @@ -235,7 +235,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
>              }
>              shash_add_nocopy(local_options,
>                               xasprintf("--%s", options[idx].name),
> -                             optarg ? xstrdup(optarg) : NULL);
> +                             nullable_xstrdup(optarg));
>              break;
>
>          case 'h':
> diff --git a/ovn/utilities/ovn-sbctl.c b/ovn/utilities/ovn-sbctl.c
> index c0ee518..a12b247 100644
> --- a/ovn/utilities/ovn-sbctl.c
> +++ b/ovn/utilities/ovn-sbctl.c
> @@ -249,7 +249,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
>              }
>              shash_add_nocopy(local_options,
>                               xasprintf("--%s", options[idx].name),
> -                             optarg ? xstrdup(optarg) : NULL);
> +                             nullable_xstrdup(optarg));
>              break;
>
>          case 'h':
> diff --git a/ovsdb/replication.c b/ovsdb/replication.c
> index 1fa0f25..9bf3721 100644
> --- a/ovsdb/replication.c
> +++ b/ovsdb/replication.c
> @@ -115,7 +115,7 @@ replication_run(struct shash *all_dbs)
>  void
>  set_remote_ovsdb_server(const char *remote_server)
>  {
> -    remote_ovsdb_server = remote_server ? xstrdup(remote_server) : NULL;
> +    remote_ovsdb_server = nullable_xstrdup(remote_server);
>  }
>
>  void
> diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
> index 4d7f9f9..c4ae692 100644
> --- a/utilities/ovs-vsctl.c
> +++ b/utilities/ovs-vsctl.c
> @@ -287,7 +287,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
>              }
>              shash_add_nocopy(local_options,
>                               xasprintf("--%s", options[idx].name),
> -                             optarg ? xstrdup(optarg) : NULL);
> +                             nullable_xstrdup(optarg));
>              break;
>
>          case 'h':
> diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c
> index f8f8964..917a80c 100644
> --- a/vswitchd/bridge.c
> +++ b/vswitchd/bridge.c
> @@ -1212,9 +1212,7 @@ bridge_configure_ipfix(struct bridge *br)
>                                                "enable-output-sampling", false);
>
>          virtual_obs_id = smap_get(&be_cfg->other_config, "virtual_obs_id");
> -        be_opts.virtual_obs_id = (virtual_obs_id
> -                                  ? xstrdup(virtual_obs_id)
> -                                  : NULL);
> +        be_opts.virtual_obs_id = nullable_xstrdup(virtual_obs_id);
>      }
>
>      if (n_fe_opts > 0) {
> @@ -1236,9 +1234,7 @@ bridge_configure_ipfix(struct bridge *br)
>                                                    "enable-tunnel-sampling", true);
>                  virtual_obs_id = smap_get(&fe_cfg->ipfix->other_config,
>                                            "virtual_obs_id");
> -                opts->virtual_obs_id = (virtual_obs_id
> -                                        ? xstrdup(virtual_obs_id)
> -                                        : NULL);
> +                opts->virtual_obs_id = nullable_xstrdup(virtual_obs_id);
>                  opts++;
>              }
>          }
> diff --git a/vtep/vtep-ctl.c b/vtep/vtep-ctl.c
> index 5c18971..759150f 100644
> --- a/vtep/vtep-ctl.c
> +++ b/vtep/vtep-ctl.c
> @@ -237,7 +237,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
>              }
>              shash_add_nocopy(local_options,
>                               xasprintf("--%s", options[idx].name),
> -                             optarg ? xstrdup(optarg) : NULL);
> +                             nullable_xstrdup(optarg));
>              break;
>
>          case 'h':
> --
> 2.1.3
>
> _______________________________________________
> dev mailing list
> dev@openvswitch.org
> http://openvswitch.org/mailman/listinfo/dev
Ben Pfaff June 27, 2016, 3:33 a.m. UTC | #2
Thanks, applied to master.

On Sat, Jun 25, 2016 at 01:09:40PM -0700, William Tu wrote:
> Looks good to me.
> 
> Acked-by: William Tu <u9012063@gmail.com>
> 
> On Fri, Jun 24, 2016 at 9:23 PM, Ben Pfaff <blp@ovn.org> wrote:
> > It's a pretty common pattern so create a function for it.
> >
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> > ---
> >  lib/dpif-netdev.c            | 6 ++----
> >  lib/jsonrpc.c                | 2 +-
> >  lib/ovsdb-error.c            | 6 +++---
> >  lib/syslog-libc.c            | 4 ++--
> >  lib/util.c                   | 8 +++++++-
> >  lib/util.h                   | 1 +
> >  lib/vlog.c                   | 2 +-
> >  ofproto/ofproto-dpif-ipfix.c | 6 ------
> >  ofproto/ofproto-dpif-sflow.c | 6 +++---
> >  ofproto/ofproto.c            | 5 ++---
> >  ovn/utilities/ovn-nbctl.c    | 2 +-
> >  ovn/utilities/ovn-sbctl.c    | 2 +-
> >  ovsdb/replication.c          | 2 +-
> >  utilities/ovs-vsctl.c        | 2 +-
> >  vswitchd/bridge.c            | 8 ++------
> >  vtep/vtep-ctl.c              | 2 +-
> >  16 files changed, 29 insertions(+), 35 deletions(-)
> >
> > diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
> > index 70f320d..ff4227c 100644
> > --- a/lib/dpif-netdev.c
> > +++ b/lib/dpif-netdev.c
> > @@ -2531,7 +2531,7 @@ dpif_netdev_pmd_set(struct dpif *dpif, const char *cmask)
> >
> >      if (!cmask_equals(dp->requested_pmd_cmask, cmask)) {
> >          free(dp->requested_pmd_cmask);
> > -        dp->requested_pmd_cmask = cmask ? xstrdup(cmask) : NULL;
> > +        dp->requested_pmd_cmask = nullable_xstrdup(cmask);
> >      }
> >
> >      return 0;
> > @@ -2690,9 +2690,7 @@ reconfigure_pmd_threads(struct dp_netdev *dp)
> >      /* Reconfigures the cpu mask. */
> >      ovs_numa_set_cpu_mask(dp->requested_pmd_cmask);
> >      free(dp->pmd_cmask);
> > -    dp->pmd_cmask = dp->requested_pmd_cmask
> > -                    ? xstrdup(dp->requested_pmd_cmask)
> > -                    : NULL;
> > +    dp->pmd_cmask = nullable_xstrdup(dp->requested_pmd_cmask);
> >
> >      /* Restores the non-pmd. */
> >      dp_netdev_set_nonpmd(dp);
> > diff --git a/lib/jsonrpc.c b/lib/jsonrpc.c
> > index 1112b4a..aba742c 100644
> > --- a/lib/jsonrpc.c
> > +++ b/lib/jsonrpc.c
> > @@ -512,7 +512,7 @@ jsonrpc_create(enum jsonrpc_msg_type type, const char *method,
> >  {
> >      struct jsonrpc_msg *msg = xmalloc(sizeof *msg);
> >      msg->type = type;
> > -    msg->method = method ? xstrdup(method) : NULL;
> > +    msg->method = nullable_xstrdup(method);
> >      msg->params = params;
> >      msg->result = result;
> >      msg->error = error;
> > diff --git a/lib/ovsdb-error.c b/lib/ovsdb-error.c
> > index d3549cb..dbe8149 100644
> > --- a/lib/ovsdb-error.c
> > +++ b/lib/ovsdb-error.c
> > @@ -1,4 +1,4 @@
> > -/* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
> > +/* Copyright (c) 2009, 2010, 2011, 2012, 2016 Nicira, Inc.
> >   *
> >   * Licensed under the Apache License, Version 2.0 (the "License");
> >   * you may not use this file except in compliance with the License.
> > @@ -183,8 +183,8 @@ ovsdb_error_clone(const struct ovsdb_error *old)
> >      if (old) {
> >          struct ovsdb_error *new = xmalloc(sizeof *new);
> >          new->tag = old->tag;
> > -        new->details = old->details ? xstrdup(old->details) : NULL;
> > -        new->syntax = old->syntax ? xstrdup(old->syntax) : NULL;
> > +        new->details = nullable_xstrdup(old->details);
> > +        new->syntax = nullable_xstrdup(old->syntax);
> >          new->errno_ = old->errno_;
> >          return new;
> >      } else {
> > diff --git a/lib/syslog-libc.c b/lib/syslog-libc.c
> > index 8858c3c..b702d41 100644
> > --- a/lib/syslog-libc.c
> > +++ b/lib/syslog-libc.c
> > @@ -1,5 +1,5 @@
> >  /*
> > - * Copyright (c) 2015 Nicira, Inc.
> > + * Copyright (c) 2015, 2016 Nicira, Inc.
> >   *
> >   * Licensed under the Apache License, Version 2.0 (the "License");
> >   * you may not use this file except in compliance with the License.
> > @@ -64,7 +64,7 @@ syslog_libc_open(struct syslogger *this OVS_UNUSED, int facility)
> >       * 'program_name', so make a private copy just for openlog().  (We keep
> >       * a pointer to the private copy to suppress memory leak warnings in
> >       * case openlog() does make its own copy.) */
> > -    ident = program_name ? xstrdup(program_name) : NULL;
> > +    ident = nullable_xstrdup(program_name);
> >
> >      openlog(ident, LOG_NDELAY, facility);
> >  }
> > diff --git a/lib/util.c b/lib/util.c
> > index 6ca04ad..e1dc3d2 100644
> > --- a/lib/util.c
> > +++ b/lib/util.c
> > @@ -1,5 +1,5 @@
> >  /*
> > - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
> > + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
> >   *
> >   * Licensed under the Apache License, Version 2.0 (the "License");
> >   * you may not use this file except in compliance with the License.
> > @@ -151,6 +151,12 @@ xstrdup(const char *s)
> >      return xmemdup0(s, strlen(s));
> >  }
> >
> > +char * MALLOC_LIKE
> > +nullable_xstrdup(const char *s)
> > +{
> > +    return s ? xstrdup(s) : NULL;
> > +}
> > +
> >  char *
> >  xvasprintf(const char *format, va_list args)
> >  {
> > diff --git a/lib/util.h b/lib/util.h
> > index 7be4a30..e738c9f 100644
> > --- a/lib/util.h
> > +++ b/lib/util.h
> > @@ -112,6 +112,7 @@ void *xrealloc(void *, size_t);
> >  void *xmemdup(const void *, size_t) MALLOC_LIKE;
> >  char *xmemdup0(const char *, size_t) MALLOC_LIKE;
> >  char *xstrdup(const char *) MALLOC_LIKE;
> > +char *nullable_xstrdup(const char *) MALLOC_LIKE;
> >  char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE;
> >  char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE;
> >  void *x2nrealloc(void *p, size_t *n, size_t s);
> > diff --git a/lib/vlog.c b/lib/vlog.c
> > index 30b5bc2..333337b 100644
> > --- a/lib/vlog.c
> > +++ b/lib/vlog.c
> > @@ -426,7 +426,7 @@ vlog_reopen_log_file(void)
> >      char *fn;
> >
> >      ovs_mutex_lock(&log_file_mutex);
> > -    fn = log_file_name ? xstrdup(log_file_name) : NULL;
> > +    fn = nullable_xstrdup(log_file_name);
> >      ovs_mutex_unlock(&log_file_mutex);
> >
> >      if (fn) {
> > diff --git a/ofproto/ofproto-dpif-ipfix.c b/ofproto/ofproto-dpif-ipfix.c
> > index 35f481d..5744abb 100644
> > --- a/ofproto/ofproto-dpif-ipfix.c
> > +++ b/ofproto/ofproto-dpif-ipfix.c
> > @@ -469,12 +469,6 @@ nullable_string_is_equal(const char *a, const char *b)
> >      return a ? b && !strcmp(a, b) : !b;
> >  }
> >
> > -static char *
> > -nullable_xstrdup(const char *s)
> > -{
> > -    return s ? xstrdup(s) : NULL;
> > -}
> > -
> >  static bool
> >  ofproto_ipfix_bridge_exporter_options_equal(
> >      const struct ofproto_ipfix_bridge_exporter_options *a,
> > diff --git a/ofproto/ofproto-dpif-sflow.c b/ofproto/ofproto-dpif-sflow.c
> > index 5d26b7c..7d0aa36 100644
> > --- a/ofproto/ofproto-dpif-sflow.c
> > +++ b/ofproto/ofproto-dpif-sflow.c
> > @@ -1,5 +1,5 @@
> >  /*
> > - * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
> > + * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
> >   * Copyright (c) 2009 InMon Corp.
> >   *
> >   * Licensed under the Apache License, Version 2.0 (the "License");
> > @@ -115,8 +115,8 @@ ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
> >  {
> >      struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
> >      sset_clone(&new->targets, &old->targets);
> > -    new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
> > -    new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
> > +    new->agent_device = nullable_xstrdup(old->agent_device);
> > +    new->control_ip = nullable_xstrdup(old->control_ip);
> >      return new;
> >  }
> >
> > diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
> > index 087b321..cf3235a 100644
> > --- a/ofproto/ofproto.c
> > +++ b/ofproto/ofproto.c
> > @@ -784,8 +784,7 @@ void
> >  ofproto_set_cpu_mask(const char *cmask)
> >  {
> >      free(pmd_cpu_mask);
> > -
> > -    pmd_cpu_mask = cmask ? xstrdup(cmask) : NULL;
> > +    pmd_cpu_mask = nullable_xstrdup(cmask);
> >  }
> >
> >  void
> > @@ -811,7 +810,7 @@ void
> >  ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
> >  {
> >      free(p->dp_desc);
> > -    p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
> > +    p->dp_desc = nullable_xstrdup(dp_desc);
> >  }
> >
> >  int
> > diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
> > index 7789cdd..345647a 100644
> > --- a/ovn/utilities/ovn-nbctl.c
> > +++ b/ovn/utilities/ovn-nbctl.c
> > @@ -235,7 +235,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
> >              }
> >              shash_add_nocopy(local_options,
> >                               xasprintf("--%s", options[idx].name),
> > -                             optarg ? xstrdup(optarg) : NULL);
> > +                             nullable_xstrdup(optarg));
> >              break;
> >
> >          case 'h':
> > diff --git a/ovn/utilities/ovn-sbctl.c b/ovn/utilities/ovn-sbctl.c
> > index c0ee518..a12b247 100644
> > --- a/ovn/utilities/ovn-sbctl.c
> > +++ b/ovn/utilities/ovn-sbctl.c
> > @@ -249,7 +249,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
> >              }
> >              shash_add_nocopy(local_options,
> >                               xasprintf("--%s", options[idx].name),
> > -                             optarg ? xstrdup(optarg) : NULL);
> > +                             nullable_xstrdup(optarg));
> >              break;
> >
> >          case 'h':
> > diff --git a/ovsdb/replication.c b/ovsdb/replication.c
> > index 1fa0f25..9bf3721 100644
> > --- a/ovsdb/replication.c
> > +++ b/ovsdb/replication.c
> > @@ -115,7 +115,7 @@ replication_run(struct shash *all_dbs)
> >  void
> >  set_remote_ovsdb_server(const char *remote_server)
> >  {
> > -    remote_ovsdb_server = remote_server ? xstrdup(remote_server) : NULL;
> > +    remote_ovsdb_server = nullable_xstrdup(remote_server);
> >  }
> >
> >  void
> > diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
> > index 4d7f9f9..c4ae692 100644
> > --- a/utilities/ovs-vsctl.c
> > +++ b/utilities/ovs-vsctl.c
> > @@ -287,7 +287,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
> >              }
> >              shash_add_nocopy(local_options,
> >                               xasprintf("--%s", options[idx].name),
> > -                             optarg ? xstrdup(optarg) : NULL);
> > +                             nullable_xstrdup(optarg));
> >              break;
> >
> >          case 'h':
> > diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c
> > index f8f8964..917a80c 100644
> > --- a/vswitchd/bridge.c
> > +++ b/vswitchd/bridge.c
> > @@ -1212,9 +1212,7 @@ bridge_configure_ipfix(struct bridge *br)
> >                                                "enable-output-sampling", false);
> >
> >          virtual_obs_id = smap_get(&be_cfg->other_config, "virtual_obs_id");
> > -        be_opts.virtual_obs_id = (virtual_obs_id
> > -                                  ? xstrdup(virtual_obs_id)
> > -                                  : NULL);
> > +        be_opts.virtual_obs_id = nullable_xstrdup(virtual_obs_id);
> >      }
> >
> >      if (n_fe_opts > 0) {
> > @@ -1236,9 +1234,7 @@ bridge_configure_ipfix(struct bridge *br)
> >                                                    "enable-tunnel-sampling", true);
> >                  virtual_obs_id = smap_get(&fe_cfg->ipfix->other_config,
> >                                            "virtual_obs_id");
> > -                opts->virtual_obs_id = (virtual_obs_id
> > -                                        ? xstrdup(virtual_obs_id)
> > -                                        : NULL);
> > +                opts->virtual_obs_id = nullable_xstrdup(virtual_obs_id);
> >                  opts++;
> >              }
> >          }
> > diff --git a/vtep/vtep-ctl.c b/vtep/vtep-ctl.c
> > index 5c18971..759150f 100644
> > --- a/vtep/vtep-ctl.c
> > +++ b/vtep/vtep-ctl.c
> > @@ -237,7 +237,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
> >              }
> >              shash_add_nocopy(local_options,
> >                               xasprintf("--%s", options[idx].name),
> > -                             optarg ? xstrdup(optarg) : NULL);
> > +                             nullable_xstrdup(optarg));
> >              break;
> >
> >          case 'h':
> > --
> > 2.1.3
> >
> > _______________________________________________
> > dev mailing list
> > dev@openvswitch.org
> > http://openvswitch.org/mailman/listinfo/dev
diff mbox

Patch

diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
index 70f320d..ff4227c 100644
--- a/lib/dpif-netdev.c
+++ b/lib/dpif-netdev.c
@@ -2531,7 +2531,7 @@  dpif_netdev_pmd_set(struct dpif *dpif, const char *cmask)
 
     if (!cmask_equals(dp->requested_pmd_cmask, cmask)) {
         free(dp->requested_pmd_cmask);
-        dp->requested_pmd_cmask = cmask ? xstrdup(cmask) : NULL;
+        dp->requested_pmd_cmask = nullable_xstrdup(cmask);
     }
 
     return 0;
@@ -2690,9 +2690,7 @@  reconfigure_pmd_threads(struct dp_netdev *dp)
     /* Reconfigures the cpu mask. */
     ovs_numa_set_cpu_mask(dp->requested_pmd_cmask);
     free(dp->pmd_cmask);
-    dp->pmd_cmask = dp->requested_pmd_cmask
-                    ? xstrdup(dp->requested_pmd_cmask)
-                    : NULL;
+    dp->pmd_cmask = nullable_xstrdup(dp->requested_pmd_cmask);
 
     /* Restores the non-pmd. */
     dp_netdev_set_nonpmd(dp);
diff --git a/lib/jsonrpc.c b/lib/jsonrpc.c
index 1112b4a..aba742c 100644
--- a/lib/jsonrpc.c
+++ b/lib/jsonrpc.c
@@ -512,7 +512,7 @@  jsonrpc_create(enum jsonrpc_msg_type type, const char *method,
 {
     struct jsonrpc_msg *msg = xmalloc(sizeof *msg);
     msg->type = type;
-    msg->method = method ? xstrdup(method) : NULL;
+    msg->method = nullable_xstrdup(method);
     msg->params = params;
     msg->result = result;
     msg->error = error;
diff --git a/lib/ovsdb-error.c b/lib/ovsdb-error.c
index d3549cb..dbe8149 100644
--- a/lib/ovsdb-error.c
+++ b/lib/ovsdb-error.c
@@ -1,4 +1,4 @@ 
-/* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
+/* Copyright (c) 2009, 2010, 2011, 2012, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -183,8 +183,8 @@  ovsdb_error_clone(const struct ovsdb_error *old)
     if (old) {
         struct ovsdb_error *new = xmalloc(sizeof *new);
         new->tag = old->tag;
-        new->details = old->details ? xstrdup(old->details) : NULL;
-        new->syntax = old->syntax ? xstrdup(old->syntax) : NULL;
+        new->details = nullable_xstrdup(old->details);
+        new->syntax = nullable_xstrdup(old->syntax);
         new->errno_ = old->errno_;
         return new;
     } else {
diff --git a/lib/syslog-libc.c b/lib/syslog-libc.c
index 8858c3c..b702d41 100644
--- a/lib/syslog-libc.c
+++ b/lib/syslog-libc.c
@@ -1,5 +1,5 @@ 
 /*
- * Copyright (c) 2015 Nicira, Inc.
+ * Copyright (c) 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -64,7 +64,7 @@  syslog_libc_open(struct syslogger *this OVS_UNUSED, int facility)
      * 'program_name', so make a private copy just for openlog().  (We keep
      * a pointer to the private copy to suppress memory leak warnings in
      * case openlog() does make its own copy.) */
-    ident = program_name ? xstrdup(program_name) : NULL;
+    ident = nullable_xstrdup(program_name);
 
     openlog(ident, LOG_NDELAY, facility);
 }
diff --git a/lib/util.c b/lib/util.c
index 6ca04ad..e1dc3d2 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -1,5 +1,5 @@ 
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -151,6 +151,12 @@  xstrdup(const char *s)
     return xmemdup0(s, strlen(s));
 }
 
+char * MALLOC_LIKE
+nullable_xstrdup(const char *s)
+{
+    return s ? xstrdup(s) : NULL;
+}
+
 char *
 xvasprintf(const char *format, va_list args)
 {
diff --git a/lib/util.h b/lib/util.h
index 7be4a30..e738c9f 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -112,6 +112,7 @@  void *xrealloc(void *, size_t);
 void *xmemdup(const void *, size_t) MALLOC_LIKE;
 char *xmemdup0(const char *, size_t) MALLOC_LIKE;
 char *xstrdup(const char *) MALLOC_LIKE;
+char *nullable_xstrdup(const char *) MALLOC_LIKE;
 char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE;
 char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE;
 void *x2nrealloc(void *p, size_t *n, size_t s);
diff --git a/lib/vlog.c b/lib/vlog.c
index 30b5bc2..333337b 100644
--- a/lib/vlog.c
+++ b/lib/vlog.c
@@ -426,7 +426,7 @@  vlog_reopen_log_file(void)
     char *fn;
 
     ovs_mutex_lock(&log_file_mutex);
-    fn = log_file_name ? xstrdup(log_file_name) : NULL;
+    fn = nullable_xstrdup(log_file_name);
     ovs_mutex_unlock(&log_file_mutex);
 
     if (fn) {
diff --git a/ofproto/ofproto-dpif-ipfix.c b/ofproto/ofproto-dpif-ipfix.c
index 35f481d..5744abb 100644
--- a/ofproto/ofproto-dpif-ipfix.c
+++ b/ofproto/ofproto-dpif-ipfix.c
@@ -469,12 +469,6 @@  nullable_string_is_equal(const char *a, const char *b)
     return a ? b && !strcmp(a, b) : !b;
 }
 
-static char *
-nullable_xstrdup(const char *s)
-{
-    return s ? xstrdup(s) : NULL;
-}
-
 static bool
 ofproto_ipfix_bridge_exporter_options_equal(
     const struct ofproto_ipfix_bridge_exporter_options *a,
diff --git a/ofproto/ofproto-dpif-sflow.c b/ofproto/ofproto-dpif-sflow.c
index 5d26b7c..7d0aa36 100644
--- a/ofproto/ofproto-dpif-sflow.c
+++ b/ofproto/ofproto-dpif-sflow.c
@@ -1,5 +1,5 @@ 
 /*
- * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
  * Copyright (c) 2009 InMon Corp.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -115,8 +115,8 @@  ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
 {
     struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
     sset_clone(&new->targets, &old->targets);
-    new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
-    new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
+    new->agent_device = nullable_xstrdup(old->agent_device);
+    new->control_ip = nullable_xstrdup(old->control_ip);
     return new;
 }
 
diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
index 087b321..cf3235a 100644
--- a/ofproto/ofproto.c
+++ b/ofproto/ofproto.c
@@ -784,8 +784,7 @@  void
 ofproto_set_cpu_mask(const char *cmask)
 {
     free(pmd_cpu_mask);
-
-    pmd_cpu_mask = cmask ? xstrdup(cmask) : NULL;
+    pmd_cpu_mask = nullable_xstrdup(cmask);
 }
 
 void
@@ -811,7 +810,7 @@  void
 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
 {
     free(p->dp_desc);
-    p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
+    p->dp_desc = nullable_xstrdup(dp_desc);
 }
 
 int
diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
index 7789cdd..345647a 100644
--- a/ovn/utilities/ovn-nbctl.c
+++ b/ovn/utilities/ovn-nbctl.c
@@ -235,7 +235,7 @@  parse_options(int argc, char *argv[], struct shash *local_options)
             }
             shash_add_nocopy(local_options,
                              xasprintf("--%s", options[idx].name),
-                             optarg ? xstrdup(optarg) : NULL);
+                             nullable_xstrdup(optarg));
             break;
 
         case 'h':
diff --git a/ovn/utilities/ovn-sbctl.c b/ovn/utilities/ovn-sbctl.c
index c0ee518..a12b247 100644
--- a/ovn/utilities/ovn-sbctl.c
+++ b/ovn/utilities/ovn-sbctl.c
@@ -249,7 +249,7 @@  parse_options(int argc, char *argv[], struct shash *local_options)
             }
             shash_add_nocopy(local_options,
                              xasprintf("--%s", options[idx].name),
-                             optarg ? xstrdup(optarg) : NULL);
+                             nullable_xstrdup(optarg));
             break;
 
         case 'h':
diff --git a/ovsdb/replication.c b/ovsdb/replication.c
index 1fa0f25..9bf3721 100644
--- a/ovsdb/replication.c
+++ b/ovsdb/replication.c
@@ -115,7 +115,7 @@  replication_run(struct shash *all_dbs)
 void
 set_remote_ovsdb_server(const char *remote_server)
 {
-    remote_ovsdb_server = remote_server ? xstrdup(remote_server) : NULL;
+    remote_ovsdb_server = nullable_xstrdup(remote_server);
 }
 
 void
diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
index 4d7f9f9..c4ae692 100644
--- a/utilities/ovs-vsctl.c
+++ b/utilities/ovs-vsctl.c
@@ -287,7 +287,7 @@  parse_options(int argc, char *argv[], struct shash *local_options)
             }
             shash_add_nocopy(local_options,
                              xasprintf("--%s", options[idx].name),
-                             optarg ? xstrdup(optarg) : NULL);
+                             nullable_xstrdup(optarg));
             break;
 
         case 'h':
diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c
index f8f8964..917a80c 100644
--- a/vswitchd/bridge.c
+++ b/vswitchd/bridge.c
@@ -1212,9 +1212,7 @@  bridge_configure_ipfix(struct bridge *br)
                                               "enable-output-sampling", false);
 
         virtual_obs_id = smap_get(&be_cfg->other_config, "virtual_obs_id");
-        be_opts.virtual_obs_id = (virtual_obs_id
-                                  ? xstrdup(virtual_obs_id)
-                                  : NULL);
+        be_opts.virtual_obs_id = nullable_xstrdup(virtual_obs_id);
     }
 
     if (n_fe_opts > 0) {
@@ -1236,9 +1234,7 @@  bridge_configure_ipfix(struct bridge *br)
                                                   "enable-tunnel-sampling", true);
                 virtual_obs_id = smap_get(&fe_cfg->ipfix->other_config,
                                           "virtual_obs_id");
-                opts->virtual_obs_id = (virtual_obs_id
-                                        ? xstrdup(virtual_obs_id)
-                                        : NULL);
+                opts->virtual_obs_id = nullable_xstrdup(virtual_obs_id);
                 opts++;
             }
         }
diff --git a/vtep/vtep-ctl.c b/vtep/vtep-ctl.c
index 5c18971..759150f 100644
--- a/vtep/vtep-ctl.c
+++ b/vtep/vtep-ctl.c
@@ -237,7 +237,7 @@  parse_options(int argc, char *argv[], struct shash *local_options)
             }
             shash_add_nocopy(local_options,
                              xasprintf("--%s", options[idx].name),
-                             optarg ? xstrdup(optarg) : NULL);
+                             nullable_xstrdup(optarg));
             break;
 
         case 'h':