Use strlen even for constant strings

GCC optimizes strlen("string constant") to a constant, even with -O0.
Thus, replace patterns like sizeof("string constant")-1 with
strlen("string constant") where possible, for clarity.  In particular,
for expressions intended to add up the lengths of components going into
a string, this often makes it clearer that the expression counts the
trailing '\0' exactly once, by putting the +1 for the '\0' at the end of
the expression, rather than hidden in a sizeof in the middle of the
expression.
This commit is contained in:
Josh Triplett 2014-03-15 11:40:07 -07:00 committed by Zbigniew Jędrzejewski-Szmek
parent 039dd4afd6
commit f8294e4175
13 changed files with 40 additions and 40 deletions

View file

@ -953,7 +953,7 @@ static int bus_init_private(Manager *m) {
return 0;
strcpy(sa.un.sun_path, "/run/systemd/private");
salen = offsetof(union sockaddr_union, un.sun_path) + sizeof("/run/systemd/private") - 1;
salen = offsetof(union sockaddr_union, un.sun_path) + strlen("/run/systemd/private");
} else {
size_t left = sizeof(sa.un.sun_path);
char *p = sa.un.sun_path;

View file

@ -361,7 +361,7 @@ static int service_arm_timer(Service *s, usec_t usec) {
static char *sysv_translate_name(const char *name) {
char *r;
r = new(char, strlen(name) + sizeof(".service"));
r = new(char, strlen(name) + strlen(".service") + 1);
if (!r)
return NULL;

View file

@ -686,7 +686,7 @@ static void dispatch_message_real(
#ifdef HAVE_SELINUX
if (use_selinux()) {
if (label) {
x = alloca(sizeof("_SELINUX_CONTEXT=") + label_len);
x = alloca(strlen("_SELINUX_CONTEXT=") + label_len + 1);
*((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
IOVEC_SET_STRING(iovec[n++], x);

View file

@ -46,7 +46,7 @@ static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned
.msg_iovlen = n_iovec,
.msg_name = &sa,
.msg_namelen = offsetof(union sockaddr_union, un.sun_path)
+ sizeof("/run/systemd/journal/syslog") - 1,
+ strlen("/run/systemd/journal/syslog"),
};
struct cmsghdr *cmsg;
union {

View file

@ -1390,7 +1390,7 @@ int bus_kernel_create_starter(const char *bus, const char *name, BusNamePolicy *
assert(bus);
assert(name);
p = alloca(sizeof("/dev/kdbus/") - 1 + DECIMAL_STR_MAX(uid_t) + 1 + strlen(bus) + sizeof("/bus"));
p = alloca(strlen("/dev/kdbus/") + DECIMAL_STR_MAX(uid_t) + 1 + strlen(bus) + strlen("/bus") + 1);
sprintf(p, "/dev/kdbus/%lu-%s/bus", (unsigned long) getuid(), bus);
fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
@ -1502,7 +1502,7 @@ int bus_kernel_create_monitor(const char *bus) {
assert(bus);
p = alloca(sizeof("/dev/kdbus/") - 1 + DECIMAL_STR_MAX(uid_t) + 1 + strlen(bus) + sizeof("/bus"));
p = alloca(strlen("/dev/kdbus/") + DECIMAL_STR_MAX(uid_t) + 1 + strlen(bus) + strlen("/bus") + 1);
sprintf(p, "/dev/kdbus/%lu-%s/bus", (unsigned long) getuid(), bus);
fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC);

View file

@ -227,8 +227,8 @@ static int bus_socket_auth_verify_client(sd_bus *b) {
if (f)
b->can_fds =
(f - e == sizeof("\r\nAGREE_UNIX_FD") - 1) &&
memcmp(e + 2, "AGREE_UNIX_FD", sizeof("AGREE_UNIX_FD") - 1) == 0;
(f - e == strlen("\r\nAGREE_UNIX_FD")) &&
memcmp(e + 2, "AGREE_UNIX_FD", strlen("AGREE_UNIX_FD")) == 0;
b->rbuffer_size -= (start - (char*) b->rbuffer);
memmove(b->rbuffer, start, b->rbuffer_size);

View file

@ -51,7 +51,7 @@ struct sd_bus_track {
({ \
char *_x; \
size_t _l = strlen(name); \
_x = alloca(sizeof(MATCH_PREFIX)-1+_l+sizeof(MATCH_SUFFIX)); \
_x = alloca(strlen(MATCH_PREFIX)+_l+strlen(MATCH_SUFFIX)+1); \
strcpy(stpcpy(stpcpy(_x, MATCH_PREFIX), name), MATCH_SUFFIX); \
_x; \
})

View file

@ -509,7 +509,7 @@ static int check_hierarchy(const char *p) {
assert(p);
/* Check if this controller actually really exists */
cc = alloca(sizeof("/sys/fs/cgroup/") + strlen(p));
cc = alloca(strlen("/sys/fs/cgroup/") + strlen(p) + 1);
strcpy(stpcpy(cc, "/sys/fs/cgroup/"), p);
if (access(cc, F_OK) < 0)
return -errno;

View file

@ -493,7 +493,7 @@ char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) {
/* We'll only escape the obvious characters here, to play
* safe. */
r = new(char, strlen(name) * 4 + 1 + sizeof(".service")-1);
r = new(char, strlen(name) * 4 + strlen(".service") + 1);
if (!r)
return NULL;

View file

@ -4185,7 +4185,7 @@ int socket_from_display(const char *display, char **path) {
k = strspn(display+1, "0123456789");
f = new(char, sizeof("/tmp/.X11-unix/X") + k);
f = new(char, strlen("/tmp/.X11-unix/X") + k + 1);
if (!f)
return -ENOMEM;

View file

@ -65,7 +65,7 @@ static int apply_sysctl(const char *property, const char *value) {
log_debug("Setting '%s' to '%s'", property, value);
p = new(char, sizeof("/proc/sys/") + strlen(property));
p = new(char, strlen("/proc/sys/") + strlen(property) + 1);
if (!p)
return log_oom();

View file

@ -333,11 +333,11 @@ static void output_units_list(const UnitInfo *unit_infos, unsigned c) {
unsigned n_shown = 0;
int job_count = 0;
max_id_len = sizeof("UNIT")-1;
load_len = sizeof("LOAD")-1;
active_len = sizeof("ACTIVE")-1;
sub_len = sizeof("SUB")-1;
job_len = sizeof("JOB")-1;
max_id_len = strlen("UNIT");
load_len = strlen("LOAD");
active_len = strlen("ACTIVE");
sub_len = strlen("SUB");
job_len = strlen("JOB");
desc_len = 0;
for (u = unit_infos; u < unit_infos + c; u++) {
@ -639,10 +639,10 @@ static int socket_info_compare(const struct socket_info *a, const struct socket_
static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
struct socket_info *s;
unsigned pathlen = sizeof("LISTEN") - 1,
typelen = (sizeof("TYPE") - 1) * arg_show_types,
socklen = sizeof("UNIT") - 1,
servlen = sizeof("ACTIVATES") - 1;
unsigned pathlen = strlen("LISTEN"),
typelen = strlen("TYPE") * arg_show_types,
socklen = strlen("UNIT"),
servlen = strlen("ACTIVATES");
const char *on, *off;
for (s = socket_infos; s < socket_infos + cs; s++) {
@ -836,10 +836,10 @@ static int timer_info_compare(const struct timer_info *a, const struct timer_inf
static int output_timers_list(struct timer_info *timer_infos, unsigned n) {
struct timer_info *t;
unsigned
nextlen = sizeof("NEXT") - 1,
leftlen = sizeof("LEFT") - 1,
unitlen = sizeof("UNIT") - 1,
activatelen = sizeof("ACTIVATES") - 1;
nextlen = strlen("NEXT"),
leftlen = strlen("LEFT"),
unitlen = strlen("UNIT"),
activatelen = strlen("ACTIVATES");
const char *on, *off;
@ -1034,8 +1034,8 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
unsigned max_id_len, id_cols, state_cols;
const UnitFileList *u;
max_id_len = sizeof("UNIT FILE")-1;
state_cols = sizeof("STATE")-1;
max_id_len = strlen("UNIT FILE");
state_cols = strlen("STATE");
for (u = units; u < units + c; u++) {
max_id_len = MAX(max_id_len, strlen(basename(u->path)));
@ -1792,10 +1792,10 @@ static void output_jobs_list(const struct job_info* jobs, unsigned n, bool skipp
pager_open_if_enabled();
id_len = sizeof("JOB")-1;
unit_len = sizeof("UNIT")-1;
type_len = sizeof("TYPE")-1;
state_len = sizeof("STATE")-1;
id_len = strlen("JOB");
unit_len = strlen("UNIT");
type_len = strlen("TYPE");
state_len = strlen("STATE");
for (j = jobs; j < jobs + n; j++) {
uint32_t id = j->id;
@ -6332,7 +6332,7 @@ static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const ch
struct msghdr msghdr = {
.msg_name = &sockaddr,
.msg_namelen = offsetof(struct sockaddr_un, sun_path)
+ sizeof("/run/systemd/shutdownd") - 1,
+ strlen("/run/systemd/shutdownd"),
.msg_iov = iovec,
.msg_iovlen = 1,
};

View file

@ -1142,7 +1142,7 @@ static int add_rule(struct udev_rules *rules, char *line,
}
if (startswith(key, "ATTR{")) {
attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
if (attr == NULL) {
log_error("error parsing ATTR attribute");
goto invalid;
@ -1156,7 +1156,7 @@ static int add_rule(struct udev_rules *rules, char *line,
}
if (startswith(key, "SECLABEL{")) {
attr = get_key_attribute(rules->udev, key + sizeof("SECLABEL")-1);
attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
if (!attr) {
log_error("error parsing SECLABEL attribute");
goto invalid;
@ -1198,7 +1198,7 @@ static int add_rule(struct udev_rules *rules, char *line,
log_error("invalid ATTRS operation");
goto invalid;
}
attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
if (attr == NULL) {
log_error("error parsing ATTRS attribute");
goto invalid;
@ -1223,7 +1223,7 @@ static int add_rule(struct udev_rules *rules, char *line,
}
if (startswith(key, "ENV{")) {
attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
attr = get_key_attribute(rules->udev, key + strlen("ENV"));
if (attr == NULL) {
log_error("error parsing ENV attribute");
goto invalid;
@ -1282,7 +1282,7 @@ static int add_rule(struct udev_rules *rules, char *line,
}
if (startswith(key, "IMPORT")) {
attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
if (attr == NULL) {
log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
continue;
@ -1328,7 +1328,7 @@ static int add_rule(struct udev_rules *rules, char *line,
log_error("invalid TEST operation");
goto invalid;
}
attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
attr = get_key_attribute(rules->udev, key + strlen("TEST"));
if (attr != NULL) {
mode = strtol(attr, NULL, 8);
rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
@ -1339,7 +1339,7 @@ static int add_rule(struct udev_rules *rules, char *line,
}
if (startswith(key, "RUN")) {
attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
attr = get_key_attribute(rules->udev, key + strlen("RUN"));
if (attr == NULL)
attr = "program";