Add funtions:
    str_is_null_or_empty()
    nullable_strlen()

Signed-off-by: Changliang Wu <[email protected]>
---
 lib/colors.c    |  2 +-
 lib/lldp/lldp.c |  7 ++++---
 lib/util.h      | 12 ++++++++++++
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/lib/colors.c b/lib/colors.c
index 13456445e..2836c0c28 100644
--- a/lib/colors.c
+++ b/lib/colors.c
@@ -117,7 +117,7 @@ colors_parse_from_env(const struct color_key color_dic[])
          token != NULL;
          token = strsep(&s, ":")) {
         char *name = strsep(&token, "=");
-        for (char *ptr = token; ptr != NULL && *ptr != '\0'; ptr++) {
+        for (char *ptr = token; !str_is_null_or_empty(ptr); ptr++) {
             /* We accept only decimals and ';' for color marker. */
             if (*ptr == ';' || (*ptr >= '0' && *ptr <= '9')) {
                 continue;
diff --git a/lib/lldp/lldp.c b/lib/lldp/lldp.c
index 6fdcfef56..86e0381d6 100644
--- a/lib/lldp/lldp.c
+++ b/lib/lldp/lldp.c
@@ -28,6 +28,7 @@
 #include "compiler.h"
 #include "dp-packet.h"
 #include "packets.h"
+#include "util.h"
 
 VLOG_DEFINE_THIS_MODULE(lldp);
 
@@ -191,14 +192,14 @@ lldp_send(struct lldpd *global OVS_UNUSED,
     lldp_tlv_end(p, start);
 
     /* System name */
-    if (chassis->c_name && *chassis->c_name != '\0') {
+    if (!str_is_null_or_empty(chassis->c_name)) {
         lldp_tlv_start(p, LLDP_TLV_SYSTEM_NAME, &start);
         dp_packet_put(p, chassis->c_name, strlen(chassis->c_name));
         lldp_tlv_end(p, start);
     }
 
     /* System description (skip it if empty) */
-    if (chassis->c_descr && *chassis->c_descr != '\0') {
+    if (!str_is_null_or_empty(chassis->c_descr)) {
         lldp_tlv_start(p, LLDP_TLV_SYSTEM_DESCR, &start);
         dp_packet_put(p, chassis->c_descr, strlen(chassis->c_descr));
         lldp_tlv_end(p, start);
@@ -231,7 +232,7 @@ lldp_send(struct lldpd *global OVS_UNUSED,
     }
 
     /* Port description */
-    if (port->p_descr && *port->p_descr != '\0') {
+    if (!str_is_null_or_empty(port->p_descr)) {
         lldp_tlv_start(p, LLDP_TLV_PORT_DESCR, &start);
         dp_packet_put(p, port->p_descr, strlen(port->p_descr));
         lldp_tlv_end(p, start);
diff --git a/lib/util.h b/lib/util.h
index ef993626a..e0613ae9c 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -197,6 +197,18 @@ void free_pagealign(void *);
 OVS_RETURNS_NONNULL void *xmalloc_size_align(size_t, size_t) MALLOC_LIKE;
 void free_size_align(void *);
 
+static inline bool
+str_is_null_or_empty(const char *s)
+{
+    return !s || *s == '\0';
+}
+
+static inline int
+nullable_strlen(const char *s)
+{
+    return s ? strlen(s) : 0;
+}
+
 /* The C standards say that neither the 'dst' nor 'src' argument to
  * memcpy() may be null, even if 'n' is zero.  This wrapper tolerates
  * the null case. */
-- 
2.43.5

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to