[string] return 0 if nullptr is passed to StringLength (#9886)

This commit ensures that segfault won't happen if
`nullptr` is passed  to `StringLength()`.

Signed-off-by: Maciej Baczmanski <[email protected]>
This commit is contained in:
Maciej Baczmański
2024-03-04 10:21:29 -08:00
committed by GitHub
parent 27eb8ed09f
commit 08a7600e4a
2 changed files with 7 additions and 4 deletions
+5 -2
View File
@@ -89,13 +89,16 @@ exit:
uint16_t StringLength(const char *aString, uint16_t aMaxLength)
{
uint16_t ret;
uint16_t ret = 0;
for (ret = 0; (ret < aMaxLength) && (aString[ret] != kNullChar); ret++)
VerifyOrExit(aString != nullptr);
for (; (ret < aMaxLength) && (aString[ret] != kNullChar); ret++)
{
// Empty loop.
}
exit:
return ret;
}
+2 -2
View File
@@ -85,8 +85,8 @@ static constexpr char kNullChar = '\0'; ///< null character.
* @param[in] aString A pointer to the string.
* @param[in] aMaxLength The maximum length in bytes.
*
* @returns The number of characters that precede the terminating null character or @p aMaxLength, whichever is
* smaller.
* @returns The number of characters that precede the terminating null character or @p aMaxLength,
* whichever is smaller. `0` if @p aString is `nullptr`.
*
*/
uint16_t StringLength(const char *aString, uint16_t aMaxLength);