Skip to content

Commit e2fc4f8

Browse files
committed
Fix a failed assertion when opening a file in write mode.
This was failing when a file without a leading directory is opened.
1 parent 565e9f6 commit e2fc4f8

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

fs.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3462,32 +3462,37 @@ static fs_result fs_file_alloc_and_open_or_info(fs* pFS, const char* pFilePath,
34623462
int dirPathLen;
34633463

34643464
dirPathLen = fs_path_directory(pDirPathStack, sizeof(pDirPathStack), pFilePath, FS_NULL_TERMINATED);
3465-
if (dirPathLen >= (int)sizeof(pDirPathStack)) {
3466-
pDirPathHeap = (char*)fs_malloc(dirPathLen + 1, fs_get_allocation_callbacks(pFS));
3467-
if (pDirPathHeap == NULL) {
3468-
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS));
3469-
fs_file_free(ppFile);
3470-
return FS_OUT_OF_MEMORY;
3465+
if (dirPathLen > 0) {
3466+
if (dirPathLen >= (int)sizeof(pDirPathStack)) {
3467+
pDirPathHeap = (char*)fs_malloc(dirPathLen + 1, fs_get_allocation_callbacks(pFS));
3468+
if (pDirPathHeap == NULL) {
3469+
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS));
3470+
fs_file_free(ppFile);
3471+
return FS_OUT_OF_MEMORY;
3472+
}
3473+
3474+
dirPathLen = fs_path_directory(pDirPathHeap, dirPathLen + 1, pFilePath, FS_NULL_TERMINATED);
3475+
if (dirPathLen < 0) {
3476+
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS));
3477+
fs_file_free(ppFile);
3478+
fs_free(pDirPathHeap, fs_get_allocation_callbacks(pFS));
3479+
return FS_ERROR;
3480+
}
3481+
3482+
pDirPath = pDirPathHeap;
3483+
} else {
3484+
pDirPath = pDirPathStack;
34713485
}
34723486

3473-
dirPathLen = fs_path_directory(pDirPathHeap, dirPathLen + 1, pFilePath, FS_NULL_TERMINATED);
3474-
if (dirPathLen < 0) {
3487+
/* Don't try creating a directory if there is */
3488+
result = fs_mkdir(pFS, pDirPath, FS_IGNORE_MOUNTS);
3489+
if (result != FS_SUCCESS && result != FS_ALREADY_EXISTS) {
34753490
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS));
34763491
fs_file_free(ppFile);
3477-
fs_free(pDirPathHeap, fs_get_allocation_callbacks(pFS));
3478-
return FS_ERROR;
3492+
return result;
34793493
}
3480-
3481-
pDirPath = pDirPathHeap;
34823494
} else {
3483-
pDirPath = pDirPathStack;
3484-
}
3485-
3486-
result = fs_mkdir(pFS, pDirPath, FS_IGNORE_MOUNTS);
3487-
if (result != FS_SUCCESS && result != FS_ALREADY_EXISTS) {
3488-
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS));
3489-
fs_file_free(ppFile);
3490-
return result;
3495+
/* Getting here means the file was specified without a leading path and there is nothing to create. */
34913496
}
34923497
}
34933498

0 commit comments

Comments
 (0)