Below is a small portion of the source code where fclose is causing error? This function is not called always, at some particular condition this function is called.
int write_into_file (char * file_name)
{
FILE * fp = NULL ;
if (file_name == NULL)
{
return FAIL ;
}
if ((fp = fopen (file_name , "r")) == NULL)
{
if ((fp = fopen (file_name, "w")) == NULL)
{
return FAIL ;
}
}
fclose (fp) ;
fp = NULL ;
return SUCESS;
}
We are passing character buffer of size 1024 to file_name.
Please can anyone tell me why fclose causes segmentation fault?
I see no possible way that
fclosecould be causing a segfault here, I think your problem lies somewhere else in the program.It might be possible that the stack got corrupted elsewhere, and that the bug shows when
fclosegets called. I suggest you to review your other source code once more, and step over it with a debugger/memory analyzer closely watching what happens.The only other thing I can see that might cause a bug is
file_namenot being null-terminated.