forked from Imagelibrary/rtems
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.)
This commit is contained in:
committed by
Sebastian Huber
parent
b03c103dbd
commit
1dbd1079a5
@@ -283,11 +283,13 @@ static void delete_editor(struct editor *ed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct editor *find_editor(struct env *env, char *filename) {
|
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 *ed = env->current;
|
||||||
struct editor *start = ed;
|
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 {
|
do {
|
||||||
if (strcmp(fn, ed->filename) == 0) return ed;
|
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) {
|
static int new_file(struct editor *ed, char *filename) {
|
||||||
if (*filename) {
|
if (*filename) {
|
||||||
strncpy(ed->filename, filename, FILENAME_MAX);
|
strlcpy(ed->filename, filename, sizeof(ed->filename));
|
||||||
} else {
|
} else {
|
||||||
sprintf(ed->filename, "Untitled-%d", ++ed->env->untitled);
|
sprintf(ed->filename, "Untitled-%d", ++ed->env->untitled);
|
||||||
ed->newfile = 1;
|
ed->newfile = 1;
|
||||||
@@ -1776,8 +1778,8 @@ static void save_editor(struct editor *ed) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
strncpy(
|
strlcpy(
|
||||||
ed->filename, (const char*) ed->env->linebuf, FILENAME_MAX);
|
ed->filename, (const char*) ed->env->linebuf, sizeof(ed->filename));
|
||||||
ed->newfile = 0;
|
ed->newfile = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user