]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-event: replace snprintf() with strpcpyf()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 3 Jul 2019 14:27:27 +0000 (23:27 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 4 Jul 2019 17:43:56 +0000 (02:43 +0900)
src/libsystemd/sd-event/sd-event.c
src/test/test-strxcpyx.c

index 09285c19d86b6d29047128a2be01e2ee3c0bc13b..5adbceeb0247c7a7dcdf8e9ab22df27fe0927bed 100644 (file)
@@ -23,6 +23,7 @@
 #include "signal-util.h"
 #include "string-table.h"
 #include "string-util.h"
+#include "strxcpyx.h"
 #include "time-util.h"
 
 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
@@ -3248,15 +3249,16 @@ _public_ int sd_event_dispatch(sd_event *e) {
 }
 
 static void event_log_delays(sd_event *e) {
-        char b[ELEMENTSOF(e->delays) * DECIMAL_STR_MAX(unsigned) + 1];
-        unsigned i;
-        int o;
+        char b[ELEMENTSOF(e->delays) * DECIMAL_STR_MAX(unsigned) + 1], *p;
+        size_t l, i;
 
-        for (i = o = 0; i < ELEMENTSOF(e->delays); i++) {
-                o += snprintf(&b[o], sizeof(b) - o, "%u ", e->delays[i]);
+        p = b;
+        l = sizeof(b);
+        for (i = 0; i < ELEMENTSOF(e->delays); i++) {
+                l = strpcpyf(&p, l, "%u ", e->delays[i]);
                 e->delays[i] = 0;
         }
-        log_debug("Event loop iterations: %.*s", o, b);
+        log_debug("Event loop iterations: %s", b);
 }
 
 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
index 21d56d9be68aae0704565b6ba6d8ca73b170f739..d7199124a3d9ed246ce173a2d392fc2ea5edf4a7 100644 (file)
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
+#include <stdio.h>
 #include <string.h>
 
 #include "string-util.h"
@@ -78,6 +79,24 @@ static void test_strscpyl(void) {
         assert_se(space_left == 10);
 }
 
+static void test_sd_event_code_migration(void) {
+        char b[100 * DECIMAL_STR_MAX(unsigned) + 1];
+        char c[100 * DECIMAL_STR_MAX(unsigned) + 1], *p;
+        unsigned i;
+        size_t l;
+        int o;
+
+        for (i = o = 0; i < 100; i++)
+                o += snprintf(&b[o], sizeof(b) - o, "%u ", i);
+
+        p = c;
+        l = sizeof(c);
+        for (i = 0; i < 100; i++)
+                l = strpcpyf(&p, l, "%u ", i);
+
+        assert_se(streq(b, c));
+}
+
 int main(int argc, char *argv[]) {
         test_strpcpy();
         test_strpcpyf();
@@ -85,5 +104,7 @@ int main(int argc, char *argv[]) {
         test_strscpy();
         test_strscpyl();
 
+        test_sd_event_code_migration();
+
         return 0;
 }