Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: invalid chars in file name cause segfault (issue #922) #921

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ int savefile() {
del_range_chars(name, 0, 1 + force_rewrite);

#ifndef NO_WORDEXP
wordexp(name, &p, 0);
if (p.we_wordc < 1) {
sc_error("Failed expanding filepath");
int rc;
if((rc=wordexp(name, &p, 0))==0){;
sc_error("Failed expanding filepath: %s",worderror(rc));
return -1;
}
if ((len = strlen(p.we_wordv[0])) >= sizeof(name)) {
Expand Down Expand Up @@ -2262,6 +2262,8 @@ void load_file(char * file) {
* \brief Attempt to load a tbl into a sheet
* \return none
*/


void load_tbl(char * loading_file) {
char name[PATHLEN];
strcpy(name, ""); //force name to be empty
Expand All @@ -2279,12 +2281,18 @@ void load_tbl(char * loading_file) {
}
memcpy(name, loading_file, len+1);
#else
wordexp(loading_file, &p, 0);
for (c=0; c < p.we_wordc; c++) {
if (c) sprintf(name + strlen(name), " ");
sprintf(name + strlen(name), "%s", p.we_wordv[c]);
int rc;
if((rc = wordexp(loading_file, &p, 0))== 0){
for (c=0; c < p.we_wordc; c++) {
if (c) sprintf(name + strlen(name), " ");
sprintf(name + strlen(name), "%s", p.we_wordv[c]);
}
wordfree(&p);
} else{
sc_error("%s", worderror(rc));
return;
}
wordfree(&p);

#endif

if (strlen(name) != 0) {
Expand All @@ -2300,6 +2308,23 @@ void load_tbl(char * loading_file) {
}
}

static const char *worderror(int errnum) {
switch (errnum)
{
case WRDE_BADCHAR:
return "File name with <newline>, '|', '&', ';', '<', '>', '(', ')', '{', '}' not supported";
case WRDE_BADVAL:
return "Reference to undefined shell variable when WRDE_UNDEF was set in flags to wordexp()";
case WRDE_CMDSUB:
return "Command substitution requested when WRDE_NOCMD was set in flags to wordexp()";
case WRDE_NOSPACE:
return "Attempt to allocate memory in wordexp() failed";
case WRDE_SYNTAX:
return "Shell syntax error, such as unbalanced parentheses or unterminated string";
default:
return "Unknown error from wordexp() function";
}
}

/**
* \brief create an empty workbook and attach it to session
Expand Down
1 change: 1 addition & 0 deletions src/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void remove_backup(char * file);
int backup_exists(char * file);
void openfile_nested(char * file);
void openfile_under_cursor(int r, int c);
static const char *worderror(int errnum);

// load functions
void load_file(char * loading_file);
Expand Down