summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2018-05-17 23:23:28 +0200
committerAaron Marcher <me@drkhsh.at>2018-05-17 23:26:56 +0200
commitc83b388a3f8f7a8c8d5a5cfddb6ab397005371a1 (patch)
tree7ec72cf6a75f0bdc858a2e67f1fc214a8f916de5 /util.c
parentb759662983a159da8a3c361f4f222287b2e43331 (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.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/util.c b/util.c
index 8808aba..6113049 100644
--- a/util.c
+++ b/util.c
@@ -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;
}