Skip to content

Commit c9291e3

Browse files
address issues related to cf.openFile() error handling (#5030)
- fix crash bug when path only contains dir separators - fix error handling when no path is specified in write mode - clarify function documentation Fixes #5028
1 parent 00f0cc4 commit c9291e3

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

code/cfile/cfile.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,9 @@ int cfile_get_path_type(const SCP_string& dir)
18471847
auto start = buf.find_first_not_of("\\/");
18481848
auto end = buf.find_last_not_of("\\/");
18491849

1850-
if ( (start > 0) || (end < buf.length()-1) ) {
1850+
if (start == SCP_string::npos) {
1851+
buf = "";
1852+
} else if ( (start > 0) || (end < buf.length()-1) ) {
18511853
buf = buf.substr(start, end-start+1);
18521854
}
18531855
}

code/scripting/api/libs/cfile.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ ADE_FUNC(listFiles, l_CFile, "string directory, string filter",
106106
}
107107

108108
ADE_FUNC(openFile, l_CFile, "string Filename, [string Mode=\"r\", string Path = \"\"]",
109-
"Opens a file. 'Mode' uses standard C fopen arguments. Use a blank string for path for any directory, or a slash for the root directory."
110-
"Be EXTREMELY CAREFUL when using this function, as you may PERMANENTLY delete any file by accident",
109+
"Opens a file. 'Mode' uses standard C fopen arguments. In read mode use a blank string for path for any directory, "
110+
"or a slash for the root directory. When using write mode a valid path must be specified. "
111+
"Be EXTREMELY CAREFUL when using this function, as you may PERMANENTLY delete any file by accident",
111112
"file",
112113
"File handle, or invalid file handle if the specified file couldn't be opened")
113114
{
@@ -126,6 +127,9 @@ ADE_FUNC(openFile, l_CFile, "string Filename, [string Mode=\"r\", string Path =
126127
if(path == CF_TYPE_INVALID)
127128
return ade_set_error(L, "o", l_File.Set(cfile_h()));
128129

130+
if((path == CF_TYPE_ANY) && (strchr(n_mode,'w') || strchr(n_mode,'+') || strchr(n_mode,'a')))
131+
return ade_set_error(L, "o", l_File.Set(cfile_h()));
132+
129133
CFILE *cfp = cfopen(n_filename, n_mode, type, path);
130134

131135
if(!cf_is_valid(cfp))

0 commit comments

Comments
 (0)