From 7bf7ce28b5a1f589f0f2382e54c03ab9b0794fab Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 20 Jul 2017 11:38:15 +0200 Subject: [PATCH] string-util: add strlen_ptr() helper strlen_ptr() is to strlen() what streq_ptr() is to streq(): i.e. it handles NULL strings in a smart way. --- src/basic/bus-label.h | 4 +++- src/basic/escape.c | 2 +- src/basic/hexdecoct.c | 3 ++- src/basic/string-util.c | 4 ++-- src/basic/string-util.h | 7 +++++++ src/nss-myhostname/nss-myhostname.c | 2 +- src/systemctl/systemctl.c | 2 +- src/test/test-string-util.c | 7 +++++++ 8 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/basic/bus-label.h b/src/basic/bus-label.h index 62fb2c450c..600268b767 100644 --- a/src/basic/bus-label.h +++ b/src/basic/bus-label.h @@ -23,9 +23,11 @@ #include #include +#include "string-util.h" + char *bus_label_escape(const char *s); char *bus_label_unescape_n(const char *f, size_t l); static inline char *bus_label_unescape(const char *f) { - return bus_label_unescape_n(f, f ? strlen(f) : 0); + return bus_label_unescape_n(f, strlen_ptr(f)); } diff --git a/src/basic/escape.c b/src/basic/escape.c index 85e4b5282e..22b8b04156 100644 --- a/src/basic/escape.c +++ b/src/basic/escape.c @@ -314,7 +314,7 @@ int cunescape_length_with_prefix(const char *s, size_t length, const char *prefi /* Undoes C style string escaping, and optionally prefixes it. */ - pl = prefix ? strlen(prefix) : 0; + pl = strlen_ptr(prefix); r = new(char, pl+length+1); if (!r) diff --git a/src/basic/hexdecoct.c b/src/basic/hexdecoct.c index 2d6e377f0a..766770389c 100644 --- a/src/basic/hexdecoct.c +++ b/src/basic/hexdecoct.c @@ -25,6 +25,7 @@ #include "alloc-util.h" #include "hexdecoct.h" #include "macro.h" +#include "string-util.h" #include "util.h" char octchar(int x) { @@ -569,7 +570,7 @@ static int base64_append_width(char **prefix, int plen, lines = (len + width - 1) / width; - slen = sep ? strlen(sep) : 0; + slen = strlen_ptr(sep); t = realloc(*prefix, plen + 1 + slen + (indent + width + 1) * lines); if (!t) return -ENOMEM; diff --git a/src/basic/string-util.c b/src/basic/string-util.c index cd58ef97ef..3287865863 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -215,7 +215,7 @@ char *strnappend(const char *s, const char *suffix, size_t b) { } char *strappend(const char *s, const char *suffix) { - return strnappend(s, suffix, suffix ? strlen(suffix) : 0); + return strnappend(s, suffix, strlen_ptr(suffix)); } char *strjoin_real(const char *x, ...) { @@ -707,7 +707,7 @@ char *strextend(char **x, ...) { assert(x); - l = f = *x ? strlen(*x) : 0; + l = f = strlen_ptr(*x); va_start(ap, x); for (;;) { diff --git a/src/basic/string-util.h b/src/basic/string-util.h index e8a0836538..34eb952ce9 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -200,3 +200,10 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase); #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep) bool string_is_safe(const char *p) _pure_; + +static inline size_t strlen_ptr(const char *s) { + if (!s) + return 0; + + return strlen(s); +} diff --git a/src/nss-myhostname/nss-myhostname.c b/src/nss-myhostname/nss-myhostname.c index 0570fde592..869d233d49 100644 --- a/src/nss-myhostname/nss-myhostname.c +++ b/src/nss-myhostname/nss-myhostname.c @@ -211,7 +211,7 @@ static enum nss_status fill_in_hostent( c++; l_canonical = strlen(canonical); - l_additional = additional ? strlen(additional) : 0; + l_additional = strlen_ptr(additional); ms = ALIGN(l_canonical+1)+ (additional ? ALIGN(l_additional+1) : 0) + sizeof(char*) + diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 55fce62480..36675dc499 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -2010,7 +2010,7 @@ static void output_machines_list(struct machine_info *machine_infos, unsigned n) for (m = machine_infos; m < machine_infos + n; m++) { namelen = MAX(namelen, strlen(m->name) + (m->is_host ? sizeof(" (host)") - 1 : 0)); - statelen = MAX(statelen, m->state ? strlen(m->state) : 0); + statelen = MAX(statelen, strlen_ptr(m->state)); failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units)); jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs)); diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index 4b3e924cfb..604701ff7a 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -336,6 +336,12 @@ static void test_first_word(void) { assert_se(!first_word("Hellooo", "Hello")); } +static void test_strlen_ptr(void) { + assert_se(strlen_ptr("foo") == 3); + assert_se(strlen_ptr("") == 0); + assert_se(strlen_ptr(NULL) == 0); +} + int main(int argc, char *argv[]) { test_string_erase(); test_ascii_strcasecmp_n(); @@ -358,6 +364,7 @@ int main(int argc, char *argv[]) { test_in_charset(); test_split_pair(); test_first_word(); + test_strlen_ptr(); return 0; }