Skip to content

Fix bugs and improve error handling in emerg-shutdown#359

Open
assisted-by-ai wants to merge 1 commit intoKicksecure:masterfrom
assisted-by-ai:claude/debug-emerg-shutdown-2jYUY
Open

Fix bugs and improve error handling in emerg-shutdown#359
assisted-by-ai wants to merge 1 commit intoKicksecure:masterfrom
assisted-by-ai:claude/debug-emerg-shutdown-2jYUY

Conversation

@assisted-by-ai
Copy link
Copy Markdown

This PR addresses several bugs and improves error handling in the emergency shutdown utility.

Key changes:

  • Remove unused include: Removed #include <stdbool.h> which was not needed
  • Fix console_fd initialization: Changed initial value from 0 to -1 to properly represent an uninitialized file descriptor
  • Fix string length calculation: Removed the + 1 from strlen(str) in the print() function to avoid writing the null terminator
  • Fix memory leak: Added free(arg_copy) when strtok() returns NULL in load_list() to prevent memory leaks on early return
  • Fix separator variable: Changed hardcoded "," to use the sep variable in the second strtok() call for consistency
  • Fix panic key detection logic: Inverted the condition in hw_monitor() to correctly detect when a key group is supported (changed from checking if key is NOT supported to checking if it IS supported)
  • Add missing exit calls: Added exit(0) after kill_system() calls to ensure proper program termination
  • Add input validation: Added null check for arg_part in fifo_monitor() to validate that the timeout value is not empty before parsing it

These changes improve code correctness, prevent resource leaks, and enhance error handling robustness.

https://claude.ai/code/session_016o12KW3j7F1dUBsXBXjfQg

- Fix print() writing NUL terminator to fd (off-by-one in strlen+1)
- Fix load_list() hardcoding "," separator instead of using sep param,
  breaking pipe-separated key alias parsing
- Add missing exit() after kill_system() in paranoid mode and
  --instant-shutdown to prevent fall-through on syscall failure
- Fix memory leak of arg_copy on early return in load_list()
- Fix overly strict device key support check: require any alias per
  group to be supported, not all aliases
- Add NULL check for empty --timeout= value to prevent NULL deref
- Remove duplicate stdbool.h include
- Initialize console_fd to -1 instead of 0 (stdin)

https://claude.ai/code/session_016o12KW3j7F1dUBsXBXjfQg
Copy link
Copy Markdown
Contributor

@ArrayBolt3 ArrayBolt3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly accepted in ArrayBolt3@17a9da5. Comments below.

#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepted.

Comment on lines -113 to +112
int console_fd = 0;
int console_fd = -1;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepted. (Doesn't actually do anything but this is probably a more reasonable default.)

Comment on lines -280 to +279
size_t len = strlen(str) + 1;
size_t len = strlen(str);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already applied from an earlier PR.


arg_part = strtok(arg_val, sep);
if (arg_part == NULL) {
free(arg_copy);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepted.

result_list[result_list_len - 1] = safe_calloc(1, strlen(arg_part) + 1);
strcpy(result_list[result_list_len - 1], arg_part);
} while ((arg_part = strtok(NULL, ",")) != NULL);
} while ((arg_part = strtok(NULL, sep)) != NULL);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already applied from an earlier PR.

if (paranoid_mode) {
/* Something was removed, we don't care what, shut down now */
kill_system();
exit(0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit() was moved into kill_system().

}

kill_system();
exit(0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit() was moved into kill_system().

Comment on lines +694 to +702
bool group_supported = false;
for (kg_idx = 0; panic_key_list[pkl_idx][kg_idx] != 0; kg_idx++) {
if (!bitset_get(key_flags, panic_key_list[pkl_idx][kg_idx])) {
supports_panic = false;
if (bitset_get(key_flags, panic_key_list[pkl_idx][kg_idx])) {
group_supported = true;
break;
}
}
if (!supports_panic) {
if (!group_supported) {
supports_panic = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepted.

Comment on lines +958 to +962
if (arg_part == NULL) {
print(fd_stderr, "Timeout value is empty!\n");
print_usage();
exit(1);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already accepted from an earlier PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants