ifstream::open() segfault

Started by
10 comments, last by Bregma 15 years, 7 months ago
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));
}

Advertisement
Try printing the file name before hand:

cout << "'" << sessFileName.c_str() << "'" << endl;

My guess is that the c_str( ) member function is actually the one to blame. Using a debugger will also help find the cause.
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.
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
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);}
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
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.
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

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)?
I'll give that small test a try.
I am running g++ on debian

This topic is closed to new replies.

Advertisement