W
Wes
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++.
The idea of the program is to open one file as input, read bytes from
it, do some bitwise operations on the bytes, and then write them to
this second file. However, when the second file is 15360 bytes long,
the program dies with a "Segmentation Fault (core dumped)" error!
I checked with gdb, and it says the last function to run was memcpy()
from libc, which would explain the segmentation fault I think (the
function was passed an invalid pointer maybe?).
So how do I fix it? Below is the function's code:
### BEGIN CODE ###
int encFile(char* file)
{
ifstream:
os_type size;
char* buffer = new char[32];
char* ofile = new char[sizeof(file) + 4];
char* tbuff = new char[32];
ifstream plain(file, ios::in|ios::binary|ios::ate);
if(!plain.is_open())
{
cout << "Unable to open file '" << file
<< "'. Closing." << endl;
return 0;
}
size = plain.tellg();
plain.seekg(0, ios::beg);
for(int i = 0; i < sizeof(file); i++)
{
ofile = file;
}
ofile[sizeof(file) + 1] = '.';
ofile[sizeof(file) + 2] = 'e';
ofile[sizeof(file) + 3] = 'n';
ofile[sizeof(file) + 4] = 'c';
ofstream enc(ofile, ios:
ut|ios::binary);
while(size - plain.tellg() > 32)
{
plain.read (buffer, 32);
tbuff = Encrypt(buffer);
enc.write(tbuff, 32); // this is where we die at 15360
}
size = size - plain.tellg();
plain.read(buffer, size);
enc.write(Encrypt(buffer), size);
plain.close();
enc.close();
delete buffer;
delete ofile;
return 0;
}
### END CODE ###
Any thoughts?
The idea of the program is to open one file as input, read bytes from
it, do some bitwise operations on the bytes, and then write them to
this second file. However, when the second file is 15360 bytes long,
the program dies with a "Segmentation Fault (core dumped)" error!
I checked with gdb, and it says the last function to run was memcpy()
from libc, which would explain the segmentation fault I think (the
function was passed an invalid pointer maybe?).
So how do I fix it? Below is the function's code:
### BEGIN CODE ###
int encFile(char* file)
{
ifstream:
char* buffer = new char[32];
char* ofile = new char[sizeof(file) + 4];
char* tbuff = new char[32];
ifstream plain(file, ios::in|ios::binary|ios::ate);
if(!plain.is_open())
{
cout << "Unable to open file '" << file
<< "'. Closing." << endl;
return 0;
}
size = plain.tellg();
plain.seekg(0, ios::beg);
for(int i = 0; i < sizeof(file); i++)
{
ofile = file;
}
ofile[sizeof(file) + 1] = '.';
ofile[sizeof(file) + 2] = 'e';
ofile[sizeof(file) + 3] = 'n';
ofile[sizeof(file) + 4] = 'c';
ofstream enc(ofile, ios:
while(size - plain.tellg() > 32)
{
plain.read (buffer, 32);
tbuff = Encrypt(buffer);
enc.write(tbuff, 32); // this is where we die at 15360
}
size = size - plain.tellg();
plain.read(buffer, size);
enc.write(Encrypt(buffer), size);
plain.close();
enc.close();
delete buffer;
delete ofile;
return 0;
}
### END CODE ###
Any thoughts?