Skip to content
Snippets Groups Projects
Commit d49134e6 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

strListReadFile() now returns NULL on any file read failure

Previously, the partially-read lines would be returned in an allocated
string list. This is a suspected cause of issue #791: even though the file was
successfully opened exclusively (using sopen... SH_DENYRW), it's possible that
Samba had already allowed another client or local process to open the same
file, but is now a denying read (most likely, the first read). As Deuce
pointed out, the xpdev *nix implementation of sopen() locks the region/record
of the entire file. So before this change, strListReadFile() might fail on
the first read, and a function that uses iniFileRead() to modify the contents
of an ini file, might end up writing back the empty list (with added keys),
thus deleting all the existing content of the file.

Also in this change:
- Eliminated the unnecessary local/wrapped str_list_read_file() function.
- Elminate unecessary null-before-free check
- Fixed potential memory leak upon malloc failure (the potentially-allocated
  list wasn't freed).
parent 357fd0cb
No related branches found
No related tags found
No related merge requests found
......@@ -627,11 +627,16 @@ void strListFree(str_list_t* list)
}
}
static str_list_t str_list_read_file(FILE* fp, str_list_t* lp, size_t max_line_len)
// Read from the current file position (in stream 'fp') to the end of file
// adding each read line (up to max_line_len chars) to the specified string
// list (or allocating a new one, when 'lp' is NULL).
// Returns NULL of error (i.e. memory allocation or file read failure).
// Upon error, the allocated list will be freed, but only if 'lp' was NULL.
str_list_t strListReadFile(FILE* fp, str_list_t* lp, size_t max_line_len)
{
char* buf=NULL;
size_t count;
str_list_t list;
str_list_t list = NULL;
if(max_line_len<1)
max_line_len=2048;
......@@ -646,15 +651,20 @@ static str_list_t str_list_read_file(FILE* fp, str_list_t* lp, size_t max_line_l
count=strListCount(*lp);
while(!feof(fp)) {
if(buf==NULL && (buf=(char*)malloc(max_line_len+1))==NULL)
return(NULL);
break;
if(fgets(buf,max_line_len+1,fp)==NULL)
break;
strListAppend(lp, buf, count++);
}
if(!feof(fp)) {
strListFreeStrings(list);
free(list);
free(buf);
return NULL;
}
}
if(buf)
free(buf);
free(buf);
return(*lp);
}
......@@ -664,7 +674,7 @@ size_t strListInsertFile(FILE* fp, str_list_t* lp, size_t index, size_t max_line
str_list_t list;
size_t count;
if((list=str_list_read_file(fp, NULL, max_line_len)) == NULL)
if((list = strListReadFile(fp, NULL, max_line_len)) == NULL)
return(0);
count = strListInsertList(lp, list, index);
......@@ -674,11 +684,6 @@ size_t strListInsertFile(FILE* fp, str_list_t* lp, size_t index, size_t max_line
return(count);
}
str_list_t strListReadFile(FILE* fp, str_list_t* lp, size_t max_line_len)
{
return str_list_read_file(fp,lp,max_line_len);
}
size_t strListWriteFile(FILE* fp, const str_list_t list, const char* separator)
{
size_t i;
......@@ -801,8 +806,7 @@ char* strListCreateBlock(str_list_t list)
void strListFreeBlock(char* block)
{
if(block!=NULL)
free(block); /* this must be done here for Windows-DLL reasons */
free(block); /* this must be done here for Windows-DLL reasons */
}
int strListTruncateTrailingWhitespaces(str_list_t list)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment