[network-name] implement additional validation rules (#6988)

According to SPEC-981, we need to verify 3 more things while setting a
network name:
- The name length >= 1.
- There are no UTF control characters in the name.
- There are no UTF NUL characters in the name. This is covered by the
  second rule.
This commit is contained in:
whd
2021-09-09 15:41:06 -07:00
committed by GitHub
parent 7563ee8d1d
commit e2f0afab5d
5 changed files with 8 additions and 1 deletions
+2
View File
@@ -172,6 +172,8 @@ bool IsValidUtf8String(const char *aString, size_t aLength)
if ((byte & 0x80) == 0)
{
// We don't allow control characters.
VerifyOrExit(!iscntrl(byte), ret = false);
continue;
}
+2
View File
@@ -237,6 +237,7 @@ private:
/**
* This function validates whether a given byte sequence (string) follows UTF-8 encoding.
* Control characters are not allowed.
*
* @param[in] aString A null-terminated byte sequence.
*
@@ -248,6 +249,7 @@ bool IsValidUtf8String(const char *aString);
/**
* This function validates whether a given byte sequence (string) follows UTF-8 encoding.
* Control characters are not allowed.
*
* @param[in] aString A byte sequence.
* @param[in] aLength Length of the sequence.
+1
View File
@@ -155,6 +155,7 @@ Error NetworkName::Set(const char *aNameString)
Error error;
NameData data(aNameString, kMaxSize + 1);
VerifyOrExit(data.GetLength() >= 1, error = kErrorInvalidArgs);
VerifyOrExit(IsValidUtf8String(aNameString), error = kErrorInvalidArgs);
error = Set(data);
+1 -1
View File
@@ -137,7 +137,7 @@ void NetworkNameTlv::SetNetworkName(const Mac::NameData &aNameData)
bool NetworkNameTlv::IsValid(void) const
{
return IsValidUtf8String(mNetworkName, GetLength());
return GetLength() >= 1 && IsValidUtf8String(mNetworkName, GetLength());
}
void SteeringDataTlv::CopyTo(SteeringData &aSteeringData) const
+2
View File
@@ -138,6 +138,8 @@ void TestUtf8(void)
VerifyOrQuit(!IsValidUtf8String("\xef\x80"));
VerifyOrQuit(!IsValidUtf8String("\xf7\x80\x80"));
VerifyOrQuit(!IsValidUtf8String("\xff"));
VerifyOrQuit(!IsValidUtf8String("NUL\x00NUL", 7)); // UTF-8 NUL
VerifyOrQuit(!IsValidUtf8String("abcde\x11")); // control character
printf(" -- PASS\n");
}