Commit 6e0ed3c1 authored by David Lawrence Ramsey's avatar David Lawrence Ramsey
Browse files

add various portability and potential segfault fixes involving getcwd()

calls


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2428 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
No related merge requests found
Showing with 39 additions and 31 deletions
+39 -31
...@@ -12,8 +12,18 @@ CVS code - ...@@ -12,8 +12,18 @@ CVS code -
cut_to_eol() cut_to_eol()
- Fix inaccurate comment. (DLR) - Fix inaccurate comment. (DLR)
- files.c: - files.c:
get_full_path()
- Rework handling of the results of getcwd() in order to avoid
segfaults if they fail, and to remove uses of the nonportable
GNU extension where passing a size of 0 will get a string as
long as we need. (DLR)
do_browser() do_browser()
- Rename variable lineno to fileline to avoid confusion. (DLR) - Rename variable lineno to fileline to avoid confusion. (DLR)
do_browse_from()
- Rework handling of the results of getcwd() in order to avoid
segfaults if they fail, and to remove uses of the nonportable
GNU extension where passing a size of 0 will get a string as
long as we need. (DLR)
- nano.c: - nano.c:
help_init() help_init()
- When calculating allocsize, take multibyte characters into - When calculating allocsize, take multibyte characters into
...@@ -39,6 +49,11 @@ CVS code - ...@@ -39,6 +49,11 @@ CVS code -
- Instead of breaking a line at a space and readding the space - Instead of breaking a line at a space and readding the space
afterwards, just break the line after the space, as it's more afterwards, just break the line after the space, as it's more
efficient. (DLR) efficient. (DLR)
- nano.h:
- Define PATH_MAX as 4096 if it isn't defined, as passing a size
of 0 to get a string as long as we need is a nonportable GNU
extension, and hence it won't work on non-GNU systems that
don't define PATH_MAX. (DLR)
- utils.c: - utils.c:
regexec_safe() regexec_safe()
- Rename to safe_regexec() for consistency. (DLR) - Rename to safe_regexec() for consistency. (DLR)
......
...@@ -983,15 +983,8 @@ char *get_full_path(const char *origpath) ...@@ -983,15 +983,8 @@ char *get_full_path(const char *origpath)
return NULL; return NULL;
/* Get the current directory. */ /* Get the current directory. */
#if PATH_MAX != -1
d_here = charalloc(PATH_MAX + 1); d_here = charalloc(PATH_MAX + 1);
#else
d_here = NULL;
#endif
d_here = getcwd(d_here, PATH_MAX + 1); d_here = getcwd(d_here, PATH_MAX + 1);
#if PATH_MAX != -1
align(&d_here);
#endif
if (d_here != NULL) { if (d_here != NULL) {
const char *last_slash; const char *last_slash;
...@@ -999,6 +992,8 @@ char *get_full_path(const char *origpath) ...@@ -999,6 +992,8 @@ char *get_full_path(const char *origpath)
bool path_only; bool path_only;
struct stat fileinfo; struct stat fileinfo;
align(&d_here);
/* If the current directory isn't "/", tack a slash onto the end /* If the current directory isn't "/", tack a slash onto the end
* of it. */ * of it. */
if (strcmp(d_here, "/") != 0) { if (strcmp(d_here, "/") != 0) {
...@@ -1055,26 +1050,24 @@ char *get_full_path(const char *origpath) ...@@ -1055,26 +1050,24 @@ char *get_full_path(const char *origpath)
} else { } else {
/* Get the full path and save it in d_there. */ /* Get the full path and save it in d_there. */
free(d_there); free(d_there);
#if PATH_MAX != -1
d_there = charalloc(PATH_MAX + 1); d_there = charalloc(PATH_MAX + 1);
#else
d_there = NULL;
#endif
d_there = getcwd(d_there, PATH_MAX + 1); d_there = getcwd(d_there, PATH_MAX + 1);
#if PATH_MAX != -1
align(&d_there);
#endif
if (d_there == NULL) if (d_there != NULL) {
align(&d_there);
if (strcmp(d_there, "/") != 0) {
/* Make sure d_there ends in a slash. */
d_there = charealloc(d_there,
strlen(d_there) + 2);
strcat(d_there, "/");
}
} else
/* If we couldn't get the full path, set path_only /* If we couldn't get the full path, set path_only
* to TRUE so that we clean up correctly, free all * to TRUE so that we clean up correctly, free all
* allocated memory, and return NULL. */ * allocated memory, and return NULL. */
path_only = TRUE; path_only = TRUE;
else if (strcmp(d_there, "/") != 0) {
/* Make sure d_there ends in a slash. */
d_there = charealloc(d_there, strlen(d_there) + 2);
strcat(d_there, "/");
}
/* Finally, go back to the path specified in d_here, /* Finally, go back to the path specified in d_here,
* where we were before. */ * where we were before. */
...@@ -1090,7 +1083,7 @@ char *get_full_path(const char *origpath) ...@@ -1090,7 +1083,7 @@ char *get_full_path(const char *origpath)
* d_there_file contains the filename portion of the answer. If * d_there_file contains the filename portion of the answer. If
* this is the case, tack d_there_file onto the end of * this is the case, tack d_there_file onto the end of
* d_there, so that d_there contains the complete answer. */ * d_there, so that d_there contains the complete answer. */
if (!path_only && d_there) { if (!path_only && d_there != NULL) {
d_there = charealloc(d_there, strlen(d_there) + d_there = charealloc(d_there, strlen(d_there) +
strlen(d_there_file) + 1); strlen(d_there_file) + 1);
strcat(d_there, d_there_file); strcat(d_there, d_there_file);
...@@ -2764,7 +2757,7 @@ char *do_browse_from(const char *inpath) ...@@ -2764,7 +2757,7 @@ char *do_browse_from(const char *inpath)
struct stat st; struct stat st;
char *path; char *path;
/* This holds the tilde-expanded version of inpath. */ /* This holds the tilde-expanded version of inpath. */
DIR *dir; DIR *dir = NULL;
assert(inpath != NULL); assert(inpath != NULL);
...@@ -2780,15 +2773,12 @@ char *do_browse_from(const char *inpath) ...@@ -2780,15 +2773,12 @@ char *do_browse_from(const char *inpath)
striponedir(path); striponedir(path);
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) { if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
free(path); free(path);
#if PATH_MAX != -1
path = charalloc(PATH_MAX + 1); path = charalloc(PATH_MAX + 1);
#else
path = NULL;
#endif
path = getcwd(path, PATH_MAX + 1); path = getcwd(path, PATH_MAX + 1);
#if PATH_MAX != -1
align(&path); if (path != NULL)
#endif align(&path);
} }
} }
...@@ -2802,7 +2792,9 @@ char *do_browse_from(const char *inpath) ...@@ -2802,7 +2792,9 @@ char *do_browse_from(const char *inpath)
} }
#endif #endif
dir = opendir(path); if (path != NULL)
dir = opendir(path);
if (dir == NULL) { if (dir == NULL) {
beep(); beep();
free(path); free(path);
......
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
/* Set a default value for PATH_MAX, so we can use it in lines like /* Set a default value for PATH_MAX, so we can use it in lines like
* "path = getcwd(NULL, PATH_MAX + 1);". */ * "path = getcwd(NULL, PATH_MAX + 1);". */
#ifndef PATH_MAX #ifndef PATH_MAX
#define PATH_MAX -1 #define PATH_MAX 4096
#endif #endif
#ifdef USE_SLANG #ifdef USE_SLANG
......
...@@ -110,6 +110,7 @@ bool parse_num(const char *str, ssize_t *val) ...@@ -110,6 +110,7 @@ bool parse_num(const char *str, ssize_t *val)
void align(char **strp) void align(char **strp)
{ {
assert(strp != NULL); assert(strp != NULL);
if (*strp != NULL) if (*strp != NULL)
*strp = charealloc(*strp, strlen(*strp) + 1); *strp = charealloc(*strp, strlen(*strp) + 1);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment