mirror of
https://github.com/espressif/openthread.git
synced 2026-08-01 16:47:47 +00:00
[log] fix uninitialized string use (#6584)
This commit fixes an issue with some of the log related methods where
`String` object may be used without being initialized (this was added
from commit b39f5b6dd and PR #6564).
This commit ensures that `String` variable definitions are always
immediately followed by a `StringWriter()` definition which then
initializes the string buffer (sets it as empty).
This commit also updates `test_string` unit test.
This commit is contained in:
+33
-39
@@ -49,12 +49,13 @@ template <uint16_t kSize> void PrintString(const char *aName, const String<kSize
|
||||
void TestStringWriter(void)
|
||||
{
|
||||
String<kStringSize> str;
|
||||
StringWriter writer(str);
|
||||
constexpr char kLongString[] = "abcdefghijklmnopqratuvwxyzabcdefghijklmnopqratuvwxyz";
|
||||
|
||||
printf("\nTest 1: StringWriter constructor\n");
|
||||
|
||||
VerifyOrQuit(StringWriter(str).GetSize() == kStringSize, "GetSize() failed");
|
||||
VerifyOrQuit(StringWriter(str).GetLength() == 0, "GetLength() failed for empty string");
|
||||
VerifyOrQuit(writer.GetSize() == kStringSize, "GetSize() failed");
|
||||
VerifyOrQuit(writer.GetLength() == 0, "GetLength() failed for empty string");
|
||||
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "") == 0, "String content is incorrect");
|
||||
|
||||
@@ -62,51 +63,44 @@ void TestStringWriter(void)
|
||||
|
||||
printf(" -- PASS\n");
|
||||
|
||||
printf("\nTest 2: String::Append() method\n");
|
||||
printf("\nTest 2: StringWriter::Append() method\n");
|
||||
|
||||
{
|
||||
StringWriter writer(str);
|
||||
writer.Append("Hi");
|
||||
VerifyOrQuit(writer.GetLength() == 2, "GetLength() failed");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "Hi") == 0, "String content is incorrect");
|
||||
PrintString("str", str);
|
||||
|
||||
writer.Append("Hi");
|
||||
VerifyOrQuit(writer.GetLength() == 2, "GetLength() failed");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "Hi") == 0, "String content is incorrect");
|
||||
PrintString("str", str);
|
||||
writer.Append("%s%d", "!", 12);
|
||||
VerifyOrQuit(writer.GetLength() == 5, "GetLength() failed");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "Hi!12") == 0, "String content is incorrect");
|
||||
PrintString("str", str);
|
||||
|
||||
writer.Append("%s%d", "!", 12);
|
||||
VerifyOrQuit(writer.GetLength() == 5, "GetLength() failed");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "Hi!12") == 0, "String content is incorrect");
|
||||
PrintString("str", str);
|
||||
writer.Append(kLongString);
|
||||
VerifyOrQuit(writer.IsTruncated() && writer.GetLength() == 5 + sizeof(kLongString) - 1,
|
||||
"String::Append() did not handle overflow buffer correctly");
|
||||
PrintString("str", str);
|
||||
|
||||
writer.Append(kLongString);
|
||||
VerifyOrQuit(writer.IsTruncated() && writer.GetLength() == 5 + sizeof(kLongString) - 1,
|
||||
"String::Append() did not handle overflow buffer correctly");
|
||||
PrintString("str", str);
|
||||
}
|
||||
printf("\nTest 3: StringWriter::Clear() method\n");
|
||||
|
||||
printf("\nTest 3: String::Clear() method\n");
|
||||
writer.Clear();
|
||||
writer.Append("Hello");
|
||||
VerifyOrQuit(writer.GetLength() == 5, "GetLength() failed for empty string");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "Hello") == 0, "String content is incorrect");
|
||||
PrintString("str", str);
|
||||
|
||||
{
|
||||
StringWriter writer(str);
|
||||
writer.Clear();
|
||||
VerifyOrQuit(writer.GetLength() == 0, "GetLength() failed for empty string");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "") == 0, "String content is incorrect");
|
||||
|
||||
writer.Append("Hello");
|
||||
VerifyOrQuit(writer.GetLength() == 5, "GetLength() failed for empty string");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "Hello") == 0, "String content is incorrect");
|
||||
PrintString("str", str);
|
||||
writer.Append("%d", 12);
|
||||
VerifyOrQuit(writer.GetLength() == 2, "GetLength() failed");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "12") == 0, "String content is incorrect");
|
||||
PrintString("str", str);
|
||||
|
||||
writer.Clear();
|
||||
VerifyOrQuit(writer.GetLength() == 0, "GetLength() failed for empty string");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "") == 0, "String content is incorrect");
|
||||
|
||||
writer.Append("%d", 12);
|
||||
VerifyOrQuit(writer.GetLength() == 2, "GetLength() failed");
|
||||
VerifyOrQuit(strcmp(str.AsCString(), "12") == 0, "String content is incorrect");
|
||||
PrintString("str", str);
|
||||
|
||||
writer.Clear().Append(kLongString);
|
||||
VerifyOrQuit(writer.IsTruncated() && writer.GetLength() == sizeof(kLongString) - 1,
|
||||
"String::Clear() + String::Append() did not handle overflow buffer correctly");
|
||||
PrintString("str", str);
|
||||
}
|
||||
writer.Clear().Append(kLongString);
|
||||
VerifyOrQuit(writer.IsTruncated() && writer.GetLength() == sizeof(kLongString) - 1,
|
||||
"String::Clear() + String::Append() did not handle overflow buffer correctly");
|
||||
PrintString("str", str);
|
||||
|
||||
printf(" -- PASS\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user