1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-27 07:22:31 +03:00

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.
This commit is contained in:
Lennart Poettering 2017-07-20 11:38:15 +02:00
parent 6f8cbcdb27
commit 7bf7ce28b5
8 changed files with 24 additions and 7 deletions

View File

@ -23,9 +23,11 @@
#include <stdlib.h>
#include <string.h>
#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));
}

View File

@ -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)

View File

@ -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;

View File

@ -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 (;;) {

View File

@ -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);
}

View File

@ -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*) +

View File

@ -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));

View File

@ -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;
}