util: virerror: Don't use stack'd buffers in error report helpers

This was (probably) a relict from times when we cared about OOM
conditions and the possibility to report the error. Nowadays it doesn't
make sense as virRaiseErrorFull will do an allocated copy of the strings
and also concatenate the error message prefix with the detail which
doesn't guarantee that the result will be less than 1024 chars.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-02-23 15:38:02 +01:00
parent c800ed6399
commit 3487554736

View File

@ -28,6 +28,7 @@
#include "virlog.h" #include "virlog.h"
#include "virthread.h" #include "virthread.h"
#include "virstring.h" #include "virstring.h"
#include "virbuffer.h"
#define LIBVIRT_VIRERRORPRIV_H_ALLOW #define LIBVIRT_VIRERRORPRIV_H_ALLOW
#include "virerrorpriv.h" #include "virerrorpriv.h"
@ -1285,23 +1286,23 @@ void virReportErrorHelper(int domcode,
const char *fmt, ...) const char *fmt, ...)
{ {
int save_errno = errno; int save_errno = errno;
va_list args; g_autofree char *detail = NULL;
char errorMessage[VIR_ERROR_MAX_LENGTH]; const char *errormsg;
const char *virerr;
if (fmt) { if (fmt) {
va_list args;
va_start(args, fmt); va_start(args, fmt);
g_vsnprintf(errorMessage, sizeof(errorMessage)-1, fmt, args); detail = g_strdup_vprintf(fmt, args);
va_end(args); va_end(args);
} else {
errorMessage[0] = '\0';
} }
virerr = virErrorMsg(errorcode, (errorMessage[0] ? errorMessage : NULL)); errormsg = virErrorMsg(errorcode, detail);
virRaiseErrorFull(filename, funcname, linenr, virRaiseErrorFull(filename, funcname, linenr,
domcode, errorcode, VIR_ERR_ERROR, domcode, errorcode, VIR_ERR_ERROR,
virerr, errorMessage, NULL, errormsg, detail, NULL,
-1, -1, virerr, errorMessage); -1, -1, errormsg, detail);
errno = save_errno; errno = save_errno;
} }
@ -1325,36 +1326,28 @@ void virReportSystemErrorFull(int domcode,
const char *fmt, ...) const char *fmt, ...)
{ {
int save_errno = errno; int save_errno = errno;
char msgDetailBuf[VIR_ERROR_MAX_LENGTH]; virBuffer buf = VIR_BUFFER_INITIALIZER;
g_autofree char *detail = NULL;
const char *errnoDetail = g_strerror(theerrno); const char *errormsg;
const char *msg = virErrorMsg(VIR_ERR_SYSTEM_ERROR, fmt);
const char *msgDetail = NULL;
if (fmt) { if (fmt) {
va_list args; va_list args;
size_t len;
int n;
va_start(args, fmt); va_start(args, fmt);
n = g_vsnprintf(msgDetailBuf, sizeof(msgDetailBuf), fmt, args); virBufferVasprintf(&buf, fmt, args);
va_end(args); va_end(args);
len = strlen(errnoDetail); virBufferAddLit(&buf, ": ");
if (0 <= n && n + 2 + len < sizeof(msgDetailBuf)) {
strcpy(msgDetailBuf + n, ": ");
n += 2;
strcpy(msgDetailBuf + n, errnoDetail);
msgDetail = msgDetailBuf;
}
} }
if (!msgDetail) virBufferAdd(&buf, g_strerror(theerrno), -1);
msgDetail = errnoDetail;
detail = virBufferContentAndReset(&buf);
errormsg = virErrorMsg(VIR_ERR_SYSTEM_ERROR, detail);
virRaiseErrorFull(filename, funcname, linenr, virRaiseErrorFull(filename, funcname, linenr,
domcode, VIR_ERR_SYSTEM_ERROR, VIR_ERR_ERROR, domcode, VIR_ERR_SYSTEM_ERROR, VIR_ERR_ERROR,
msg, msgDetail, NULL, theerrno, -1, msg, msgDetail); errormsg, detail, NULL, theerrno, -1, errormsg, detail);
errno = save_errno; errno = save_errno;
} }