Skip to content

Commit

Permalink
Merge branch 'pr-921' into dev - Fix: invalid chars in file nae cause…
Browse files Browse the repository at this point in the history
… segfault (issue #922)
  • Loading branch information
andmarti1424 committed Feb 12, 2025
2 parents 60d98ab + 74c8013 commit 100b2cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
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

0 comments on commit 100b2cf

Please sign in to comment.