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.
/* See LICENSE file for copyright and license details. */
/* See LICENSE file for copyright and license details. */
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
struct sigaction act;
struct timespec start, current, diff, intspec, wait;
size_t i, len;
struct sigaction act;
struct timespec start, current, diff, intspec, wait;
size_t i, len;
char status[MAXLEN];
sflag = 0;
char status[MAXLEN];
sflag = 0;
for (i = len = 0; i < LEN(args); i++) {
const char * res = args[i].func(args[i].args);
res = (res == NULL) ? unknown_str : res;
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;
bprintf(const char *fmt, ...)
{
va_list ap;
bprintf(const char *fmt, ...)
{
va_list ap;
- 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");