Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement internal strlcpy to replace the strncpy #620

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ collect (PROJECT_LIB_SOURCES version.c)
add_subdirectory (virtio)
add_subdirectory (rpmsg)
add_subdirectory (remoteproc)
add_subdirectory (utils)
if (WITH_VIRTIO_MMIO_DRV)
add_subdirectory (virtio_mmio)
endif (WITH_VIRTIO_MMIO_DRV)
Expand All @@ -23,6 +24,7 @@ set (OPENAMP_LIB open_amp)

configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/openamp/version_def.h)
collect (PROJECT_INC_DIRS " ${PROJECT_BINARY_DIR}/include/generated/openamp")
collect (PROJECT_INC_DIRS " ${PROJECT_BINARY_DIR}/include/internal")

if (NOT CMAKE_INSTALL_LIBDIR)
set (CMAKE_INSTALL_LIBDIR "lib")
Expand Down
33 changes: 33 additions & 0 deletions lib/include/internal/string.h
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);
3 changes: 2 additions & 1 deletion lib/remoteproc/remoteproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <internal/string.h>
#include <metal/alloc.h>
#include <metal/log.h>
#include <metal/utilities.h>
Expand Down Expand Up @@ -306,7 +307,7 @@ void remoteproc_init_mem(struct remoteproc_mem *mem, const char *name,
if (!mem || !io || size == 0)
return;
if (name)
strncpy(mem->name, name, sizeof(mem->name));
(void)strlcpy(mem->name, name, sizeof(mem->name));
else
mem->name[0] = 0;
mem->pa = pa;
Expand Down
5 changes: 3 additions & 2 deletions lib/rpmsg/rpmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <internal/string.h>
#include <openamp/rpmsg.h>
#include <metal/alloc.h>

Expand Down Expand Up @@ -141,7 +142,7 @@ int rpmsg_send_ns_message(struct rpmsg_endpoint *ept, unsigned long flags)

ns_msg.flags = flags;
ns_msg.addr = ept->addr;
strncpy(ns_msg.name, ept->name, sizeof(ns_msg.name));
(void)strlcpy(ns_msg.name, ept->name, sizeof(ns_msg.name));
ret = rpmsg_send_offchannel_raw(ept, ept->addr,
RPMSG_NS_EPT_ADDR,
&ns_msg, sizeof(ns_msg), true);
Expand Down Expand Up @@ -305,7 +306,7 @@ void rpmsg_register_endpoint(struct rpmsg_device *rdev,
rpmsg_ept_cb cb,
rpmsg_ns_unbind_cb ns_unbind_cb, void *priv)
{
strncpy(ept->name, name ? name : "", sizeof(ept->name));
(void)strlcpy(ept->name, name ? name : "", sizeof(ept->name));
ept->refcnt = 1;
ept->addr = src;
ept->dest_addr = dest;
Expand Down
1 change: 1 addition & 0 deletions lib/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
collect (PROJECT_LIB_SOURCES string.c)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

many RTOS support strlcpy need some method to remove string.c or strlcpy from build.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you see a potential failure in not using this RTOS strlcpy implementation?

perhaps defining is a weak function would answer to your concern?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you see a potential failure in not using this RTOS strlcpy implementation?

the link will report the multiple instance error if RTOS provide one implementation.

perhaps defining is a weak function would answer to your concern?

Yes, it's better.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weak attribute added

29 changes: 29 additions & 0 deletions lib/utils/string.c
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);
}
Loading