[PATCH] cpukit/libmisc/shell: Fix string turncation warning

Gedare Bloom gedare at rtems.org
Fri Sep 4 14:18:26 UTC 2020


two things: first, typo in your commit messages: "truncation"

On Fri, Sep 4, 2020 at 3:34 AM Aschref Ben-Thabet
<aschref.ben-thabet at embedded-brains.de> wrote:
>
> From: Aschref Ben Thabet <aschref.ben-thabet at embedded-brains.de>
>
> strncpy is considered unsafe as the resulting string may not be NULL
> terminated. In addition it fills the unused part of the destination
> buffer unnecessarily with NULL bytes.
>
> strlcpy is designed to solve the null-termination problems – it always
> null-terminates. It’s certainly an improvement over strncpy.
> ---
>  cpukit/libmisc/shell/main_edit.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/cpukit/libmisc/shell/main_edit.c b/cpukit/libmisc/shell/main_edit.c
> index feefd6bff1..ab2b9eacbf 100644
> --- a/cpukit/libmisc/shell/main_edit.c
> +++ b/cpukit/libmisc/shell/main_edit.c
> @@ -286,8 +286,8 @@ static struct editor *find_editor(struct env *env, char *filename) {
>    struct editor *ed = env->current;
>    struct editor *start = ed;
>
> -  if (!realpath(filename, fn)) strncpy(fn, filename, sizeof(fn)-1);
> -
> +  if (!realpath(filename, fn))  strlcpy(fn, filename, sizeof(fn));
> +
>    do {
>      if (strcmp(fn, ed->filename) == 0) return ed;
>      ed = ed->next;
> @@ -297,7 +297,7 @@ static struct editor *find_editor(struct env *env, char *filename) {
>
>  static int new_file(struct editor *ed, char *filename) {
>    if (*filename) {
> -      strncpy(ed->filename, filename, sizeof(ed->filename)-1);
> +      strlcpy(ed->filename, filename, sizeof(ed->filename));
>    }
>    else {
>      sprintf(ed->filename, "Untitled-%d", ++ed->env->untitled);
> @@ -1776,8 +1776,8 @@ static void save_editor(struct editor *ed) {
>          return;
>        }
>      }
> -    strncpy(
> -        ed->filename, (const char*)ed->env->linebuf, sizeof(ed->filename)-1);
> +    strlcpy(
> +        ed->filename, (const char*)ed->env->linebuf, sizeof(ed->filename));
This can probably fit on one line now.

>      ed->newfile = 0;
>    }
>
> --
> 2.26.2
>
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel


More information about the devel mailing list