Commit f7a8db5e authored by Ghislain MARY's avatar Ghislain MARY
Browse files

Do not access the fd field of the bctbx_file_t as it may not be set by some...

Do not access the fd field of the bctbx_file_t as it may not be set by some vfs implementation. Furthermore if the file fails to open bctbx_file_open returns NULL and so accessing the fd field will result in a crash.
parent 7d9a8dfe
...@@ -397,10 +397,6 @@ LpConfig * lp_config_new_from_buffer(const char *buffer){ ...@@ -397,10 +397,6 @@ LpConfig * lp_config_new_from_buffer(const char *buffer){
} }
LpConfig *lp_config_new_with_factory(const char *config_filename, const char *factory_config_filename) { LpConfig *lp_config_new_with_factory(const char *config_filename, const char *factory_config_filename) {
int fd;
bctbx_vfs_file_t* pFile = NULL;
LpConfig *lpconfig=lp_new0(LpConfig,1); LpConfig *lpconfig=lp_new0(LpConfig,1);
lpconfig->g_bctbx_vfs = bctbx_vfs_get_default(); lpconfig->g_bctbx_vfs = bctbx_vfs_get_default();
...@@ -430,23 +426,20 @@ LpConfig *lp_config_new_with_factory(const char *config_filename, const char *fa ...@@ -430,23 +426,20 @@ LpConfig *lp_config_new_with_factory(const char *config_filename, const char *fa
} }
} }
#endif /*_WIN32*/ #endif /*_WIN32*/
/*open with r+ to check if we can write on it later*/
pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,lpconfig->filename, "r+"); /*open with r+ to check if we can write on it later*/
fd = pFile->fd; lpconfig->pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,lpconfig->filename, "r+");
lpconfig->pFile = pFile;
#ifdef RENAME_REQUIRES_NONEXISTENT_NEW_PATH #ifdef RENAME_REQUIRES_NONEXISTENT_NEW_PATH
if (fd == -1){ if (lpconfig->pFile == NULL){
pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,lpconfig->tmpfilename, "r+"); lpconfig->pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,lpconfig->tmpfilename, "r+");
if (fd){ if (lpconfig->pFile == NULL){
ms_warning("Could not open %s but %s works, app may have crashed during last sync.",lpconfig->filename,lpconfig->tmpfilename); ms_warning("Could not open %s but %s works, app may have crashed during last sync.",lpconfig->filename,lpconfig->tmpfilename);
} }
} }
#endif #endif
if (fd != -1){ if (lpconfig->pFile != NULL){
lp_config_parse(lpconfig, pFile); lp_config_parse(lpconfig, lpconfig->pFile);
bctbx_file_close(pFile); bctbx_file_close(lpconfig->pFile);
lpconfig->pFile = NULL; lpconfig->pFile = NULL;
lpconfig->modified=0; lpconfig->modified=0;
} }
...@@ -463,10 +456,8 @@ fail: ...@@ -463,10 +456,8 @@ fail:
int lp_config_read_file(LpConfig *lpconfig, const char *filename){ int lp_config_read_file(LpConfig *lpconfig, const char *filename){
char* path = lp_realpath(filename, NULL); char* path = lp_realpath(filename, NULL);
int fd=-1;
bctbx_vfs_file_t* pFile = bctbx_file_open(lpconfig->g_bctbx_vfs, path, "r"); bctbx_vfs_file_t* pFile = bctbx_file_open(lpconfig->g_bctbx_vfs, path, "r");
fd = pFile->fd; if (pFile != NULL){
if (fd != -1){
ms_message("Reading config information from %s", path); ms_message("Reading config information from %s", path);
lp_config_parse(lpconfig, pFile); lp_config_parse(lpconfig, pFile);
bctbx_file_close(pFile); bctbx_file_close(pFile);
...@@ -927,7 +918,7 @@ static const char *_lp_config_dirname(char *path) { ...@@ -927,7 +918,7 @@ static const char *_lp_config_dirname(char *path) {
} }
bool_t lp_config_relative_file_exists(const LpConfig *lpconfig, const char *filename) { bool_t lp_config_relative_file_exists(const LpConfig *lpconfig, const char *filename) {
bctbx_vfs_file_t *pFile = lpconfig->pFile; bctbx_vfs_file_t *pFile;
if (lpconfig->filename == NULL) { if (lpconfig->filename == NULL) {
return FALSE; return FALSE;
} else { } else {
...@@ -943,10 +934,10 @@ bool_t lp_config_relative_file_exists(const LpConfig *lpconfig, const char *file ...@@ -943,10 +934,10 @@ bool_t lp_config_relative_file_exists(const LpConfig *lpconfig, const char *file
pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath, "r"); pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath, "r");
ms_free(realfilepath); ms_free(realfilepath);
if (pFile->fd != -1) { if (pFile != NULL) {
bctbx_file_close(pFile); bctbx_file_close(pFile);
} }
return pFile->fd > 0; return pFile != NULL;
} }
} }
...@@ -955,9 +946,8 @@ void lp_config_write_relative_file(const LpConfig *lpconfig, const char *filenam ...@@ -955,9 +946,8 @@ void lp_config_write_relative_file(const LpConfig *lpconfig, const char *filenam
const char *dir = NULL; const char *dir = NULL;
char *filepath = NULL; char *filepath = NULL;
char *realfilepath = NULL; char *realfilepath = NULL;
int fd = 0; bctbx_vfs_file_t *pFile;
bctbx_vfs_file_t *pFile = lpconfig->pFile;
if (lpconfig->filename == NULL) return; if (lpconfig->filename == NULL) return;
if(strlen(data) == 0) { if(strlen(data) == 0) {
...@@ -974,10 +964,8 @@ void lp_config_write_relative_file(const LpConfig *lpconfig, const char *filenam ...@@ -974,10 +964,8 @@ void lp_config_write_relative_file(const LpConfig *lpconfig, const char *filenam
goto end; goto end;
} }
pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath, "w"); pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath, "w");
fd = pFile->fd; if(pFile == NULL) {
if(fd == -1) {
ms_error("Could not open %s for write", realfilepath); ms_error("Could not open %s for write", realfilepath);
goto end; goto end;
} }
...@@ -994,7 +982,6 @@ int lp_config_read_relative_file(const LpConfig *lpconfig, const char *filename, ...@@ -994,7 +982,6 @@ int lp_config_read_relative_file(const LpConfig *lpconfig, const char *filename,
char *dup_config_file = NULL; char *dup_config_file = NULL;
const char *dir = NULL; const char *dir = NULL;
char *filepath = NULL; char *filepath = NULL;
int fd = 0;
bctbx_vfs_file_t* pFile = NULL; bctbx_vfs_file_t* pFile = NULL;
char* realfilepath = NULL; char* realfilepath = NULL;
...@@ -1011,15 +998,11 @@ int lp_config_read_relative_file(const LpConfig *lpconfig, const char *filename, ...@@ -1011,15 +998,11 @@ int lp_config_read_relative_file(const LpConfig *lpconfig, const char *filename,
} }
pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath,"r"); pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath,"r");
if (pFile !=NULL) if (pFile == NULL) {
fd = pFile->fd;
if(fd == -1 ) {
ms_error("Could not open %s for read.", realfilepath); ms_error("Could not open %s for read.", realfilepath);
goto err; goto err;
} }
if(bctbx_file_read(pFile, data, 1, (off_t)max_length) < 0){ if(bctbx_file_read(pFile, data, 1, (off_t)max_length) < 0){
ms_error("%s could not be loaded.", realfilepath); ms_error("%s could not be loaded.", realfilepath);
goto err; goto err;
......
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