Skip to content

Commit

Permalink
[cli] fix crash when full logs is on (#5408)
Browse files Browse the repository at this point in the history
It is possible that log is printed before the Interpreter (UART or
CONSOLE) object is created when FULL LOGS is enabled. In such case, OT
crashes. This commit fixes this bug.
  • Loading branch information
simonlingoogle committed Aug 18, 2020
1 parent 962c06f commit 54b3192
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ const struct Command Interpreter::sCommands[] = {
{"version", &Interpreter::ProcessVersion},
};

Interpreter *Interpreter::sInterpreter = nullptr;

Interpreter::Interpreter(Instance *aInstance)
: mUserCommands(nullptr)
, mUserCommandsLength(0)
Expand Down Expand Up @@ -4679,8 +4681,12 @@ extern "C" void otCliPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, cons
OT_UNUSED_VARIABLE(aLogLevel);
OT_UNUSED_VARIABLE(aLogRegion);

VerifyOrExit(Interpreter::IsInitialized(), OT_NOOP);

Interpreter::GetInterpreter().OutputFormatV(aFormat, aArgs);
Interpreter::GetInterpreter().OutputFormat("\r\n");
exit:
return;
}

} // namespace Cli
Expand Down
18 changes: 17 additions & 1 deletion src/cli/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,20 @@ class Interpreter
* @returns A reference to the interpreter object.
*
*/
static Interpreter &GetInterpreter(void);
static Interpreter &GetInterpreter(void)
{
OT_ASSERT(sInterpreter != nullptr);

return *sInterpreter;
}

/**
* This method returns whether the interpreter is initialized.
*
* @returns Whether the interpreter is initialized.
*
*/
static bool IsInitialized(void) { return sInterpreter != nullptr; }

/**
* This method interprets a CLI command.
Expand Down Expand Up @@ -236,6 +249,9 @@ class Interpreter
*/
void SetUserCommands(const otCliCommand *aCommands, uint8_t aLength);

protected:
static Interpreter *sInterpreter;

private:
enum
{
Expand Down
9 changes: 1 addition & 8 deletions src/cli/cli_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,11 @@ extern "C" void otPlatUartSendDone(void)
{
}

Console *Console::sConsole = nullptr;

Interpreter &Interpreter::GetInterpreter(void)
{
return *Console::sConsole;
}

void Console::Initialize(otInstance *aInstance, otCliConsoleOutputCallback aCallback, void *aContext)
{
Instance *instance = static_cast<Instance *>(aInstance);

sConsole = new (&sCliConsoleRaw) Console(instance, aCallback, aContext);
Interpreter::sInterpreter = new (&sCliConsoleRaw) Console(instance, aCallback, aContext);
}

Console::Console(Instance *aInstance, otCliConsoleOutputCallback aCallback, void *aContext)
Expand Down
11 changes: 2 additions & 9 deletions src/cli/cli_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,10 @@ namespace Cli {

static OT_DEFINE_ALIGNED_VAR(sCliUartRaw, sizeof(Uart), uint64_t);

Uart *Uart::sUart = nullptr;

Interpreter &Interpreter::GetInterpreter(void)
{
return *Uart::sUart;
}

void Uart::Initialize(otInstance *aInstance)
{
Instance *instance = static_cast<Instance *>(aInstance);
sUart = new (&sCliUartRaw) Uart(instance);
Instance *instance = static_cast<Instance *>(aInstance);
Interpreter::sInterpreter = new (&sCliUartRaw) Uart(instance);
}

Uart::Uart(Instance *aInstance)
Expand Down

0 comments on commit 54b3192

Please sign in to comment.