Changeset 1dbd1079 in rtems


Ignore:
Timestamp:
Oct 12, 2020, 4:50:24 PM (7 weeks ago)
Author:
Frank Kühndel <frank.kuehndel@…>
Branches:
master
Children:
355bc37a
Parents:
b03c103
git-author:
Frank Kühndel <frank.kuehndel@…> (10/12/20 16:50:24)
git-committer:
Sebastian Huber <sebastian.huber@…> (10/15/20 17:14:52)
Message:

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.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libmisc/shell/main_edit.c

    rb03c103 r1dbd1079  
    284284
    285285static struct editor *find_editor(struct env *env, char *filename) {
    286   char fn[PATH_MAX];
     286  char fnbuf[PATH_MAX];
     287  char *fn = fnbuf;
    287288  struct editor *ed = env->current;
    288289  struct editor *start = ed;
    289290
    290   if (!realpath(filename, fn)) strncpy(fn, filename, FILENAME_MAX);
     291  /* Note: When realpath() == NULL, usually the file doesn't exist */
     292  if (!realpath(filename, fn)) { fn = filename; }
    291293
    292294  do {
     
    299301static int new_file(struct editor *ed, char *filename) {
    300302  if (*filename) {
    301     strncpy(ed->filename, filename, FILENAME_MAX);
     303    strlcpy(ed->filename, filename, sizeof(ed->filename));
    302304  } else {
    303305    sprintf(ed->filename, "Untitled-%d", ++ed->env->untitled);
     
    17771779      }
    17781780    }
    1779     strncpy(
    1780       ed->filename, (const char*) ed->env->linebuf, FILENAME_MAX);
     1781    strlcpy(
     1782      ed->filename, (const char*) ed->env->linebuf, sizeof(ed->filename));
    17811783    ed->newfile = 0;
    17821784  }
Note: See TracChangeset for help on using the changeset viewer.