Skip to content

Commit ce55c99

Browse files
authored
fix(midi): safeguard for memory leak
1 parent fa7d041 commit ce55c99

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

libraries/USB/src/USBMIDI.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ USBMIDI::USBMIDI() {
5050

5151
// private function for setting a not null/empty MIDI device name limited to 32 characters
5252
void USBMIDI::setDeviceName(const char* name) {
53+
printf("setName called");
5354
const uint8_t maxNameLength = 32; // tinyUSB Descriptor limit
5455
if (name != nullptr && strlen(name) > 0) {
5556
if (strlen(name) > maxNameLength) {
5657
log_w("USBMIDI: Device name too long, truncating to %d characters.", maxNameLength);
5758
}
58-
midiUserDeviceName = new char[maxNameLength + 1]; // +1 for null-terminator
59+
if (!midiUserDeviceName) {
60+
midiUserDeviceName = new char[maxNameLength + 1]; // +1 for null-terminator
61+
}
5962
if (midiUserDeviceName) {
6063
strncpy(midiUserDeviceName, name, maxNameLength);
6164
// Ensure null-termination when overflowing

0 commit comments

Comments
 (0)