You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/12/29 17:13:30 UTC

[incubator-nuttx] branch master updated: Ensure that sethostname null terminates the hostname correctly

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

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new df956b0  Ensure that sethostname null terminates the hostname correctly
df956b0 is described below

commit df956b08f259b12739c7cdbd42f8e37d7e457e2b
Author: Norman Rasmussen <no...@rasmussen.co.za>
AuthorDate: Wed Dec 29 03:22:42 2021 -0800

    Ensure that sethostname null terminates the hostname correctly
    
    commit e1c306f2dd3b8f4f304a77a908e965c05fb3eab2 added sethostname using
    strncpy. This replaces it with strlcpy and uses sizeof() instead of
    re-calculating the buffer size.
    
    Rename size argument for get/sethostname to match the implementation
---
 include/unistd.h                   | 4 ++--
 libs/libc/unistd/lib_sethostname.c | 5 ++---
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/include/unistd.h b/include/unistd.h
index 02e62d2..d158955 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -386,8 +386,8 @@ FAR int   *getopterrp(void);  /* Print error message */
 FAR int   *getoptindp(void);  /* Index into argv */
 FAR int   *getoptoptp(void);  /* Unrecognized option character */
 
-int     gethostname(FAR char *name, size_t size);
-int     sethostname(FAR const char *name, size_t size);
+int     gethostname(FAR char *name, size_t namelen);
+int     sethostname(FAR const char *name, size_t namelen);
 
 /* Get configurable system variables */
 
diff --git a/libs/libc/unistd/lib_sethostname.c b/libs/libc/unistd/lib_sethostname.c
index 922ff48..8e9c850 100644
--- a/libs/libc/unistd/lib_sethostname.c
+++ b/libs/libc/unistd/lib_sethostname.c
@@ -105,7 +105,7 @@ extern char g_hostname[HOST_NAME_MAX + 1];
  *
  ****************************************************************************/
 
-int sethostname(FAR const char *name, size_t size)
+int sethostname(FAR const char *name, size_t namelen)
 {
   irqstate_t flags;
 
@@ -116,8 +116,7 @@ int sethostname(FAR const char *name, size_t size)
    */
 
   flags = enter_critical_section();
-  strncpy(g_hostname, name, MIN(HOST_NAME_MAX, size));
-  g_hostname[HOST_NAME_MAX] = '\0';
+  strlcpy(g_hostname, name, sizeof(g_hostname));
   leave_critical_section(flags);
 
   return 0;