[cli] fix crash when full logs is on (#5408)

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.
This commit is contained in:
Simon Lin
2020-08-18 08:43:12 -07:00
committed by GitHub
parent 962c06f8e7
commit 60d2569fa2
4 changed files with 26 additions and 18 deletions
+6
View File
@@ -251,6 +251,8 @@ const struct Command Interpreter::sCommands[] = {
{"version", &Interpreter::ProcessVersion},
};
Interpreter *Interpreter::sInterpreter = nullptr;
Interpreter::Interpreter(Instance *aInstance)
: mUserCommands(nullptr)
, mUserCommandsLength(0)
@@ -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
+17 -1
View File
@@ -115,7 +115,20 @@ public:
* @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.
@@ -236,6 +249,9 @@ public:
*/
void SetUserCommands(const otCliCommand *aCommands, uint8_t aLength);
protected:
static Interpreter *sInterpreter;
private:
enum
{
+1 -8
View File
@@ -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)
+2 -9
View File
@@ -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)