Unix Programming - Re: Segmentation fault when writing to a file once it has reacher

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2006 > Re: Segmentation fault when writing to a file once it has reacher





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: Segmentation fault when writing to a file once it has reacher
Bjorn Reese

2006-10-19, 1:21 pm

Wes wrote:

> char* ofile = new char[sizeof(file) + 4];

[...]
> for(int i = 0; i < sizeof(file); i++)
> {
> ofile[i] = file[i];
> }
> // then tack on the '.enc'
> ofile[sizeof(file) + 1] = '.';
> ofile[sizeof(file) + 2] = 'e';
> ofile[sizeof(file) + 3] = 'n';
> ofile[sizeof(file) + 4] = 'c';


sizeof(file) will return the size of the file pointer (most likely 4),
not the number of characters in the string that file is pointing to.
You should use strlen(file) instead.

> tbuff = Encrypt(buffer);


This is probably the cause of your crash. You have, most likely, been
fooled by the fact that char pointers work differently than std::string.
If tbuff was a std::string then the above would copy the results of
Encrypt() to the tbuff variable, and all would be fine.

However, the tbuff variable is a char pointer. Initially you allocate 32
bytes for this buffer. However, the above assignment overwrites the
tbuff pointer with whatever pointer is returned by Encrypt(). The result
of Encrypt() is not copied into the initially allocated tbuff. If
Encrypt() returns an allocated buffer then you have a memory leak here,
otherwise you are probably returning garbage that is causing the
subsequent crash. The fact that it happens when you reach 15360 is
accidental.

--
mail1dotstofanetdotdk
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com