Skip to content

Commit 5592621

Browse files
committed
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 <mankianer@gmail.com>
1 parent dff9e41 commit 5592621

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/utility/BLEUuid.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
#include "BLEUuid.h"
2424

25-
BLEUuid::BLEUuid(const char * str) :
26-
_str(str)
25+
BLEUuid::BLEUuid(const char * str)
2726
{
27+
_str = strdup(str);
2828
char temp[] = {0, 0, 0};
2929

3030
memset(_data, 0x00, sizeof(_data));
@@ -56,6 +56,11 @@ BLEUuid::BLEUuid(const char * str) :
5656
}
5757
}
5858

59+
BLEUuid::~BLEUuid()
60+
{
61+
free(_str);
62+
}
63+
5964
const char* BLEUuid::str() const
6065
{
6166
return _str;

src/utility/BLEUuid.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class BLEUuid
2828
{
2929
public:
3030
BLEUuid(const char * str);
31+
~BLEUuid();
3132

3233
const char* str() const;
3334
const uint8_t * data() const;
@@ -36,7 +37,7 @@ class BLEUuid
3637
static const char* uuidToString(const uint8_t* data, uint8_t length);
3738

3839
private:
39-
const char* _str;
40+
char* _str;
4041
uint8_t _data[BLE_UUID_MAX_LENGTH];
4142
uint8_t _length;
4243
};

0 commit comments

Comments
 (0)