Add UDPC_free_PacketInfo_ptr(...)

Also add unit test for this function.
This commit is contained in:
Stephen Seo 2023-04-19 18:26:45 +09:00
parent 00b249bdc8
commit 3fac706d86
3 changed files with 43 additions and 8 deletions

View file

@ -161,8 +161,9 @@ typedef struct UDPC_EXPORT UDPC_ConnectionId {
* *
* If \ref data is NULL or \ref dataSize is 0, then this packet is invalid. * If \ref data is NULL or \ref dataSize is 0, then this packet is invalid.
* *
* \warning This struct must be free'd with a call to UDPC_free_PacketInfo to * \warning This struct must be free'd with a call to
* avoid a memory leak. * \ref UDPC_free_PacketInfo_ptr or \ref UDPC_free_PacketInfo to avoid a memory
* leak.
*/ */
typedef struct UDPC_EXPORT UDPC_PacketInfo { typedef struct UDPC_EXPORT UDPC_PacketInfo {
/*! /*!
@ -657,19 +658,37 @@ UDPC_EXPORT UDPC_Event UDPC_get_event(UDPC_HContext ctx, unsigned long *remainin
* \brief Get a received packet from a given UDPC context. * \brief Get a received packet from a given UDPC context.
* *
* \warning The received packet (if valid) must be free'd with a call to * \warning The received packet (if valid) must be free'd with a call to
* \ref UDPC_free_PacketInfo() to avoid a memory leak. * \ref UDPC_free_PacketInfo_ptr or \ref UDPC_free_PacketInfo to avoid a memory
* leak.
*/ */
UDPC_EXPORT UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned long *remaining); UDPC_EXPORT UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned long *remaining);
/*! /*!
* \brief Frees a UDPC_PacketInfo. * \brief Frees a UDPC_PacketInfo.
* *
* Internally, the member variable \ref UDPC_PacketInfo::data will be free'd and * Internally, the member variable \ref UDPC_PacketInfo::data will be free'd.
* set to NULL and \ref UDPC_PacketInfo::dataSize will be set to 0 if the given * \ref UDPC_free_PacketInfo_ptr is safer to use than this function, as it
* packet is valid. * also zeros out the relevant data to avoid double frees.
*/ */
UDPC_EXPORT void UDPC_free_PacketInfo(UDPC_PacketInfo pInfo); UDPC_EXPORT void UDPC_free_PacketInfo(UDPC_PacketInfo pInfo);
/*!
* \brief Frees a UDPC_PacketInfo.
*
* This is a safer alternative to \ref UDPC_free_PacketInfo because it
* internally zeroes out the internal pointer and size variables, making it
* safe to pass the same ptr multiple times to this function as it avoids a
* double free.
*
* Usage:
* \code{.c}
* UDPC_PacketInfo pinfo = UDPC_get_received(ctx, NULL);
* UDPC_free_PacketInfo_ptr(&pinfo);
* UDPC_free_PacketInfo_ptr(&pinfo); // This is safe, no double free.
* \endcode
*/
UDPC_EXPORT void UDPC_free_PacketInfo_ptr(UDPC_PacketInfo *pInfoPtr);
/*! /*!
* \brief Sets public/private keys used for packet verification * \brief Sets public/private keys used for packet verification
* *

View file

@ -2496,8 +2496,14 @@ UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned long *remaining) {
void UDPC_free_PacketInfo(UDPC_PacketInfo pInfo) { void UDPC_free_PacketInfo(UDPC_PacketInfo pInfo) {
if(pInfo.data && pInfo.dataSize > 0) { if(pInfo.data && pInfo.dataSize > 0) {
std::free(pInfo.data); std::free(pInfo.data);
pInfo.data = 0; }
pInfo.dataSize = 0; }
void UDPC_free_PacketInfo_ptr(UDPC_PacketInfo *pInfoPtr) {
if (pInfoPtr->data && pInfoPtr->dataSize > 0) {
std::free(pInfoPtr->data);
pInfoPtr->data = nullptr;
pInfoPtr->dataSize = 0;
} }
} }

View file

@ -4,6 +4,7 @@
#include <UDPC_Defines.hpp> #include <UDPC_Defines.hpp>
#include <cstdio> #include <cstdio>
#include <cstdlib>
#include <cstring> #include <cstring>
#include <future> #include <future>
@ -328,3 +329,12 @@ TEST(UDPC, a4toa6) {
a4 = htonl(0x01020304); a4 = htonl(0x01020304);
EXPECT_EQ(UDPC_a4toa6(a4), a6); EXPECT_EQ(UDPC_a4toa6(a4), a6);
} }
TEST(UDPC, free_packet_ptr) {
UDPC_PacketInfo pinfo;
pinfo.dataSize = 8;
pinfo.data = (char*)std::malloc(pinfo.dataSize);
UDPC_free_PacketInfo_ptr(&pinfo);
UDPC_free_PacketInfo_ptr(&pinfo);
}