mirror of
https://github.com/sudo-project/sudo.git
synced 2025-08-30 13:58:05 +00:00
Use vsnprintf() instead of vfprintf() for sudo_printf() to avoid
problems on systems where the system printf(3) is not C99-compliant. We use our own snprintf() on such systems.
This commit is contained in:
parent
cf9fc5317e
commit
58d6554a78
@ -24,10 +24,12 @@
|
|||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <sudo_compat.h>
|
#include <sudo_compat.h>
|
||||||
|
#include <sudo_util.h>
|
||||||
#include <sudo_plugin.h>
|
#include <sudo_plugin.h>
|
||||||
#include <sudo_debug.h>
|
#include <sudo_debug.h>
|
||||||
#include <pathnames.h>
|
#include <pathnames.h>
|
||||||
@ -37,6 +39,8 @@ sudo_printf_int(int msg_type, const char * restrict fmt, ...)
|
|||||||
{
|
{
|
||||||
FILE *fp = stdout;
|
FILE *fp = stdout;
|
||||||
FILE *ttyfp = NULL;
|
FILE *ttyfp = NULL;
|
||||||
|
char sbuf[8192];
|
||||||
|
char *buf = sbuf;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
@ -50,9 +54,26 @@ sudo_printf_int(int msg_type, const char * restrict fmt, ...)
|
|||||||
fp = stderr;
|
fp = stderr;
|
||||||
FALLTHROUGH;
|
FALLTHROUGH;
|
||||||
case SUDO_CONV_INFO_MSG:
|
case SUDO_CONV_INFO_MSG:
|
||||||
|
/*
|
||||||
|
* We use vsnprintf() instead of vfprintf() here to avoid
|
||||||
|
* problems on systems where the system printf(3) is not
|
||||||
|
* C99-compliant. We use our own snprintf() on such systems.
|
||||||
|
*/
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
len = vfprintf(ttyfp ? ttyfp : fp, fmt, ap);
|
len = vsnprintf(sbuf, sizeof(sbuf), fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
if (len < 0 || len >= ssizeof(sbuf)) {
|
||||||
|
/* Try again with a dynamically-sized buffer. */
|
||||||
|
va_start(ap, fmt);
|
||||||
|
len = vasprintf(&buf, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
if (len != -1) {
|
||||||
|
if (fwrite(buf, 1, len, ttyfp ? ttyfp : fp) == 0)
|
||||||
|
len = -1;
|
||||||
|
if (buf != sbuf)
|
||||||
|
free(buf);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
len = -1;
|
len = -1;
|
||||||
|
@ -163,6 +163,8 @@ sudo_conversation_printf(int msg_type, const char * restrict fmt, ...)
|
|||||||
FILE *ttyfp = NULL;
|
FILE *ttyfp = NULL;
|
||||||
FILE *fp = stdout;
|
FILE *fp = stdout;
|
||||||
char fmt2[1024];
|
char fmt2[1024];
|
||||||
|
char sbuf[8192];
|
||||||
|
char *buf = sbuf;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
int len;
|
int len;
|
||||||
const int conv_debug_instance = sudo_debug_get_active_instance();
|
const int conv_debug_instance = sudo_debug_get_active_instance();
|
||||||
@ -201,9 +203,26 @@ sudo_conversation_printf(int msg_type, const char * restrict fmt, ...)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
va_start(ap, fmt);
|
/*
|
||||||
len = vfprintf(ttyfp ? ttyfp : fp, fmt, ap);
|
* We use vsnprintf() instead of vfprintf() here to avoid
|
||||||
va_end(ap);
|
* problems on systems where the system printf(3) is not
|
||||||
|
* C99-compliant. We use our own snprintf() on such systems.
|
||||||
|
*/
|
||||||
|
va_start(ap, fmt);
|
||||||
|
len = vsnprintf(sbuf, sizeof(sbuf), fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
if (len < 0 || len >= ssizeof(sbuf)) {
|
||||||
|
/* Try again with a dynamically-sized buffer. */
|
||||||
|
va_start(ap, fmt);
|
||||||
|
len = vasprintf(&buf, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
if (len != -1) {
|
||||||
|
if (fwrite(buf, 1, len, ttyfp ? ttyfp : fp) == 0)
|
||||||
|
len = -1;
|
||||||
|
if (buf != sbuf)
|
||||||
|
free(buf);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
len = -1;
|
len = -1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user