diff options
author | Laslo Hunhold <dev@frign.de> | 2018-05-17 23:23:28 +0200 |
---|---|---|
committer | Aaron Marcher <me@drkhsh.at> | 2018-05-17 23:26:56 +0200 |
commit | c83b388a3f8f7a8c8d5a5cfddb6ab397005371a1 (patch) | |
tree | 7ec72cf6a75f0bdc858a2e67f1fc214a8f916de5 /slstatus.c | |
parent | b759662983a159da8a3c361f4f222287b2e43331 (diff) |
Properly handle *snprintf() errors
Posix guarantees that the resulting string is null-terminated, even if
we have an overflow. Instead of doing what has already been done,
properly warn when there has been an error or overflow, so the user can
do something about it.
Diffstat (limited to 'slstatus.c')
-rw-r--r-- | slstatus.c | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include <errno.h> #include <locale.h> #include <signal.h> #include <stdio.h> @@ -53,7 +54,7 @@ main(int argc, char *argv[]) struct sigaction act; struct timespec start, current, diff, intspec, wait; size_t i, len; - int sflag; + int sflag, ret; char status[MAXLEN]; sflag = 0; @@ -88,12 +89,16 @@ main(int argc, char *argv[]) for (i = len = 0; i < LEN(args); i++) { const char * res = args[i].func(args[i].args); res = (res == NULL) ? unknown_str : res; - len += snprintf(status + len, sizeof(status) - len, - args[i].fmt, res); - - if (len >= sizeof(status)) { - status[sizeof(status) - 1] = '\0'; + if ((ret = snprintf(status + len, sizeof(status) - len, + args[i].fmt, res)) < 0) { + fprintf(stderr, "snprintf: %s\n", + strerror(errno)); + break; + } else if ((size_t)ret >= sizeof(status) - len) { + fprintf(stderr, "snprintf: Output truncated\n"); + break; } + len += ret; } if (sflag) { |