Update lambda_magic.rst (#4187)

Add some fixes in example code to make it more robust:
- Prevent from buffer overflow
- Add check on valid character
This commit is contained in:
Anton Verburg 2024-08-22 19:34:50 +02:00 committed by GitHub
parent bc5c886714
commit 2cf3f586df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -128,17 +128,24 @@ With this you can use automations or lambda to set switch or sensor states.
if (readch > 0) {
switch (readch) {
case '\n': // Ignore new-lines
break;
case '\r': // Return on CR
case '\n':
case '\r': // Return on CR or newline
buffer[pos] = 0; // Just to be sure, set last character 0
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len-1) {
if ((pos < len-1) && ( readch < 127 )) { // Filter on <127 to make sure it is a character
buffer[pos++] = readch;
buffer[pos] = 0;
}
else
{
buffer[pos] = 0; // Just to be sure, set last character 0
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
}
}
}
// No end of line has been found, so return -1.