libbb: prevent resursion on malloc failure
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
6d48d3ee2a
commit
e2944af43e
@ -15,7 +15,7 @@ const char *msg_eol = "\n";
|
|||||||
|
|
||||||
void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
|
void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
|
||||||
{
|
{
|
||||||
char *msg;
|
char *msg, *msg1;
|
||||||
int applet_len, strerr_len, msgeol_len, used;
|
int applet_len, strerr_len, msgeol_len, used;
|
||||||
|
|
||||||
if (!logmode)
|
if (!logmode)
|
||||||
@ -36,27 +36,36 @@ void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
|
|||||||
applet_len = strlen(applet_name) + 2; /* "applet: " */
|
applet_len = strlen(applet_name) + 2; /* "applet: " */
|
||||||
strerr_len = strerr ? strlen(strerr) : 0;
|
strerr_len = strerr ? strlen(strerr) : 0;
|
||||||
msgeol_len = strlen(msg_eol);
|
msgeol_len = strlen(msg_eol);
|
||||||
|
/* can't use xrealloc: it calls error_msg on failure,
|
||||||
|
* that may result in a recursion */
|
||||||
/* +3 is for ": " before strerr and for terminating NUL */
|
/* +3 is for ": " before strerr and for terminating NUL */
|
||||||
msg = xrealloc(msg, applet_len + used + strerr_len + msgeol_len + 3);
|
msg1 = realloc(msg, applet_len + used + strerr_len + msgeol_len + 3);
|
||||||
/* TODO: maybe use writev instead of memmoving? Need full_writev? */
|
if (!msg1) {
|
||||||
memmove(msg + applet_len, msg, used);
|
msg[used++] = '\n'; /* overwrites NUL */
|
||||||
used += applet_len;
|
applet_len = 0;
|
||||||
strcpy(msg, applet_name);
|
} else {
|
||||||
msg[applet_len - 2] = ':';
|
msg = msg1;
|
||||||
msg[applet_len - 1] = ' ';
|
/* TODO: maybe use writev instead of memmoving? Need full_writev? */
|
||||||
if (strerr) {
|
memmove(msg + applet_len, msg, used);
|
||||||
if (s[0]) { /* not perror_nomsg? */
|
used += applet_len;
|
||||||
msg[used++] = ':';
|
strcpy(msg, applet_name);
|
||||||
msg[used++] = ' ';
|
msg[applet_len - 2] = ':';
|
||||||
|
msg[applet_len - 1] = ' ';
|
||||||
|
if (strerr) {
|
||||||
|
if (s[0]) { /* not perror_nomsg? */
|
||||||
|
msg[used++] = ':';
|
||||||
|
msg[used++] = ' ';
|
||||||
|
}
|
||||||
|
strcpy(&msg[used], strerr);
|
||||||
|
used += strerr_len;
|
||||||
}
|
}
|
||||||
strcpy(&msg[used], strerr);
|
strcpy(&msg[used], msg_eol);
|
||||||
used += strerr_len;
|
used += msgeol_len;
|
||||||
}
|
}
|
||||||
strcpy(&msg[used], msg_eol);
|
|
||||||
|
|
||||||
if (logmode & LOGMODE_STDIO) {
|
if (logmode & LOGMODE_STDIO) {
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
full_write(STDERR_FILENO, msg, used + msgeol_len);
|
full_write(STDERR_FILENO, msg, used);
|
||||||
}
|
}
|
||||||
if (logmode & LOGMODE_SYSLOG) {
|
if (logmode & LOGMODE_SYSLOG) {
|
||||||
syslog(LOG_ERR, "%s", msg + applet_len);
|
syslog(LOG_ERR, "%s", msg + applet_len);
|
||||||
|
Loading…
Reference in New Issue
Block a user