From 5592621ce5554523a107a4f0a70c6f8433f8bc2f Mon Sep 17 00:00:00 2001 From: Mankianer Date: Thu, 1 Feb 2024 14:43:30 +0100 Subject: [PATCH] Fix UUID string handling in BLEUuid This commit addresses an issue in the BLEUuid class where the UUID string was not being properly copied. Previously, the class stored a pointer to the original string, which could lead to issues if the original string was modified or went out of scope. The fix involves changing the _str member of the BLEUuid class to be a non-const char pointer, and using the strdup function to create a copy of the string in the BLEUuid constructor. The destructor of the BLEUuid class has also been updated to free the memory allocated for the string copy. This change ensures that the BLEUuid class has its own copy of the UUID string, preventing potential issues caused by changes to the original string. Signed-off-by: Mankianer --- src/utility/BLEUuid.cpp | 9 +++++++-- src/utility/BLEUuid.h | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/utility/BLEUuid.cpp b/src/utility/BLEUuid.cpp index 0465ea9a..d822473c 100644 --- a/src/utility/BLEUuid.cpp +++ b/src/utility/BLEUuid.cpp @@ -22,9 +22,9 @@ #include "BLEUuid.h" -BLEUuid::BLEUuid(const char * str) : - _str(str) +BLEUuid::BLEUuid(const char * str) { + _str = strdup(str); char temp[] = {0, 0, 0}; memset(_data, 0x00, sizeof(_data)); @@ -56,6 +56,11 @@ BLEUuid::BLEUuid(const char * str) : } } +BLEUuid::~BLEUuid() +{ + free(_str); +} + const char* BLEUuid::str() const { return _str; diff --git a/src/utility/BLEUuid.h b/src/utility/BLEUuid.h index f6836bf6..6219ba4c 100644 --- a/src/utility/BLEUuid.h +++ b/src/utility/BLEUuid.h @@ -28,6 +28,7 @@ class BLEUuid { public: BLEUuid(const char * str); + ~BLEUuid(); const char* str() const; const uint8_t * data() const; @@ -36,7 +37,7 @@ class BLEUuid static const char* uuidToString(const uint8_t* data, uint8_t length); private: - const char* _str; + char* _str; uint8_t _data[BLE_UUID_MAX_LENGTH]; uint8_t _length; };