Skip to content

Commit

Permalink
Fix UUID string handling in BLEUuid
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Mankianer committed Feb 1, 2024
1 parent dff9e41 commit 5592621
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/utility/BLEUuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -56,6 +56,11 @@ BLEUuid::BLEUuid(const char * str) :
}
}

BLEUuid::~BLEUuid()
{
free(_str);
}

const char* BLEUuid::str() const
{
return _str;
Expand Down
3 changes: 2 additions & 1 deletion src/utility/BLEUuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BLEUuid
{
public:
BLEUuid(const char * str);
~BLEUuid();

const char* str() const;
const uint8_t * data() const;
Expand All @@ -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;
};
Expand Down

0 comments on commit 5592621

Please sign in to comment.