Fix segfault in xbus logging handler

xbus logging handler assumed QMessageLogContext always had a valid file
pointer which is not necessarily the case. If the file pointer is 0x0
log a file name <unknown> instead.
This commit is contained in:
Roland Winklmeier
2015-03-07 18:32:42 +01:00
committed by Klaus Basan
parent ac760ce9b0
commit 8a0c88f71b

View File

@@ -32,23 +32,28 @@ class QXPlaneMessageHandler
static void handler(QtMsgType type, const QMessageLogContext &context, const QString &msg) static void handler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{ {
QByteArray localMsg = msg.toLocal8Bit(); QByteArray localMsg = msg.toLocal8Bit();
char *buffer = new char[64 + localMsg.size() + std::strlen(context.file)]; QByteArray file(context.file);
if (file.isEmpty()) file = "<unknown>";
int line = context.line;
char *buffer = new char[64 + localMsg.size() + file.size()];
switch (type) { switch (type) {
case QtDebugMsg: case QtDebugMsg:
std::sprintf(buffer, "%s:%u: Debug: %s\n", context.file, context.line, localMsg.constData()); std::sprintf(buffer, "%s:%u: Debug: %s\n", file.constData(), line, localMsg.constData());
XPLMDebugString(buffer); XPLMDebugString(buffer);
break; break;
case QtWarningMsg: case QtWarningMsg:
std::sprintf(buffer, "%s:%u: Warning: %s\n", context.file, context.line, localMsg.constData()); std::sprintf(buffer, "%s:%u: Warning: %s\n", file.constData(), line, localMsg.constData());
XPLMDebugString(buffer); XPLMDebugString(buffer);
break; break;
default: default:
case QtCriticalMsg: case QtCriticalMsg:
std::sprintf(buffer, "%s:%u: Error: %s\n", context.file, context.line, localMsg.constData()); std::sprintf(buffer, "%s:%u: Error: %s\n", file.constData(), line, localMsg.constData());
XPLMDebugString(buffer); XPLMDebugString(buffer);
break; break;
case QtFatalMsg: case QtFatalMsg:
std::sprintf(buffer, "%s:%u: Fatal: %s\n", context.file, context.line, localMsg.constData()); std::sprintf(buffer, "%s:%u: Fatal: %s\n", file.constData(), line, localMsg.constData());
XPLMDebugString(buffer); XPLMDebugString(buffer);
std::abort(); std::abort();
} }