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 /util.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 'util.c')
-rw-r--r-- | util.c | 12 |
1 files changed, 6 insertions, 6 deletions
@@ -10,15 +10,15 @@ const char * bprintf(const char *fmt, ...) { va_list ap; - size_t len; + int ret; va_start(ap, fmt); - len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap); - va_end(ap); - - if (len >= sizeof(buf)) { - buf[sizeof(buf)-1] = '\0'; + if ((ret = vsnprintf(buf, sizeof(buf), fmt, ap)) < 0) { + fprintf(stderr, "vsnprintf: %s\n", strerror(errno)); + } else if ((size_t)ret >= sizeof(buf)) { + fprintf(stderr, "vsnprintf: Output truncated\n"); } + va_end(ap); return buf; } |