mirror of
https://github.com/esphome/esphome-docs.git
synced 2025-02-28 03:41:57 +01:00
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:
parent
bc5c886714
commit
2cf3f586df
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user