[posix] fix the snprintf buffer overflow issue (#9251)

This commit is contained in:
Zhanglong Xia
2023-07-05 10:20:07 -07:00
committed by GitHub
parent cc6fa77b42
commit f7b189c882
+12 -5
View File
@@ -74,15 +74,22 @@ void GetFilename(Filename &aFilename, const char *aPattern)
int Daemon::OutputFormatV(const char *aFormat, va_list aArguments)
{
char buf[OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH + 1];
int rval;
static constexpr char truncatedMsg[] = "(truncated ...)";
char buf[OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH];
int rval;
buf[OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH] = '\0';
rval = vsnprintf(buf, sizeof(buf) - 1, aFormat, aArguments);
static_assert(sizeof(truncatedMsg) < OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH,
"OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH is too short!");
rval = vsnprintf(buf, sizeof(buf), aFormat, aArguments);
VerifyOrExit(rval >= 0, otLogWarnPlat("Failed to format CLI output: %s", strerror(errno)));
if (rval >= static_cast<int>(sizeof(buf)))
{
rval = static_cast<int>(sizeof(buf) - 1);
memcpy(buf + sizeof(buf) - sizeof(truncatedMsg), truncatedMsg, sizeof(truncatedMsg));
}
VerifyOrExit(mSessionSocket != -1);
#if defined(__linux__)