You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2019/07/29 18:53:57 UTC

[mynewt-core] 02/04: kernel/os: Use strncat rather than strncpy

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit dc2e7ec1ff6ad4c411b0f4f5a0a6b946be81205a
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Tue Jun 4 17:06:25 2019 -0700

    kernel/os: Use strncat rather than strncpy
    
    strncpy() doesn't null terminate the destination buffer if the
    terminator won't fit.  strncat() always terminates the destination
    buffer (truncating the result if necessary).
---
 kernel/os/src/os_mempool.c | 3 ++-
 kernel/os/src/os_task.c    | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/kernel/os/src/os_mempool.c b/kernel/os/src/os_mempool.c
index 70e2945..ce14053 100644
--- a/kernel/os/src/os_mempool.c
+++ b/kernel/os/src/os_mempool.c
@@ -461,7 +461,8 @@ os_mempool_info_get_next(struct os_mempool *mp, struct os_mempool_info *omi)
     omi->omi_num_blocks = cur->mp_num_blocks;
     omi->omi_num_free = cur->mp_num_free;
     omi->omi_min_free = cur->mp_min_free;
-    strncpy(omi->omi_name, cur->name, sizeof(omi->omi_name));
+    omi->omi_name[0] = '\0';
+    strncat(omi->omi_name, cur->name, sizeof(omi->omi_name) - 1);
 
     return (cur);
 }
diff --git a/kernel/os/src/os_task.c b/kernel/os/src/os_task.c
index 6a0bf28..02f64be 100644
--- a/kernel/os/src/os_task.c
+++ b/kernel/os/src/os_task.c
@@ -209,7 +209,8 @@ os_task_info_get_next(const struct os_task *prev, struct os_task_info *oti)
     oti->oti_last_checkin = next->t_sanity_check.sc_checkin_last;
     oti->oti_next_checkin = next->t_sanity_check.sc_checkin_last +
         next->t_sanity_check.sc_checkin_itvl;
-    strncpy(oti->oti_name, next->t_name, sizeof(oti->oti_name));
+    oti->oti_name[0] = '\0';
+    strncat(oti->oti_name, next->t_name, sizeof(oti->oti_name) - 1);
 
     return (next);
 }