Why provide void execute()
with non-const char *cmd
?
#18
Unanswered
ondras12345
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Huhh it is a complicated question.
Thank you for your feedback, I think it is a good idea to do some housekeeping on overloaded versions of the execute function. // Required to use it without response. The idea was to make it possible to run this from some kind of
// pre-defined script. For example, a simple txt file from an SD card can contain some pre-defined commands.
// In this case, you don't have any output.
void execute( const char *cmd );
// This is the simplest version with a response channel. This way the user could print some data back,
// or even interact with the Stram object.
void execute( const char *cmd, Stream *resp );
// This is a tricky one. Shellminator and this library always developed together. The current release has a big flaw.
// It can not interact with the caller terminal. It is a problem when you have multiple terminal objects.
// You have to have a reference to the caller terminal somehow. This is why the parent argument is implemented.
// If you want to use this library without Shellminator, you can ignore the parent variable.
// I know it is not the safest thing to cast a pointer from a Shellminator object to void*, but I do not have a better idea
// right now without serious linker magic. It has to be Arduino-compatible.
void execute( const char *cmd, Stream *resp, void* parent ); From the user's point of view, if you want to use it with a non-constant type this would be the correct method: char* cmd = (char*)"help -d";
commander.execute( (const char*)cmd ); Am I understanding right your idea? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, there are several overloaded
execute()
methods.Commander-API/src/Commander-API.hpp
Lines 171 to 185 in 9d185dc
Looking at the implementation, it seems like the
const
and non-const
versions do essentially the same thing, except that theconst
version castscmnd
tochar *
before callingexecuteCommand
. This seems rather unsafe, because ifexecuteCommand
was to modify its argument, which was actually aconst char *
(e.g. a string literal), it would result in undefined behavior.Commander-API/src/Commander-API.cpp
Lines 524 to 542 in 9d185dc
I might very well be missing something, but it seems to me like it would be cleaner to declare
executeCommand
withconst char * cmd
, remove the cast and allexecute
methods with non-const
cmd
.executeCommand
copies the contents ofcmd
to its internal buffer regardless of which version ofexecute
was called.Commander-API/src/Commander-API.cpp
Lines 295 to 318 in 9d185dc
Was this a deliberate design decision? If so, what was the reasoning behind it? It is because you intend to have a version of
executeCommand
that does notstrncpy
in a future revision?Beta Was this translation helpful? Give feedback.
All reactions