Skip to content

Commit 07dcca4

Browse files
committed
EncryptedBatteryMonitor: fix memory management bugs
There was a buffer overflow in the EncryptedBatteryMonitor example code when formatting the 6-digit confirmation code for display during pairing, since the terminating null character was not accounted for in the buffer size. Also, there was a memory leak due to unnecessary dynamic allocation when setting the initial value of the string characteristic. Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
1 parent 6091ea6 commit 07dcca4

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

examples/Peripheral/EncryptedBatteryMonitor/EncryptedBatteryMonitor.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void setup() {
5959
BLE.setDisplayCode([](uint32_t confirmCode){
6060
Serial.println("New device pairing request.");
6161
Serial.print("Confirm code matches pairing device: ");
62-
char code[6];
62+
char code[7];
6363
sprintf(code, "%06d", confirmCode);
6464
Serial.println(code);
6565
});
@@ -176,8 +176,7 @@ void setup() {
176176

177177
BLE.addService(batteryService); // Add the battery service
178178
batteryLevelChar.writeValue(oldBatteryLevel); // set initial value for this characteristic
179-
char* stringCharValue = new char[32];
180-
stringCharValue = "string";
179+
const char* stringCharValue = "string";
181180
stringcharacteristic.writeValue(stringCharValue);
182181
secretValue.writeValue(0);
183182

0 commit comments

Comments
 (0)