chiark / gitweb /
Check the return value of snprintf().
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 24 Nov 2018 10:22:43 +0000 (10:22 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 8 Dec 2018 22:30:09 +0000 (22:30 +0000)
I know it can't overflow, but this explains to GCC that I know.

clunk.c

diff --git a/clunk.c b/clunk.c
index 56f6b790363d2e2b9b186461a3028e5c46bfe0c8..b2ce67c07ea0db07576dc6ef4a843a3a33a47bc3 100644 (file)
--- a/clunk.c
+++ b/clunk.c
@@ -1,4 +1,5 @@
 #include <aio.h>
+#include <assert.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -78,6 +79,7 @@ static void (*outfn)(bool) = &dummy_out;
 static void
 record_tick(void)
 {
+       int printed;
 
        displayed.tm_sec += 30;
        while (displayed.tm_sec >= 60) {
@@ -100,8 +102,10 @@ record_tick(void)
                 * POSIX aio.  Happily, it's pleasant enough in a
                 * simple case like this.
                 */
-               snprintf(statebuf, 10, "%2d:%02d:%02d\n", displayed.tm_hour,
-                        displayed.tm_min, displayed.tm_sec);
+               printed = snprintf(statebuf, 10, "%2d:%02d:%02d\n",
+                                  displayed.tm_hour, displayed.tm_min,
+                                  displayed.tm_sec);
+               assert(printed == 9);
                stateaio.aio_fildes = statefd;
                stateaio.aio_buf = statebuf;
                stateaio.aio_nbytes = 9;