Skip to content

Commit

Permalink
Update lambda_magic.rst (#4187)
Browse files Browse the repository at this point in the history
Add some fixes in example code to make it more robust:
- Prevent from buffer overflow
- Add check on valid character
  • Loading branch information
antonverburg authored Aug 22, 2024
1 parent bc5c886 commit 2cf3f58
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions cookbook/lambda_magic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2cf3f58

Please sign in to comment.