diff options
author | Frank Kühndel <frank.kuehndel@embedded-brains.de> | 2020-10-12 18:50:24 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2020-10-15 19:14:52 +0200 |
commit | 1dbd1079a5344c3c67d6bf75b04319725b02ce62 (patch) | |
tree | dba41cc66a62ce18925f5ff75c64c88aaa4fb999 /cpukit | |
parent | shell/main_edit.c: Fix use of wrong constant (diff) | |
download | rtems-1dbd1079a5344c3c67d6bf75b04319725b02ce62.tar.bz2 |
shell/main_edit.c: Fix string truncation warning
Using strlcpy() instead of strncpy():
1) Prevents the compiler warnings
2) Ensures, the string is NUL terminated.
3) Avoids that strncpy() unnecessary fills the unused part of the buffer with
0 bytes.
(Note that realpath() also returns NULL if the file does not exist - that
happens always if someone creates a new file with the editor of the shell.)
Diffstat (limited to 'cpukit')
-rw-r--r-- | cpukit/libmisc/shell/main_edit.c | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/cpukit/libmisc/shell/main_edit.c b/cpukit/libmisc/shell/main_edit.c index 02214bd942..a0110495e8 100644 --- a/cpukit/libmisc/shell/main_edit.c +++ b/cpukit/libmisc/shell/main_edit.c @@ -283,11 +283,13 @@ static void delete_editor(struct editor *ed) { } static struct editor *find_editor(struct env *env, char *filename) { - char fn[PATH_MAX]; + char fnbuf[PATH_MAX]; + char *fn = fnbuf; struct editor *ed = env->current; struct editor *start = ed; - if (!realpath(filename, fn)) strncpy(fn, filename, FILENAME_MAX); + /* Note: When realpath() == NULL, usually the file doesn't exist */ + if (!realpath(filename, fn)) { fn = filename; } do { if (strcmp(fn, ed->filename) == 0) return ed; @@ -298,7 +300,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, FILENAME_MAX); + strlcpy(ed->filename, filename, sizeof(ed->filename)); } else { sprintf(ed->filename, "Untitled-%d", ++ed->env->untitled); ed->newfile = 1; @@ -1776,8 +1778,8 @@ static void save_editor(struct editor *ed) { return; } } - strncpy( - ed->filename, (const char*) ed->env->linebuf, FILENAME_MAX); + strlcpy( + ed->filename, (const char*) ed->env->linebuf, sizeof(ed->filename)); ed->newfile = 0; } |