Sign in to follow this  

ifstream::open() segfault

This topic is 3372 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my code. it recieves SIGSEGV segmentation fault on the mentioned line
ifstream sessFile;
sessFile.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
sessFileName = sessPath+sessId.toString();
try{
	sessFile.open(sessFileName.c_str());//SIGSEGV
}catch(ifstream::failure e){
	throw(SessionFileNotReadable(sessFileName));
}

Share this post


Link to post
Share on other sites
I've sessFileName.c_str() in debugger's watch
its value shown by the debugger is

"/tmp/cgi++/session/9389040548aac8074b96a3f7471cc5b8"


EDIT:

I've double checked that read write access is given on that file.
however it should through some exception if some error occurs instead of segfaulting.

EDIT:

I've also added a
cout<<"'"<<sessFileName.c_str()<<"'"<<endl;
before passing it sessFile.open().
In debugger the first line (thats with cout) executes correctly but the next line (when open() is called) is causing segfault.

EDIT:

ifstream::open() accepts const char*
and string::c_str() returns const char* too so There cannot be const problem here
though I've tried
sessFile.open(const_cast<char*>(sessFileName.c_str()));

sessFile.open(const_cast<const char*>(sessFileName.c_str()));

non of them solved although its not expected to be solved in this way.

Share this post


Link to post
Share on other sites
Hmm it recieves segmentation fault when the file does not exist.
But shouldn't it throw some exception instead of segfault ??

However if the file exists it throws the following exception

terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear

Share this post


Link to post
Share on other sites
Quote:
Original post by nlbs
Hmm it recieves segmentation fault when the file does not exist.
But shouldn't it throw some exception instead of segfault ??


It's segfault'ing because it is throwing an exception. However, you aren't catching that particular exception that is being throw, so no catch handler means your application will crash.

Add:

catch(std::exception e)
{
std::cout << "Exception: " << e.what() << std::endl;
}
catch(...)
{
std::cout << "Non-processed exception!" << std::endl;
}


handlers and it should not longer segfault and display one of those messages.

Also, is your file name correct? I see no extension, and while it's not mandatory, it's a common mistake.

If all else fails, comment out the line: sessFile.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit); I've never, ever used that before. I do understand it's shown here, but sometimes when trying to do such advanced features, you just complicate simple tasks.


sessFileName = sessPath + sessId.toString();
ifstream sessFile(sessFileName.c_str());
if(!sessFile.is_open())
{
std::cout << "Error opening files!" << std::endl;
exit(0);
}

Share this post


Link to post
Share on other sites
even after adding those two catch blocks it also recieves segfaults IF FILE DOESN'T EXIST

However If I code simply like this


ifstream sessFile;
sessFile.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
sessFileName = sessPath+sessId.toString();
sessFile.open(sessFileName.c_str());
if(sessFile.fail()){
//throw(SessionFileNotReadable(sessFileName));
cout<<"Here";
}



It gets exception on the line where open() is called.

terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear

Share this post


Link to post
Share on other sites
Maybe a silly comment, but make sure you're using fstream instead of fstream.h. fstream.h is deprecated, and there's no guarantee that it will work properly if it hasn't been maintained on your compiler.

The other possibility I would suggest is the string is actually invalid. std::string doesn't support NULL character pointers (nor should it), but if you somehow overwrote the internal pointer with bad data (e.g.: a buffer overflow), it's possible that it is incorrect internally and then when you do c_str() to get the const raw string, you actually have a pointer to garbage, which is why std::ifstream is blowing up. I would check the state of the string before you do the open() call.

Just some thoughts.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rydinare
Maybe a silly comment, but make sure you're using fstream instead of fstream.h. fstream.h is deprecated, and there's no guarantee that it will work properly if it hasn't been maintained on your compiler.

if you are talking about #include <fstream>
Ya I am abviously using #include <fstream>
Quote:
Original post by Rydinare
The other possibility I would suggest is the string is actually invalid. std::string doesn't support NULL character pointers (nor should it), but if you somehow overwrote the internal pointer with bad data (e.g.: a buffer overflow), it's possible that it is incorrect internally and then when you do c_str() to get the const raw string, you actually have a pointer to garbage, which is why std::ifstream is blowing up. I would check the state of the string before you do the open() call.
Its probabbly not a string related problem as it has been double tested as stated in the previous posts

Share this post


Link to post
Share on other sites
I tried to reproduce your problem and I failed. This is the program I used:
#include <fstream>
#include <iostream>

using namespace std;

int main() {
ifstream sessFile;
sessFile.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
try{
sessFile.open("/tmp/cgi++/session/9389040548aac8074b96a3f7471cc5b8");
}catch(ifstream::failure e){
std::cerr << "ifstream::failure trown!\n";
}catch(...) {
std::cerr << "Non-processed exception!\n";
}
}





Can you show us a similarly short self-contained program that shows the problem? Also, what are your platform and compiler (and what version)?

Share this post


Link to post
Share on other sites
Quote:
Original post by nlbs
after I ran that it shown

ifstream::failure trown!

e.g. caught ifstream::failure exception. But donno why its segfaulting in the same case in my programm

You're throwing an exception of another type when you catch the ios_base::failure exception. Do you catch that exception anywhere?

You're also catching by value. Never catch by value. Catch by reference always.

Share this post


Link to post
Share on other sites

This topic is 3372 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this