Skip to content

Commit d054f0a

Browse files
committed
tree-wide: use xsprintf() where applicable
Also add a coccinelle receipt to help with such transitions.
1 parent 1f52a79 commit d054f0a

File tree

21 files changed

+70
-40
lines changed

21 files changed

+70
-40
lines changed

coccinelle/xsprintf.cocci

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@@
2+
expression e, fmt;
3+
expression list vaargs;
4+
@@
5+
- snprintf(e, sizeof(e), fmt, vaargs);
6+
+ xsprintf(e, fmt, vaargs);

src/basic/cgroup-util.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "set.h"
5454
#include "special.h"
5555
#include "stat-util.h"
56+
#include "stdio-util.h"
5657
#include "string-table.h"
5758
#include "string-util.h"
5859
#include "unit-name.h"
@@ -716,7 +717,7 @@ int cg_attach(const char *controller, const char *path, pid_t pid) {
716717
if (pid == 0)
717718
pid = getpid();
718719

719-
snprintf(c, sizeof(c), PID_FMT"\n", pid);
720+
xsprintf(c, PID_FMT "\n", pid);
720721

721722
return write_string_file(fs, c, 0);
722723
}

src/basic/log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ static int write_to_console(
352352
highlight = LOG_PRI(level) <= LOG_ERR && show_color;
353353

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

@@ -777,7 +777,7 @@ static void log_assert(
777777
return;
778778

779779
DISABLE_WARNING_FORMAT_NONLITERAL;
780-
snprintf(buffer, sizeof(buffer), format, text, file, line, func);
780+
xsprintf(buffer, format, text, file, line, func);
781781
REENABLE_WARNING;
782782

783783
log_abort_msg = buffer;

src/basic/signal-util.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "macro.h"
2727
#include "parse-util.h"
2828
#include "signal-util.h"
29+
#include "stdio-util.h"
2930
#include "string-table.h"
3031
#include "string-util.h"
3132

@@ -234,9 +235,9 @@ const char *signal_to_string(int signo) {
234235
return name;
235236

236237
if (signo >= SIGRTMIN && signo <= SIGRTMAX)
237-
snprintf(buf, sizeof(buf), "RTMIN+%d", signo - SIGRTMIN);
238+
xsprintf(buf, "RTMIN+%d", signo - SIGRTMIN);
238239
else
239-
snprintf(buf, sizeof(buf), "%d", signo);
240+
xsprintf(buf, "%d", signo);
240241

241242
return buf;
242243
}

src/bootchart/svg.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "fileio.h"
3838
#include "list.h"
3939
#include "macro.h"
40+
#include "stdio-util.h"
4041
#include "store.h"
4142
#include "svg.h"
4243
#include "utf8.h"
@@ -171,7 +172,7 @@ static int svg_title(FILE *of, const char *build, int pscount, double log_start,
171172

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

176177
r = read_one_line_file(filename, &model);
177178
if (r < 0)

src/cgtop/cgtop.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "parse-util.h"
4141
#include "path-util.h"
4242
#include "process-util.h"
43+
#include "stdio-util.h"
4344
#include "terminal-util.h"
4445
#include "unit-name.h"
4546
#include "util.h"
@@ -565,9 +566,9 @@ static void display(Hashmap *a) {
565566
}
566567

567568
if (arg_cpu_type == CPU_PERCENT)
568-
snprintf(buffer, sizeof(buffer), "%6s", "%CPU");
569+
xsprintf(buffer, "%6s", "%CPU");
569570
else
570-
snprintf(buffer, sizeof(buffer), "%*s", maxtcpu, "CPU Time");
571+
xsprintf(buffer, "%*s", maxtcpu, "CPU Time");
571572

572573
rows = lines();
573574
if (rows <= 10)

src/core/job.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "parse-util.h"
3636
#include "set.h"
3737
#include "special.h"
38+
#include "stdio-util.h"
3839
#include "string-table.h"
3940
#include "string-util.h"
4041
#include "strv.h"
@@ -754,7 +755,7 @@ static void job_log_status_message(Unit *u, JobType t, JobResult result) {
754755
return;
755756

756757
DISABLE_WARNING_FORMAT_NONLITERAL;
757-
snprintf(buf, sizeof(buf), format, unit_description(u));
758+
xsprintf(buf, format, unit_description(u));
758759
REENABLE_WARNING;
759760

760761
switch (t) {

src/core/unit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "set.h"
5252
#include "special.h"
5353
#include "stat-util.h"
54+
#include "stdio-util.h"
5455
#include "string-util.h"
5556
#include "strv.h"
5657
#include "unit-name.h"
@@ -1412,7 +1413,7 @@ static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) {
14121413
format = unit_get_status_message_format(u, t);
14131414

14141415
DISABLE_WARNING_FORMAT_NONLITERAL;
1415-
snprintf(buf, sizeof(buf), format, unit_description(u));
1416+
xsprintf(buf, format, unit_description(u));
14161417
REENABLE_WARNING;
14171418

14181419
mid = t == JOB_START ? SD_MESSAGE_UNIT_STARTING :

src/journal/journald-syslog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,11 @@ void server_process_syslog_message(
357357

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

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

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

src/libsystemd-network/sd-dhcp-lease.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "in-addr-util.h"
3838
#include "network-internal.h"
3939
#include "parse-util.h"
40+
#include "stdio-util.h"
4041
#include "string-util.h"
4142
#include "unaligned.h"
4243

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

842-
snprintf(key, sizeof(key), "OPTION_%"PRIu8, option->tag);
843+
xsprintf(key, "OPTION_%" PRIu8, option->tag);
843844
r = serialize_dhcp_option(f, key, option->data, option->length);
844845
if (r < 0)
845846
goto fail;

0 commit comments

Comments
 (0)