diff --git a/Packet++/src/UdpLayer.cpp b/Packet++/src/UdpLayer.cpp index 9cfaa2cbe..dec9cb9b4 100644 --- a/Packet++/src/UdpLayer.cpp +++ b/Packet++/src/UdpLayer.cpp @@ -37,11 +37,21 @@ namespace pcpp uint16_t UdpLayer::getSrcPort() const { + if (m_DataLen < sizeof(udphdr)) + { + PCPP_LOG_ERROR("Buffer too small to access source port"); + return 0; // Return an invalid port number + } return be16toh(getUdpHeader()->portSrc); } uint16_t UdpLayer::getDstPort() const { + if (m_DataLen < sizeof(udphdr)) + { + PCPP_LOG_ERROR("Buffer too small to access destination port"); + return 0; // Return an invalid port number + } return be16toh(getUdpHeader()->portDst); } @@ -150,6 +160,11 @@ namespace pcpp void UdpLayer::computeCalculateFields() { + if (m_DataLen < sizeof(udphdr)) + { + PCPP_LOG_ERROR("Buffer too small to calculate fields"); + return; + } udphdr* udpHdr = (udphdr*)m_Data; udpHdr->length = htobe16(m_DataLen); calculateChecksum(true);