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

Aschref Ben-Thabet aschref.ben-thabet at embedded-brains.de
Tue Sep 15 09:07:02 UTC 2020


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 problemsi,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 e43ff68d2b..6075bf8bfa 100644
--- a/cpukit/libmisc/shell/main_edit.c
+++ b/cpukit/libmisc/shell/main_edit.c
@@ -286,8 +286,7 @@ 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, FILENAME_MAX);
-
+  if (!realpath(filename, fn))  strlcpy(fn, filename, sizeof(fn));
   do {
     if (strcmp(fn, ed->filename) == 0) return ed;
     ed = ed->next;
@@ -297,7 +296,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;
@@ -1775,8 +1774,9 @@ 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;
   }
 
-- 
2.26.2



More information about the devel mailing list