Skip to content

Commit

Permalink
thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACC…
Browse files Browse the repository at this point in the history
…ESS (code=EXC_I386_GPFLT) (project-chip#3339)
  • Loading branch information
vivien-apple authored Oct 27, 2020
1 parent f6157a2 commit 266a378
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 9 deletions.
70 changes: 67 additions & 3 deletions examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <sys/socket.h>
#include <sys/types.h>

#include <support/SafeInt.h>
#include <support/logging/CHIPLogging.h>

bool Command::InitArguments(int argc, char ** argv)
Expand Down Expand Up @@ -97,9 +98,59 @@ bool Command::InitArgument(size_t argIndex, const char * argValue)
break;
}

case ArgumentType::Number: {
uint32_t * value = reinterpret_cast<uint32_t *>(arg.value);
case ArgumentType::Number_uint8: {
uint8_t * value = reinterpret_cast<uint8_t *>(arg.value);

// stringstream treats uint8_t as char, which is not what we want here.
uint16_t tmpValue;
std::stringstream ss(argValue);
ss >> tmpValue;
if (chip::CanCastTo<uint8_t>(tmpValue))
{
*value = static_cast<uint8_t>(tmpValue);
isValidArgument = (!ss.fail() && ss.eof() && *value >= arg.min && *value <= arg.max);
}
else
{
isValidArgument = false;
}
break;
}

case ArgumentType::Number_uint16: {
uint16_t * value = reinterpret_cast<uint16_t *>(arg.value);
std::stringstream ss(argValue);
ss >> *value;
isValidArgument = (!ss.fail() && ss.eof() && *value >= arg.min && *value <= arg.max);
break;
}

case ArgumentType::Number_uint32: {
uint32_t * value = reinterpret_cast<uint32_t *>(arg.value);
std::stringstream ss(argValue);
ss >> *value;
isValidArgument = (!ss.fail() && ss.eof() && *value >= arg.min && *value <= arg.max);
break;
}

case ArgumentType::Number_int8: {
int8_t * value = reinterpret_cast<int8_t *>(arg.value);
std::stringstream ss(argValue);
ss >> *value;
isValidArgument = (!ss.fail() && ss.eof() && *value >= arg.min && *value <= arg.max);
break;
}

case ArgumentType::Number_int16: {
int16_t * value = reinterpret_cast<int16_t *>(arg.value);
std::stringstream ss(argValue);
ss >> *value;
isValidArgument = (!ss.fail() && ss.eof() && *value >= arg.min && *value <= arg.max);
break;
}

case ArgumentType::Number_int32: {
int32_t * value = reinterpret_cast<int32_t *>(arg.value);
std::stringstream ss(argValue);
ss >> *value;
isValidArgument = (!ss.fail() && ss.eof() && *value >= arg.min && *value <= arg.max);
Expand Down Expand Up @@ -154,10 +205,23 @@ size_t Command::AddArgument(const char * name, AddressWithInterface * out)
return mArgs.size();
}

size_t Command::AddArgument(const char * name, int64_t min, int64_t max, void * out, ArgumentType type)
{
Argument arg;
arg.type = type;
arg.name = name;
arg.value = out;
arg.min = min;
arg.max = max;

mArgs.emplace(mArgs.begin(), arg);
return mArgs.size();
}

size_t Command::AddArgument(const char * name, int64_t min, int64_t max, void * out)
{
Argument arg;
arg.type = ArgumentType::Number;
arg.type = ArgumentType::Number_uint8;
arg.name = name;
arg.value = out;
arg.min = min;
Expand Down
37 changes: 31 additions & 6 deletions examples/chip-tool/commands/common/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ typedef std::initializer_list<movable_initializer_list> commands_list;

enum ArgumentType
{
Number,
Number_uint8,
Number_uint16,
Number_uint32,
Number_int8,
Number_int16,
Number_int32,
String,
Attribute,
Address
Expand Down Expand Up @@ -82,11 +87,6 @@ class Command
size_t GetArgumentsCount(void) const { return mArgs.size(); }

bool InitArguments(int argc, char ** argv);
template <class T>
size_t AddArgument(const char * name, int64_t min, int64_t max, T * out)
{
return AddArgument(name, min, max, reinterpret_cast<void *>(out));
}
size_t AddArgument(const char * name, const char * value);
/**
* @brief
Expand All @@ -98,6 +98,30 @@ class Command
*/
size_t AddArgument(const char * name, char ** value);
size_t AddArgument(const char * name, AddressWithInterface * out);
size_t AddArgument(const char * name, int64_t min, int64_t max, int8_t * out)
{
return AddArgument(name, min, max, reinterpret_cast<void *>(out), Number_int8);
}
size_t AddArgument(const char * name, int64_t min, int64_t max, int16_t * out)
{
return AddArgument(name, min, max, reinterpret_cast<void *>(out), Number_int16);
}
size_t AddArgument(const char * name, int64_t min, int64_t max, int32_t * out)
{
return AddArgument(name, min, max, reinterpret_cast<void *>(out), Number_int32);
}
size_t AddArgument(const char * name, int64_t min, int64_t max, uint8_t * out)
{
return AddArgument(name, min, max, reinterpret_cast<void *>(out), Number_uint8);
}
size_t AddArgument(const char * name, int64_t min, int64_t max, uint16_t * out)
{
return AddArgument(name, min, max, reinterpret_cast<void *>(out), Number_uint16);
}
size_t AddArgument(const char * name, int64_t min, int64_t max, uint32_t * out)
{
return AddArgument(name, min, max, reinterpret_cast<void *>(out), Number_uint32);
}
size_t AddArgument(const char * name, const void * value) { return 0; };

virtual CHIP_ERROR Run(ChipDeviceController * dc, NodeId remoteId) = 0;
Expand All @@ -107,6 +131,7 @@ class Command

private:
bool InitArgument(size_t argIndex, const char * argValue);
size_t AddArgument(const char * name, int64_t min, int64_t max, void * out, ArgumentType type);
size_t AddArgument(const char * name, int64_t min, int64_t max, void * out);

bool mCommandExitStatus = false;
Expand Down

0 comments on commit 266a378

Please sign in to comment.