Skip to content

Commit e87ba00

Browse files
committed
Merge fix for String Sysex messages.
1 parent 1bb88b1 commit e87ba00

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Firmata.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,24 @@ void FirmataClass::processSysexMessage(void)
170170
case STRING_DATA:
171171
if(currentStringCallback) {
172172
byte bufferLength = (sysexBytesRead - 1) / 2;
173-
char *buffer = (char*)malloc(bufferLength * sizeof(char));
174173
byte i = 1;
175174
byte j = 0;
176175
while(j < bufferLength) {
177-
buffer[j] = (char)storedInputData[i];
176+
// The string length will only be at most half the size of the
177+
// stored input buffer so we can decode the string within the buffer.
178+
storedInputData[j] = storedInputData[i];
178179
i++;
179-
buffer[j] += (char)(storedInputData[i] << 7);
180+
storedInputData[j] += (storedInputData[i] << 7);
180181
i++;
181182
j++;
182183
}
183-
(*currentStringCallback)(buffer);
184+
// Make sure string is null terminated. This may be the case for data
185+
// coming from client libraries in languages that don't null terminate
186+
// strings.
187+
if (storedInputData[j-1] != '\0') {
188+
storedInputData[j] = '\0';
189+
}
190+
(*currentStringCallback)((char*)&storedInputData[0]);
184191
}
185192
break;
186193
default:
@@ -189,6 +196,7 @@ void FirmataClass::processSysexMessage(void)
189196
}
190197
}
191198

199+
192200
void FirmataClass::processInput(void)
193201
{
194202
int inputData = FirmataSerial->read(); // this is 'int' to handle -1 when no data

0 commit comments

Comments
 (0)