Segmentation fault in accessing an unsigned long int
I am getting an "segmentation fault" inside the function "include_file", when I execute the line "digest.size = 0; " . what could be the problem ? If I comment the above line, there is no problem.
Error:
/bin/sh: line 1: 27058 Segmentation fault
the below is the code:
class ee_sha1class
{
private:
unsigned char *m_text;
SHA_CTX SHA1_state;
struct digest_info
{
const char *filename;
char *text;
unsigned char *mdigest;
unsigned long int size;
};
struct digest_info digest;
public:
int calculate();
int printdigest();
int input_file(const char *filename);
ee_sha1class();
};
ee_sha1class :: ee_sha1class()
{
digest.filename = (const char *) malloc(sizeof(char) * 10240);
digest.mdigest = (unsigned char *) malloc(sizeof(char) * (16*2 +1));
bzero((void*)digest.filename, 10240);
bzero(digest.mdigest, 20);
digest.size = 0;
}
int ee_sha1class :: calculate()
{
SHA1((unsigned char*)m_text, file_size, digest.mdigest);
}
int ee_sha1class :: input_file(const char *filename)
{
int fp, result = 512;
char *temp_char;
temp_char = (char*)malloc(sizeof(char) * 512);
digest.size = 0; // "Segmentation fault" error at this point
// calculate size
fp = open(filename, O_RDONLY);
printf("open: %d \n", fp);
while(result == 512)
{
result = read(fp, temp_char, 512);
*(temp_char + result) = '\0';
}
close(fp);
}
|