Skip to content

Commit 46bdde9

Browse files
authored
Merge pull request #763
fix crash in 2.28.0 due to buffer overflow
2 parents 3493272 + c928fad commit 46bdde9

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

qdlt/qdltctrlmsg.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "dlt_common.h"
44

5+
#include <exception>
56
#include <stdexcept>
67
#include <cstring>
78
#include <array>
@@ -24,7 +25,7 @@ QString asQString(IdType&& id) {
2425
template <typename T>
2526
T dltPayloadRead(const char *&dataPtr, int32_t &length, bool isBigEndian)
2627
{
27-
if (sizeof(T) > static_cast<uint32_t>(length)) {
28+
if (length < 0 || length < static_cast<int32_t>(sizeof(T))) {
2829
throw std::runtime_error("Invalid data length");
2930
}
3031

@@ -46,7 +47,7 @@ T dltPayloadRead(const char *&dataPtr, int32_t &length, bool isBigEndian)
4647

4748
IdType dltPayloadReadId(const char *&dataPtr, int32_t &length)
4849
{
49-
if (DLT_ID_SIZE > length) {
50+
if (length < 0 || length < DLT_ID_SIZE) {
5051
throw std::runtime_error("Invalid ID length");
5152
}
5253
IdType id{};
@@ -60,7 +61,7 @@ IdType dltPayloadReadId(const char *&dataPtr, int32_t &length)
6061
std::string dltPayloadReadString(const char *&dataPtr, int32_t &length, bool isBigEndian)
6162
{
6263
uint16_t strLength = dltPayloadRead<uint16_t>(dataPtr, length, isBigEndian);
63-
if (strLength > length) {
64+
if (length < 0 || strLength > length) {
6465
throw std::runtime_error(QString("Invalid string length %1 > %2").arg(strLength).arg(length).toStdString());
6566
}
6667
std::string str;
@@ -76,9 +77,11 @@ Type parse(const QByteArray& data, bool isBigEndian)
7677
{
7778
int32_t length = data.length();
7879
const char *dataPtr = data.data();
80+
uint32_t serviceId = 0;
7981

80-
auto serviceId = dltPayloadRead<uint32_t>(dataPtr, length, isBigEndian);
81-
switch (serviceId) {
82+
try {
83+
serviceId = dltPayloadRead<uint32_t>(dataPtr, length, isBigEndian);
84+
switch (serviceId) {
8285
case DLT_SERVICE_ID_GET_LOG_INFO:
8386
{
8487
GetLogInfo msg;
@@ -140,9 +143,14 @@ Type parse(const QByteArray& data, bool isBigEndian)
140143
msg.ctxid = asQString(dltPayloadReadId(dataPtr, length));
141144
return msg;
142145
}
143-
}
146+
}
144147

145-
return Uninteresting{serviceId};
148+
return Uninteresting{serviceId, false};
149+
} catch (const std::exception&) {
150+
return Uninteresting{serviceId, true};
151+
} catch (...) {
152+
return Uninteresting{serviceId, true};
153+
}
146154
}
147155

148156
}

qdlt/qdltctrlmsg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct UnregisterContext {
5757

5858
struct Uninteresting {
5959
uint32_t serviceId;
60+
bool parseError = false;
6061
};
6162

6263
using Type = std::variant<GetLogInfo, GetSoftwareVersion, GetDefaultLogLevel, SetLogLevel, Timezone,

0 commit comments

Comments
 (0)