-
Notifications
You must be signed in to change notification settings - Fork 0
Fix unsigned comparisons, integer overflows, and add defensive checks #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -38,7 +38,7 @@ void SerialCommandHandler::processCommands() { | |||||||||||||
|
|
||||||||||||||
| // Handle newline (end of command) | ||||||||||||||
| if (c == '\r' || c == '\n') { | ||||||||||||||
| if (buffer_index > 0) { | ||||||||||||||
| if (buffer_index > 0 && buffer_index < BUFFER_SIZE) { | ||||||||||||||
| command_buffer[buffer_index] = '\0'; | ||||||||||||||
| Serial.println(); // Echo newline | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -56,27 +56,25 @@ void SerialCommandHandler::processCommands() { | |||||||||||||
| args = space + 1; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (cmd != nullptr) { | ||||||||||||||
| if (strcmp(cmd, "STATUS") == 0) { | ||||||||||||||
| handleStatusCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "CONFIG") == 0) { | ||||||||||||||
| handleConfigCommand(args); | ||||||||||||||
| } else if (strcmp(cmd, "RESTART") == 0) { | ||||||||||||||
| handleRestartCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "DISCONNECT") == 0) { | ||||||||||||||
| handleDisconnectCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "CONNECT") == 0) { | ||||||||||||||
| handleConnectCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "STATS") == 0) { | ||||||||||||||
| handleStatsCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "HEALTH") == 0) { | ||||||||||||||
| handleHealthCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "HELP") == 0) { | ||||||||||||||
| handleHelpCommand(); | ||||||||||||||
| } else { | ||||||||||||||
| LOG_ERROR("Unknown command: %s", cmd); | ||||||||||||||
| handleHelpCommand(); | ||||||||||||||
| } | ||||||||||||||
| if (strcmp(cmd, "STATUS") == 0) { | ||||||||||||||
|
||||||||||||||
| handleStatusCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "CONFIG") == 0) { | ||||||||||||||
| handleConfigCommand(args); | ||||||||||||||
| } else if (strcmp(cmd, "RESTART") == 0) { | ||||||||||||||
| handleRestartCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "DISCONNECT") == 0) { | ||||||||||||||
| handleDisconnectCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "CONNECT") == 0) { | ||||||||||||||
| handleConnectCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "STATS") == 0) { | ||||||||||||||
| handleStatsCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "HEALTH") == 0) { | ||||||||||||||
| handleHealthCommand(); | ||||||||||||||
| } else if (strcmp(cmd, "HELP") == 0) { | ||||||||||||||
| handleHelpCommand(); | ||||||||||||||
| } else { | ||||||||||||||
| LOG_ERROR("Unknown command: %s", cmd); | ||||||||||||||
| handleHelpCommand(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| clearBuffer(); | ||||||||||||||
|
|
@@ -88,6 +86,12 @@ void SerialCommandHandler::processCommands() { | |||||||||||||
| if (buffer_index < BUFFER_SIZE - 1) { | ||||||||||||||
| command_buffer[buffer_index++] = c; | ||||||||||||||
| Serial.write(c); // Echo character | ||||||||||||||
| } else { | ||||||||||||||
| // Buffer full - warn user and ignore character | ||||||||||||||
| if (buffer_index == BUFFER_SIZE - 1) { | ||||||||||||||
| LOG_WARN("Command buffer full - command too long"); | ||||||||||||||
| clearBuffer(); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+91
to
+94
|
||||||||||||||
| if (buffer_index == BUFFER_SIZE - 1) { | |
| LOG_WARN("Command buffer full - command too long"); | |
| clearBuffer(); | |
| } | |
| LOG_WARN("Command buffer full - command too long"); | |
| clearBuffer(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The division-by-zero check for
current_buffer_sizeis defensive but may hide a deeper issue. The static membercurrent_buffer_sizeis initialized to 4096 at line 6 and set viainitialize(), so it should never be zero in normal operation. If it becomes zero, this indicates a serious bug elsewhere. Consider adding a LOG_ERROR or LOG_WARN here to alert developers that an unexpected state was encountered.