Merge pull request #3395 from jbrazio/fix/serial-command-clamp

[HIGH] Ensure command buffers stay NUL-terminated to prevent overflow
This commit is contained in:
ripplebiz
2026-09-11 14:09:24 +10:00
committed by GitHub
5 changed files with 50 additions and 10 deletions
+10 -2
View File
@@ -2243,6 +2243,13 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char*
void MyMesh::checkCLIRescueCmd() {
int len = strlen(cli_command);
// `cli_command` must stay NUL-terminated within its bounds. If it ever isn't,
// strlen() above can return >= sizeof(cli_command) and the loop below would
// then index past the buffer, so clamp defensively.
if (len >= (int)sizeof(cli_command)) {
cli_command[0] = 0;
len = 0;
}
while (Serial.available() && len < sizeof(cli_command)-1) {
char c = Serial.read();
if (c != '\n') {
@@ -2251,8 +2258,9 @@ void MyMesh::checkCLIRescueCmd() {
}
Serial.print(c); // echo
}
if (len == sizeof(cli_command)-1) { // command buffer full
cli_command[sizeof(cli_command)-1] = '\r';
if (len == sizeof(cli_command)-1) { // buffer full: treat as a completed line
cli_command[sizeof(cli_command)-2] = '\r'; // place end-of-line marker inside the buffer
cli_command[sizeof(cli_command)-1] = 0; // keep the buffer NUL-terminated
}
if (len > 0 && cli_command[len - 1] == '\r') { // received complete line