Skip to content

Commit ec64e05

Browse files
eriknyquistsgbihu
authored andcommitted
advertisedServiceUuid: force caller to provide storage
Previously this method was returning the address of a function-local variable. All example sketches for Central were attempting to access this address after the method had returned, so would have invoked undefined behaviour.
1 parent ec7a65f commit ec64e05

File tree

9 files changed

+38
-31
lines changed

9 files changed

+38
-31
lines changed

libraries/BLE/examples/central/led_control/led_control.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const int buttonPin = 2;
2525
int oldButtonState = LOW;
2626

27+
char uuid_buf[70];
2728

2829
void setup() {
2930
Serial.begin(9600);
@@ -46,16 +47,16 @@ void loop() {
4647

4748
if (peripheral) {
4849
// discovered a peripheral, print out address, local name, and advertised service
50+
peripheral.advertisedServiceUuid(uuid_buf);
4951
Serial.print("Found ");
5052
Serial.print(peripheral.address());
5153
Serial.print(" '");
5254
Serial.print(peripheral.localName());
5355
Serial.print("' ");
54-
Serial.print(peripheral.advertisedServiceUuid());
55-
Serial.println();
56+
Serial.println(uuid_buf);
5657

5758
// see if peripheral is advertising the LED service
58-
if (peripheral.advertisedServiceUuid() == "19b10000-e8f2-537e-4f6c-d104768a1214") {
59+
if (String(uuid_buf) == String("19b10000-e8f2-537e-4f6c-d104768a1214")) {
5960
// stop scanning
6061
BLE.stopScanning();
6162

libraries/BLE/examples/central/peripheral_explorer/peripheral_explorer.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include <ArduinoBLE.h>
2121

22+
char uuid_buf[70];
23+
2224
void setup() {
2325
Serial.begin(9600);
2426

@@ -37,13 +39,13 @@ void loop() {
3739

3840
if (peripheral) {
3941
// discovered a peripheral, print out address, local name, and advertised service
42+
peripheral.advertisedServiceUuid(uuid_buf);
4043
Serial.print("Found ");
4144
Serial.print(peripheral.address());
4245
Serial.print(" '");
4346
Serial.print(peripheral.localName());
4447
Serial.print("' ");
45-
Serial.print(peripheral.advertisedServiceUuid());
46-
Serial.println();
48+
Serial.println(uuid_buf);
4749

4850
// see if peripheral is a SensorTag
4951
if (peripheral.localName() == "SensorTag") {

libraries/BLE/examples/central/scan/scan.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20+
char uuid_buf[70];
21+
2022
#include <ArduinoBLE.h>
2123

2224
void setup() {
@@ -54,8 +56,8 @@ void loop() {
5456
if (peripheral.hasAdvertisedServiceUuid()) {
5557
Serial.print("Service UUID's: ");
5658
for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
57-
Serial.print(peripheral.advertisedServiceUuid(i));
58-
Serial.print(" ");
59+
peripheral.advertisedServiceUuid(i, uuid_buf);
60+
Serial.print(String(uuid_buf) + " ");
5961
}
6062
Serial.println();
6163
}

libraries/BLE/examples/central/scan_callback/scan_callback.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include <ArduinoBLE.h>
2121

22+
char uuid_buf[70];
23+
2224
void setup() {
2325
Serial.begin(9600);
2426

@@ -58,8 +60,8 @@ void bleCentralDiscoverHandler(BLEDevice peripheral) {
5860
if (peripheral.hasAdvertisedServiceUuid()) {
5961
Serial.print("Service UUID's: ");
6062
for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
61-
Serial.print(peripheral.advertisedServiceUuid(i));
62-
Serial.print(" ");
63+
peripheral.advertisedServiceUuid(i, uuid_buf);
64+
Serial.print(String(uuid_buf) + " ");
6365
}
6466
Serial.println();
6567
}

libraries/BLE/examples/central/sensortag_button/sensortag_button.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include <ArduinoBLE.h>
2121

22+
uuid_buf[70];
23+
2224
void setup() {
2325
Serial.begin(9600);
2426

@@ -37,13 +39,14 @@ void loop() {
3739

3840
if (peripheral) {
3941
// discovered a peripheral, print out address, local name, and advertised service
42+
peripheral.advertisedServiceUuid(uuid_buf);
43+
4044
Serial.print("Found ");
4145
Serial.print(peripheral.address());
4246
Serial.print(" '");
4347
Serial.print(peripheral.localName());
4448
Serial.print("' ");
45-
Serial.print(peripheral.advertisedServiceUuid());
46-
Serial.println();
49+
Serial.println(uuid_buf);
4750

4851
// see if peripheral is a SensorTag
4952
if (peripheral.localName() == "SensorTag") {

libraries/BLE/src/BLEDevice.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,14 @@ String BLEDevice::localName() const
273273
return BLEDeviceManager::instance()->localName(this);
274274
}
275275

276-
String BLEDevice::advertisedServiceUuid() const
276+
void BLEDevice::advertisedServiceUuid(char *buf) const
277277
{
278-
return BLEDeviceManager::instance()->advertisedServiceUuid(this);
278+
BLEDeviceManager::instance()->advertisedServiceUuid(this, buf);
279279
}
280280

281-
String BLEDevice::advertisedServiceUuid(int index) const
281+
void BLEDevice::advertisedServiceUuid(int index, char *buf) const
282282
{
283-
return BLEDeviceManager::instance()->advertisedServiceUuid(this, index);
283+
BLEDeviceManager::instance()->advertisedServiceUuid(this, index, buf);
284284
}
285285

286286
int BLEDevice::rssi() const

libraries/BLE/src/BLEDevice.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ class BLEDevice
436436
int advertisedServiceUuidCount() const; // number of services the peripheral is advertising
437437

438438
String localName() const; // returns the advertised local name as a String
439-
String advertisedServiceUuid() const; // returns the advertised service as a UUID String
440-
String advertisedServiceUuid(int index) const; // returns the nth advertised service as a UUID String
439+
void advertisedServiceUuid(char *buf) const; // returns the advertised service as a UUID String
440+
void advertisedServiceUuid(int index, char *buf) const; // returns the nth advertised service as a UUID String
441441

442442
int rssi() const; // returns the RSSI of the peripheral at discovery
443443

libraries/BLE/src/internal/BLEDeviceManager.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -740,28 +740,26 @@ String BLEDeviceManager::localName(const BLEDevice* device) const
740740
return localname_string;
741741
}
742742

743-
String BLEDeviceManager::advertisedServiceUuid(const BLEDevice* device) const
743+
void BLEDeviceManager::advertisedServiceUuid(const BLEDevice* device, char *buf) const
744744
{
745-
return advertisedServiceUuid(device, 0);
745+
advertisedServiceUuid(device, 0, buf);
746746
}
747747

748-
String BLEDeviceManager::advertisedServiceUuid(const BLEDevice* device, int index) const
748+
void BLEDeviceManager::advertisedServiceUuid(const BLEDevice* device, int index, char *buf) const
749749
{
750750
const uint8_t* adv_data = NULL;
751751
uint8_t adv_data_len = 0;
752752
uint8_t service_cnt = 0;
753753
bt_uuid_128_t service_uuid;
754-
char uuid_string[37];
755-
memset(uuid_string, 0, sizeof(uuid_string));
756754

757755
if (BLEUtils::isLocalBLE(*device) == true)
758756
{
759757
// Local device only support advertise 1 service now.
760758
if (_has_service_uuid && index == 0)
761759
{
762-
BLEUtils::uuidBT2String(&service_uuid.uuid, uuid_string);
760+
BLEUtils::uuidBT2String(&service_uuid.uuid, buf);
763761
}
764-
return uuid_string;
762+
return;
765763
}
766764

767765
getDeviceAdvertiseBuffer(device->bt_le_address(),
@@ -770,7 +768,7 @@ String BLEDeviceManager::advertisedServiceUuid(const BLEDevice* device, int inde
770768

771769
if (NULL == adv_data)
772770
{
773-
return uuid_string;
771+
return;
774772
}
775773

776774
while (adv_data_len > 1)
@@ -781,12 +779,12 @@ String BLEDeviceManager::advertisedServiceUuid(const BLEDevice* device, int inde
781779
/* Check for early termination */
782780
if (len == 0)
783781
{
784-
return uuid_string;
782+
return;
785783
}
786784

787785
if ((len + 1 > adv_data_len) || (adv_data_len < 2)) {
788786
pr_info(LOG_MODULE_BLE, "AD malformed\n");
789-
return uuid_string;
787+
return;
790788
}
791789

792790
if (type == BT_DATA_UUID16_ALL ||
@@ -808,15 +806,14 @@ String BLEDeviceManager::advertisedServiceUuid(const BLEDevice* device, int inde
808806
memcpy(service_uuid.val, &adv_data[2], 16);
809807
}
810808

811-
BLEUtils::uuidBT2String(&service_uuid.uuid, uuid_string);
809+
BLEUtils::uuidBT2String(&service_uuid.uuid, buf);
812810

813811
break;
814812
}
815813

816814
adv_data_len -= len + 1;
817815
adv_data += len + 1;
818816
}
819-
return uuid_string;
820817
}
821818

822819
int BLEDeviceManager::rssi(const BLEDevice* device) const

libraries/BLE/src/internal/BLEDeviceManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ class BLEDeviceManager
316316
int advertisedServiceUuidCount(const BLEDevice* device) const; // number of services the peripheral is advertising
317317

318318
String localName(const BLEDevice* device) const; // returns the advertised local name as a String
319-
String advertisedServiceUuid(const BLEDevice* device) const; // returns the advertised service as a UUID String
320-
String advertisedServiceUuid(const BLEDevice* device, int index) const; // returns the nth advertised service as a UUID String
319+
void advertisedServiceUuid(const BLEDevice* device, char *buf) const; // returns the advertised service as a UUID String
320+
void advertisedServiceUuid(const BLEDevice* device, int index, char *buf) const; // returns the nth advertised service as a UUID String
321321

322322
int rssi(const BLEDevice* device) const; // returns the RSSI of the peripheral at discovery
323323

0 commit comments

Comments
 (0)