Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash with parseuntil when top layer is more than the chosen parseuntil OSI layer. #1580

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Packet++/src/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,17 @@ namespace pcpp

if (curLayer != nullptr && curLayer->getOsiModelLayer() > parseUntilLayer)
{
m_LastLayer = curLayer->getPrevLayer();
delete curLayer;
m_LastLayer->m_NextLayer = nullptr;
// don't delete the first layer. If already past the target layer, treat the same as if the layer was found.
if (curLayer == m_FirstLayer)
{
curLayer->m_IsAllocatedInPacket = true;
}
else
{
m_LastLayer = curLayer->getPrevLayer();
delete curLayer;
m_LastLayer->m_NextLayer = nullptr;
}
}

if (m_LastLayer != nullptr && parseUntil == UnknownProtocol && parseUntilLayer == OsiModelLayerUnknown)
Expand Down
1 change: 1 addition & 0 deletions Tests/Packet++Test/TestDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ PTF_TEST_CASE(PacketTrailerTest);
PTF_TEST_CASE(ResizeLayerTest);
PTF_TEST_CASE(PrintPacketAndLayersTest);
PTF_TEST_CASE(ProtocolFamilyMembershipTest);
PTF_TEST_CASE(PacketParseLayerLimitTest);

// Implemented in HttpTests.cpp
PTF_TEST_CASE(HttpRequestParseMethodTest);
Expand Down
14 changes: 14 additions & 0 deletions Tests/Packet++Test/Tests/PacketTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,17 @@ PTF_TEST_CASE(ProtocolFamilyMembershipTest)
PTF_ASSERT_FALSE(httpLayer->isMemberOfProtocolFamily(pcpp::HTTPResponse));
PTF_ASSERT_FALSE(httpLayer->isMemberOfProtocolFamily(pcpp::IP));
}

PTF_TEST_CASE(PacketParseLayerLimitTest)
{
timeval time;
gettimeofday(&time, nullptr);

READ_FILE_AND_CREATE_PACKET(0, "PacketExamples/TcpPacketWithOptions3.dat");
pcpp::Packet packet0(&rawPacket0, pcpp::OsiModelPhysicalLayer);
PTF_ASSERT_EQUAL(packet0.getLastLayer(), packet0.getFirstLayer());

READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/TcpPacketWithOptions3.dat");
pcpp::Packet packet1(&rawPacket1, pcpp::OsiModelTransportLayer);
PTF_ASSERT_EQUAL(packet1.getLastLayer()->getOsiModelLayer(), pcpp::OsiModelTransportLayer);
}
1 change: 1 addition & 0 deletions Tests/Packet++Test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ int main(int argc, char* argv[])
PTF_RUN_TEST(ResizeLayerTest, "packet;resize");
PTF_RUN_TEST(PrintPacketAndLayersTest, "packet;print");
PTF_RUN_TEST(ProtocolFamilyMembershipTest, "packet");
PTF_RUN_TEST(PacketParseLayerLimitTest, "packet");

PTF_RUN_TEST(HttpRequestParseMethodTest, "http");
PTF_RUN_TEST(HttpRequestLayerParsingTest, "http");
Expand Down
Loading