mirror of
https://github.com/ARM-software/arm-trusted-firmware.git
synced 2025-04-16 09:34:18 +00:00

Implement safer version of 'strnlen' function to handle NULL terminated strings with additional bound checking and secure version of string copy function to support better security and avoid destination buffer overflow. Change-Id: I93916f003b192c1c6da6a4f78a627c8885db11d9 Signed-off-by: Jit Loon Lim <jit.loon.lim@altera.com> Signed-off-by: Girisha Dengi <girisha.dengi@intel.com>
22 lines
328 B
C
22 lines
328 B
C
/*
|
|
* Copyright (c) 2024-2025, Altera Corporation. All rights reserved.
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
size_t strnlen_secure(const char *str, size_t maxlen)
|
|
{
|
|
size_t len = 0;
|
|
|
|
if (str == NULL) {
|
|
return 0;
|
|
}
|
|
|
|
while ((len < maxlen) && (str[len] != '\0')) {
|
|
len++;
|
|
}
|
|
|
|
return len;
|
|
}
|