Ensure that sethostname null terminates the hostname correctly

commit e1c306f2dd 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
This commit is contained in:
Norman Rasmussen 2021-12-29 03:22:42 -08:00 committed by Xiang Xiao
parent fc41bb7f8a
commit df956b08f2
2 changed files with 4 additions and 5 deletions

View file

@ -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 */

View file

@ -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;