forked from zephyrproject-rtos/open-amp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: cherry pick strncpy() usage fix
Origin: OpenAMP/open-amp#620 Commits: e233473d14654f08468595ad0dbe8f7e58acf267 8591566382d055acd33f9d23e6826a8a4b0a1881 1aecdc737d463b4ff1ece36847f4f2e68d4ffe4a Status: Cherry pick PR that fixes the `stringop-truncation` compilation errors due to former usage of `strncpy()`. Signed-off-by: Tomi Fontanilles <[email protected]>
- Loading branch information
Showing
6 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* | ||
* Copyright (c) 2024, STMicroelectronics | ||
* | ||
*/ | ||
|
||
#include <string.h> | ||
|
||
/** | ||
* @internal | ||
* | ||
* @brief Copies a string to a destination buffer with size limitation and returns the length of | ||
* the source string. | ||
* | ||
* This function copies up to `size - 1` characters from the source string `src` | ||
* to the destination buffer `dest`, ensuring that the destination buffer is | ||
* null-terminated. The function returns the length of the source string `src`. | ||
* If the length of `src` is greater than or equal to `size`, the destination | ||
* buffer will be truncated. | ||
* | ||
* @param dst Destination buffer where the string will be copied. | ||
* @param src Source string to be copied. | ||
* @param size Size of the destination buffer. | ||
* @return The length of the source string `src`. | ||
* | ||
* @note If the size of the destination buffer is 0, the function does not copy any characters and | ||
* the destination buffer is not null-terminated. | ||
* @note The function ensures that the destination buffer is always null-terminated if `size` is | ||
* greater than 0. | ||
* @note: this code is inspired from the strlcpy.c file from freeBSD . | ||
*/ | ||
size_t strlcpy(char *dest, const char *src, size_t size); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
collect (PROJECT_LIB_SOURCES string.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* | ||
* Copyright (c) 2024, STMicroelectronics | ||
* | ||
*/ | ||
|
||
#include <internal/string.h> | ||
#include <metal/io.h> | ||
|
||
metal_weak size_t strlcpy(char *dst, const char *src, size_t size) | ||
{ | ||
size_t nleft = size; | ||
|
||
/* Copy as many bytes as will fit. */ | ||
if (nleft != 0) { | ||
while (--nleft != 0) { | ||
*dst = *src++; | ||
if (*dst++ == '\0') | ||
break; | ||
} | ||
} | ||
|
||
/* Not enough room in dst, add NUL and traverse rest of src. */ | ||
if (nleft == 0 && size != 0) | ||
*dst = '\0'; /* NUL-terminate dst */ | ||
|
||
return strlen(src); | ||
} |