File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff 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+
192200void FirmataClass::processInput (void )
193201{
194202 int inputData = FirmataSerial->read (); // this is 'int' to handle -1 when no data
You can’t perform that action at this time.
0 commit comments