Skip to content

Commit

Permalink
tree-wide: use xsprintf() where applicable
Browse files Browse the repository at this point in the history
Also add a coccinelle receipt to help with such transitions.
  • Loading branch information
zonque committed Jan 12, 2016
1 parent 1f52a79 commit d054f0a
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 40 deletions.
6 changes: 6 additions & 0 deletions coccinelle/xsprintf.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@@
expression e, fmt;
expression list vaargs;
@@
- snprintf(e, sizeof(e), fmt, vaargs);
+ xsprintf(e, fmt, vaargs);
3 changes: 2 additions & 1 deletion src/basic/cgroup-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "set.h"
#include "special.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
#include "unit-name.h"
Expand Down Expand Up @@ -716,7 +717,7 @@ int cg_attach(const char *controller, const char *path, pid_t pid) {
if (pid == 0)
pid = getpid();

snprintf(c, sizeof(c), PID_FMT"\n", pid);
xsprintf(c, PID_FMT "\n", pid);

return write_string_file(fs, c, 0);
}
Expand Down
4 changes: 2 additions & 2 deletions src/basic/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ static int write_to_console(
highlight = LOG_PRI(level) <= LOG_ERR && show_color;

if (show_location) {
snprintf(location, sizeof(location), "(%s:%i) ", file, line);
xsprintf(location, "(%s:%i) ", file, line);
IOVEC_SET_STRING(iovec[n++], location);
}

Expand Down Expand Up @@ -777,7 +777,7 @@ static void log_assert(
return;

DISABLE_WARNING_FORMAT_NONLITERAL;
snprintf(buffer, sizeof(buffer), format, text, file, line, func);
xsprintf(buffer, format, text, file, line, func);
REENABLE_WARNING;

log_abort_msg = buffer;
Expand Down
5 changes: 3 additions & 2 deletions src/basic/signal-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "macro.h"
#include "parse-util.h"
#include "signal-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"

Expand Down Expand Up @@ -234,9 +235,9 @@ const char *signal_to_string(int signo) {
return name;

if (signo >= SIGRTMIN && signo <= SIGRTMAX)
snprintf(buf, sizeof(buf), "RTMIN+%d", signo - SIGRTMIN);
xsprintf(buf, "RTMIN+%d", signo - SIGRTMIN);
else
snprintf(buf, sizeof(buf), "%d", signo);
xsprintf(buf, "%d", signo);

return buf;
}
Expand Down
3 changes: 2 additions & 1 deletion src/bootchart/svg.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "fileio.h"
#include "list.h"
#include "macro.h"
#include "stdio-util.h"
#include "store.h"
#include "svg.h"
#include "utf8.h"
Expand Down Expand Up @@ -171,7 +172,7 @@ static int svg_title(FILE *of, const char *build, int pscount, double log_start,

strncpy(rootbdev, &c[10], sizeof(rootbdev) - 1);
rootbdev[3] = '\0';
snprintf(filename, sizeof(filename), "/sys/block/%s/device/model", rootbdev);
xsprintf(filename, "/sys/block/%s/device/model", rootbdev);

r = read_one_line_file(filename, &model);
if (r < 0)
Expand Down
5 changes: 3 additions & 2 deletions src/cgtop/cgtop.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "stdio-util.h"
#include "terminal-util.h"
#include "unit-name.h"
#include "util.h"
Expand Down Expand Up @@ -565,9 +566,9 @@ static void display(Hashmap *a) {
}

if (arg_cpu_type == CPU_PERCENT)
snprintf(buffer, sizeof(buffer), "%6s", "%CPU");
xsprintf(buffer, "%6s", "%CPU");
else
snprintf(buffer, sizeof(buffer), "%*s", maxtcpu, "CPU Time");
xsprintf(buffer, "%*s", maxtcpu, "CPU Time");

rows = lines();
if (rows <= 10)
Expand Down
3 changes: 2 additions & 1 deletion src/core/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "parse-util.h"
#include "set.h"
#include "special.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
Expand Down Expand Up @@ -754,7 +755,7 @@ static void job_log_status_message(Unit *u, JobType t, JobResult result) {
return;

DISABLE_WARNING_FORMAT_NONLITERAL;
snprintf(buf, sizeof(buf), format, unit_description(u));
xsprintf(buf, format, unit_description(u));
REENABLE_WARNING;

switch (t) {
Expand Down
3 changes: 2 additions & 1 deletion src/core/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "set.h"
#include "special.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "unit-name.h"
Expand Down Expand Up @@ -1412,7 +1413,7 @@ static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) {
format = unit_get_status_message_format(u, t);

DISABLE_WARNING_FORMAT_NONLITERAL;
snprintf(buf, sizeof(buf), format, unit_description(u));
xsprintf(buf, format, unit_description(u));
REENABLE_WARNING;

mid = t == JOB_START ? SD_MESSAGE_UNIT_STARTING :
Expand Down
4 changes: 2 additions & 2 deletions src/journal/journald-syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,11 @@ void server_process_syslog_message(

IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");

snprintf(syslog_priority, sizeof(syslog_priority), "PRIORITY=%i", priority & LOG_PRIMASK);
xsprintf(syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK);
IOVEC_SET_STRING(iovec[n++], syslog_priority);

if (priority & LOG_FACMASK) {
snprintf(syslog_facility, sizeof(syslog_facility), "SYSLOG_FACILITY=%i", LOG_FAC(priority));
xsprintf(syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority));
IOVEC_SET_STRING(iovec[n++], syslog_facility);
}

Expand Down
3 changes: 2 additions & 1 deletion src/libsystemd-network/sd-dhcp-lease.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "in-addr-util.h"
#include "network-internal.h"
#include "parse-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "unaligned.h"

Expand Down Expand Up @@ -839,7 +840,7 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
LIST_FOREACH(options, option, lease->private_options) {
char key[strlen("OPTION_000")+1];

snprintf(key, sizeof(key), "OPTION_%"PRIu8, option->tag);
xsprintf(key, "OPTION_%" PRIu8, option->tag);
r = serialize_dhcp_option(f, key, option->data, option->length);
if (r < 0)
goto fail;
Expand Down
7 changes: 5 additions & 2 deletions src/libsystemd/sd-bus/bus-kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "formats-util.h"
#include "memfd-util.h"
#include "parse-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "user-util.h"
Expand Down Expand Up @@ -849,7 +850,8 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
if (k->src_id == KDBUS_SRC_ID_KERNEL)
bus_message_set_sender_driver(bus, m);
else {
snprintf(m->sender_buffer, sizeof(m->sender_buffer), ":1.%llu", (unsigned long long) k->src_id);
xsprintf(m->sender_buffer, ":1.%llu",
(unsigned long long)k->src_id);
m->sender = m->creds.unique_name = m->sender_buffer;
}

Expand All @@ -860,7 +862,8 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
else if (k->dst_id == KDBUS_DST_ID_NAME)
m->destination = bus->unique_name; /* fill in unique name if the well-known name is missing */
else {
snprintf(m->destination_buffer, sizeof(m->destination_buffer), ":1.%llu", (unsigned long long) k->dst_id);
xsprintf(m->destination_buffer, ":1.%llu",
(unsigned long long)k->dst_id);
m->destination = m->destination_buffer;
}

Expand Down
3 changes: 2 additions & 1 deletion src/login/logind-seat.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "logind-seat.h"
#include "mkdir.h"
#include "parse-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "util.h"
Expand Down Expand Up @@ -181,7 +182,7 @@ static int vt_allocate(unsigned int vtnr) {

assert(vtnr >= 1);

snprintf(p, sizeof(p), "/dev/tty%u", vtnr);
xsprintf(p, "/dev/tty%u", vtnr);
fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return -errno;
Expand Down
4 changes: 3 additions & 1 deletion src/network/networkctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "pager.h"
#include "parse-util.h"
#include "socket-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
Expand Down Expand Up @@ -275,7 +276,8 @@ static int ieee_oui(sd_hwdb *hwdb, struct ether_addr *mac, char **ret) {
if (memcmp(mac, "\0\0\0", 3) == 0)
return -EINVAL;

snprintf(modalias, sizeof(modalias), "OUI:" ETHER_ADDR_FORMAT_STR, ETHER_ADDR_FORMAT_VAL(*mac));
xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR,
ETHER_ADDR_FORMAT_VAL(*mac));

r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description);
if (r < 0)
Expand Down
3 changes: 2 additions & 1 deletion src/shared/switch-root.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "mkdir.h"
#include "path-util.h"
#include "rm-rf.h"
#include "stdio-util.h"
#include "string-util.h"
#include "switch-root.h"
#include "user-util.h"
Expand Down Expand Up @@ -77,7 +78,7 @@ int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot,
char new_mount[PATH_MAX];
struct stat sb;

snprintf(new_mount, sizeof(new_mount), "%s%s", new_root, i);
xsprintf(new_mount, "%s%s", new_root, i);

mkdir_p_label(new_mount, 0755);

Expand Down
3 changes: 2 additions & 1 deletion src/test/test-libudev.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "libudev.h"

#include "stdio-util.h"
#include "string-util.h"
#include "udev-util.h"
#include "util.h"
Expand Down Expand Up @@ -460,7 +461,7 @@ int main(int argc, char *argv[]) {

/* add sys path if needed */
if (!startswith(syspath, "/sys")) {
snprintf(path, sizeof(path), "/sys/%s", syspath);
xsprintf(path, "/sys/%s", syspath);
syspath = path;
}

Expand Down
3 changes: 2 additions & 1 deletion src/udev/collect/collect.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "alloc-util.h"
#include "libudev-private.h"
#include "macro.h"
#include "stdio-util.h"
#include "string-util.h"

#define BUFSIZE 16
Expand Down Expand Up @@ -91,7 +92,7 @@ static int prepare(char *dir, char *filename)
if (r < 0 && errno != EEXIST)
return -errno;

snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
xsprintf(buf, "%s/%s", dir, filename);

fd = open(buf,O_RDWR|O_CREAT|O_CLOEXEC, S_IRUSR|S_IWUSR);
if (fd < 0)
Expand Down
10 changes: 6 additions & 4 deletions src/udev/udev-builtin-input_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <linux/input.h>

#include "fd-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "udev.h"
#include "util.h"
Expand Down Expand Up @@ -66,8 +67,8 @@ static void extract_info(struct udev_device *dev, const char *devpath, bool test
if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0)
return;

snprintf(width, sizeof(width), "%d", abs_size_mm(&xabsinfo));
snprintf(height, sizeof(height), "%d", abs_size_mm(&yabsinfo));
xsprintf(width, "%d", abs_size_mm(&xabsinfo));
xsprintf(height, "%d", abs_size_mm(&yabsinfo));

udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width);
udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height);
Expand All @@ -93,7 +94,7 @@ static void get_cap_mask(struct udev_device *dev,
if (!v)
v = "";

snprintf(text, sizeof(text), "%s", v);
xsprintf(text, "%s", v);
log_debug("%s raw kernel attribute: %s", attr, text);

memzero(bitmask, bitmask_size);
Expand All @@ -115,7 +116,8 @@ static void get_cap_mask(struct udev_device *dev,

if (test) {
/* printf pattern with the right unsigned long number of hex chars */
snprintf(text, sizeof(text), " bit %%4u: %%0%zulX\n", 2 * sizeof(unsigned long));
xsprintf(text, " bit %%4u: %%0%zulX\n",
2 * sizeof(unsigned long));
log_debug("%s decoded bit map:", attr);
val = bitmask_size / sizeof (unsigned long);
/* skip over leading zeros */
Expand Down
15 changes: 8 additions & 7 deletions src/udev/udev-builtin-net_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@

#include "fd-util.h"
#include "fileio.h"
#include "stdio-util.h"
#include "string-util.h"
#include "udev.h"

Expand Down Expand Up @@ -228,7 +229,7 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
err = -ENOENT;
goto out;
}
snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci));
xsprintf(slots, "%s/slots", udev_device_get_syspath(pci));
dir = opendir(slots);
if (!dir) {
err = -errno;
Expand All @@ -247,7 +248,7 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
continue;
if (i < 1)
continue;
snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
xsprintf(str, "%s/%s/address", slots, dent->d_name);
if (read_one_line_file(str, &address) >= 0) {
/* match slot address with device by stripping the function */
if (strneq(address, udev_device_get_sysname(names->pcidev), strlen(address)))
Expand Down Expand Up @@ -380,7 +381,7 @@ static int names_bcma(struct udev_device *dev, struct netnames *names) {
return -EINVAL;
/* suppress the common core == 0 */
if (core > 0)
snprintf(names->bcma_core, sizeof(names->bcma_core), "b%u", core);
xsprintf(names->bcma_core, "b%u", core);

names->type = NET_BCMA;
return 0;
Expand Down Expand Up @@ -469,9 +470,9 @@ static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test)
/* skip commonly misused 00:00:00 (Xerox) prefix */
if (memcmp(names->mac, "\0\0\0", 3) == 0)
return -EINVAL;
snprintf(str, sizeof(str), "OUI:%02X%02X%02X%02X%02X%02X",
names->mac[0], names->mac[1], names->mac[2],
names->mac[3], names->mac[4], names->mac[5]);
xsprintf(str, "OUI:%02X%02X%02X%02X%02X%02X", names->mac[0],
names->mac[1], names->mac[2], names->mac[3], names->mac[4],
names->mac[5]);
udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test);
return 0;
}
Expand Down Expand Up @@ -523,7 +524,7 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
if (err >= 0 && names.mac_valid) {
char str[IFNAMSIZ];

snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix,
xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix,
names.mac[0], names.mac[1], names.mac[2],
names.mac[3], names.mac[4], names.mac[5]);
udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
Expand Down
Loading

0 comments on commit d054f0a

Please sign in to comment.