Skip to content

Commit 5b437d3

Browse files
authored
Fix wrong offset calculation in SOME/IP-TP (#1898)
* Fix the bug in `SomeIpTpLayer::getOffset()` and `SomeIpTpLayer::setOffset()` * Fix formatting
1 parent 440f19b commit 5b437d3

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

Packet++/header/SomeIpLayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ namespace pcpp
344344
/// Set the Offset. Already has to be in multiples of 16 bytes.
345345
/// If 32 bytes have already been transmitted, the offset has to be set to 2.
346346
/// @param[in] offset Offset to set. Already has to be in multiples of 16 bytes.
347+
/// @throw std::invalid_argument if offset isn't a multiple of 16
347348
void setOffset(uint32_t offset);
348349

349350
/// Get the More Segments Flag

Packet++/src/SomeIpLayer.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,18 @@ namespace pcpp
306306

307307
uint32_t SomeIpTpLayer::getOffset() const
308308
{
309-
return (be32toh(getSomeIpTpHeader()->offsetAndFlag) & SOMEIP_TP_OFFSET_MASK) >> 4;
309+
return (be32toh(getSomeIpTpHeader()->offsetAndFlag) & SOMEIP_TP_OFFSET_MASK);
310310
}
311311

312312
void SomeIpTpLayer::setOffset(uint32_t offset)
313313
{
314-
uint32_t val = (offset << 4) | (be32toh(getSomeIpTpHeader()->offsetAndFlag) & ~SOMEIP_TP_OFFSET_MASK);
314+
if ((offset & SOMEIP_TP_OFFSET_MASK) != offset)
315+
{
316+
throw std::invalid_argument("Invalid offset - should be a multiple of 16");
317+
}
318+
319+
uint32_t val =
320+
(offset & SOMEIP_TP_OFFSET_MASK) | (be32toh(getSomeIpTpHeader()->offsetAndFlag) & ~SOMEIP_TP_OFFSET_MASK);
315321
getSomeIpTpHeader()->offsetAndFlag = htobe32(val);
316322
}
317323

Tests/Packet++Test/Tests/SomeIpTests.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ PTF_TEST_CASE(SomeIpTpParsingTest)
233233
PTF_ASSERT_EQUAL(someIpTpLayer2->getMessageType(), pcpp::SomeIpLayer::MsgType::TP_REQUEST_NO_RETURN, enumclass);
234234
PTF_ASSERT_EQUAL(someIpTpLayer2->getMessageTypeAsInt(), (uint8_t)pcpp::SomeIpLayer::MsgType::TP_REQUEST_NO_RETURN);
235235
PTF_ASSERT_EQUAL(someIpTpLayer2->getReturnCode(), 0);
236-
PTF_ASSERT_EQUAL(someIpTpLayer2->getOffset() * 16, 91872);
236+
PTF_ASSERT_EQUAL(someIpTpLayer2->getOffset(), 91872);
237237
PTF_ASSERT_FALSE(someIpTpLayer2->getMoreSegmentsFlag());
238238
PTF_ASSERT_EQUAL(someIpTpLayer2->getPduPayloadSize(), 225);
239239
PTF_ASSERT_EQUAL(someIpTpLayer2->getPduPayload()[0], 0xab);
@@ -284,8 +284,8 @@ PTF_TEST_CASE(SomeIpTpCreationTest)
284284
pcpp::EthLayer ethLayer2(ethLayer1);
285285
pcpp::IPv4Layer ipLayer2(ipLayer1);
286286
pcpp::UdpLayer udpLayer2(udpLayer1);
287-
pcpp::SomeIpTpLayer someIpTpLayer2(0xd05f, 0x8001, 0, 0, 1, pcpp::SomeIpLayer::MsgType::REQUEST_NO_RETURN, 0,
288-
91872 / 16, false, data2, dataLen2);
287+
pcpp::SomeIpTpLayer someIpTpLayer2(0xd05f, 0x8001, 0, 0, 1, pcpp::SomeIpLayer::MsgType::REQUEST_NO_RETURN, 0, 91872,
288+
false, data2, dataLen2);
289289

290290
pcpp::Packet someIpTpPacket2(500);
291291
PTF_ASSERT_TRUE(someIpTpPacket2.addLayer(&ethLayer2));
@@ -306,15 +306,18 @@ PTF_TEST_CASE(SomeIpTpEditTest)
306306
const size_t dataLen2 = 225;
307307
uint8_t data2[dataLen2] = { 0 };
308308

309-
pcpp::SomeIpTpLayer someIpTpLayer(0x6059, 0x410c, 0x3, 0xa, 0x5, pcpp::SomeIpLayer::MsgType::REQUEST, 0, 91872 / 16,
309+
pcpp::SomeIpTpLayer someIpTpLayer(0x6059, 0x410c, 0x3, 0xa, 0x5, pcpp::SomeIpLayer::MsgType::REQUEST, 0, 91872,
310310
true, data2, dataLen2);
311-
someIpTpLayer.setOffset(123);
311+
someIpTpLayer.setOffset(1968);
312312

313-
PTF_ASSERT_EQUAL(someIpTpLayer.getOffset(), 123);
313+
PTF_ASSERT_EQUAL(someIpTpLayer.getOffset(), 1968);
314314
PTF_ASSERT_TRUE(someIpTpLayer.getMoreSegmentsFlag());
315315

316316
someIpTpLayer.setMoreSegmentsFlag(false);
317317

318-
PTF_ASSERT_EQUAL(someIpTpLayer.getOffset(), 123);
318+
PTF_ASSERT_EQUAL(someIpTpLayer.getOffset(), 1968);
319319
PTF_ASSERT_FALSE(someIpTpLayer.getMoreSegmentsFlag());
320+
321+
PTF_ASSERT_RAISES(someIpTpLayer.setOffset(17), std::invalid_argument,
322+
"Invalid offset - should be a multiple of 16");
320323
}

0 commit comments

Comments
 (0)