Fgets() crashs after a number of executions?

Up vote 0 down vote favorite share g+ share fb share tw.

I'm coding a program to crack the CRC16. I've been having some problems with outputting the file and keep the calculated CRC16(have no idea why it changes when I write it to a file). So what I'm doing here is read the input file, writing it to a output file with some gibberish and then I read the output file again and calculate it's CRC16.

If it matches with the desired CRC16, then it is done. However after a bunch of executions the fgets() method crashes with a Seg fault. Anyone could help me?

Please ignore the performance issues, this is a test. Int main(int argc, char* argv){ char outfilestrlen(argv1); strcpy(outfile,argv1); strcat(outfile,". Crack"); char crc165; strcpy(crc16,argv2); char newcrc165; char gebrish80; char cat2; int full = 1; int p = 0; int i,j,k; for(i=32; I = NULL){ fputs(line,out); } fputs(gebrish,out); fclose(file); fclose(out); printf("read gain\n"); out = fopen(outfile,"r"); while(fgets(line,80,out)!

= NULL){ result. Process_bytes(line,strlen(line)); printf("%s",line); } int crc = result.checksum(); sprintf(newcrc16,"%x",crc); printf("%s",newcrc16); if(strcmp(crc16,newcrc16) == 0){ printf("%s",gebrish); return 0; } } return 0; } c fgets link|improve this question asked Apr 5 at 19:37Leandro Machado1.

– stark Apr 5 at 21:49 Welcome to StackOverflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the FAQ and especially How do I ask questions here?

– Jonathan Leffler 2 days ago.

This causes a buffer overrun: char outfilestrlen(argv1); strcpy(outfile,argv1); strcat(outfile,". Crack"); as there is not enough space in outfile for terminating null character and ". Crack".

It will be overwriting memory it is not supposed to and may be the cause of the segmentation fault. Change to: char outfilestrlen(argv1) + 1 + 6; strcpy(outfile,argv1); strcat(outfile,". Crack"); Before accessing argv elements ensure they have been provided by checking argc: if (argc > 2) { /* Safe to use argv1 and argv2.

*/ } Check return values from fopen() also.

Thanks for the note on the buffer size. However the error is not there, I fixed the code(I guess the compiler was fixing it before) but it gives the same error and crashes in the same time. – Leandro Machado Apr 5 at 20:57.

The error is most likely due to not checking the return value from open, and then calling fgets on a bad file. Returns from system calls should always be checked if subsequent operations depend on them. Even close can fail.

I see what you mean, but I'm testing it in a really controlled environment. The files will always be there. Anyway, it crashes after a bunch of execution of the loop, not in the first moment.

Also, I need to know why it crashes, not how to avoid the crash. Thanks! – Leandro Machado Apr 5 at 21:02.

The problem is that I tried to Read and Write from the same file in different moments without calling fclose() after the use. This way after some execution of the loop it crashes with a STATUS_VIOLATION. I have no idea why it didn't crash right away, but all I did was add a flcose() after reading the file for the CRC16 calculation.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions