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

Aschref Ben-Thabet aschref.ben-thabet at embedded-brains.de
Fri Sep 4 09:34:23 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 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));
     ed->newfile = 0;
   }
 
-- 
2.26.2



More information about the devel mailing list