diff --git a/src/tools/fs_shell/external_commands_beos.cpp b/src/tools/fs_shell/external_commands_beos.cpp index 519bc13378..84b66e367a 100644 --- a/src/tools/fs_shell/external_commands_beos.cpp +++ b/src/tools/fs_shell/external_commands_beos.cpp @@ -11,8 +11,10 @@ #include "external_commands.h" #include "fs_shell_command_beos.h" + static port_id sReplyPort = -1; + static port_id get_command_port() { @@ -29,52 +31,54 @@ get_command_port() bool -get_external_command(char *input, int len) +FSShell::get_external_command(char *input, int len) { // get/create the port port_id port = get_command_port(); if (port < 0) { - fprintf(stderr, "Failed to create command port: %s\n", strerror(port)); + fprintf(stderr, "Error: Failed to create command port: %s\n", + strerror(port)); return false; } while (true) { // read a message - external_command_message message; + char _message[sizeof(external_command_message) + kMaxCommandLength]; + external_command_message* message = (external_command_message*)_message; ssize_t bytesRead; do { int32 code; - bytesRead = read_port(port, &code, &message, sizeof(message)); + bytesRead = read_port(port, &code, message, sizeof(_message)); } while (bytesRead == B_INTERRUPTED); if (bytesRead < 0) { - fprintf(stderr, "Reading from port failed: %s\n", + fprintf(stderr, "Error: Reading from port failed: %s\n", strerror(bytesRead)); return false; } // get the len of the command - int commandLen = (char*)&message + bytesRead - message.command; + int commandLen = _message + bytesRead - message->command; if (commandLen <= 1) { - fprintf(stderr, "No command given.\n"); + fprintf(stderr, "Error: No command given.\n"); continue; } if (commandLen > len) { - fprintf(stderr, "Command too long. Ignored.\n"); + fprintf(stderr, "Error: Command too long. Ignored.\n"); continue; } // copy the command - memcpy(input, message.command, commandLen); + memcpy(input, message->command, commandLen); input[len - 1] = '\0'; // always NULL-terminate - sReplyPort = message.reply_port; + sReplyPort = message->reply_port; return true; } } void -reply_to_external_command(int result) +FSShell::reply_to_external_command(int result) { if (sReplyPort >= 0) { // prepare the message @@ -89,15 +93,15 @@ reply_to_external_command(int result) sReplyPort = -1; if (error != B_OK) { - fprintf(stderr, "Failed to send command result to reply port: %s\n", - strerror(error)); + fprintf(stderr, "Error: Failed to send command result to reply " + "port: %s\n", strerror(error)); } } } void -external_command_cleanup() +FSShell::external_command_cleanup() { // The port will be deleted automatically when the team exits. } diff --git a/src/tools/fs_shell/fs_shell_command_beos.cpp b/src/tools/fs_shell/fs_shell_command_beos.cpp index fe5b3f27dc..e72184f5a8 100644 --- a/src/tools/fs_shell/fs_shell_command_beos.cpp +++ b/src/tools/fs_shell/fs_shell_command_beos.cpp @@ -16,8 +16,15 @@ bool send_external_command(const char *command, int *result) { - external_command_message message; - strncpy(message.command, command, sizeof(message.command)); + int commandLen = strlen(command); + if (commandLen > kMaxCommandLength) { + fprintf(stderr, "Error: Command line too long.\n"); + return false; + } + + char _message[sizeof(external_command_message) + kMaxCommandLength]; + external_command_message* message = (external_command_message*)_message; + strcpy(message->command, command); // find the command port port_id commandPort = find_port(kFSShellCommandPort); @@ -33,12 +40,13 @@ send_external_command(const char *command, int *result) strerror(replyPort)); return false; } - message.reply_port = replyPort; + message->reply_port = replyPort; // send the command message status_t error; do { - error = write_port(commandPort, 0, &message, sizeof(message)); + error = write_port(commandPort, 0, message, + sizeof(external_command_message) + commandLen); } while (error == B_INTERRUPTED); if (error != B_OK) { diff --git a/src/tools/fs_shell/fs_shell_command_beos.h b/src/tools/fs_shell/fs_shell_command_beos.h index e90682c5a6..2b9ed6db54 100644 --- a/src/tools/fs_shell/fs_shell_command_beos.h +++ b/src/tools/fs_shell/fs_shell_command_beos.h @@ -5,12 +5,15 @@ #ifndef FS_SHELL_COMMAND_BEOS_H #define FS_SHELL_COMMAND_BEOS_H +#include + + static const char *kFSShellCommandPort = "fs shell command port"; +static const int kMaxCommandLength = 102400; struct external_command_message { port_id reply_port; - char command[20480]; -// TODO: Increase command size and transmit only as much as necessary. + char command[1]; }; struct external_command_reply {