I have problem running and debugging this piece of code:
bool readSectionHeaders(char* path, int numOfSections, int peSectionsOff, IMAGE_SECTION_HEADER* out) {
bool retr = false; //return value
//open file
FILE* file;
file = fopen (path, "rb");
if(file == NULL) {
perror("WRG"); //TODO
return false;
}
do { //do while(false) only for easier error correction
//seek to first section
fseek(file, peSectionsOff, SEEK_SET);
//read all sections
unsigned int count;
IMAGE_SECTION_HEADER sectionHeaders[numOfSections];
count = fread(sectionHeaders, sizeof(IMAGE_SECTION_HEADER), numOfSections, file);
//check Bytes count
if(count != sizeof(IMAGE_SECTION_HEADER)*numOfSections) {
break;
}
//copy sections
memcpy(out, sectionHeaders, count);
//exit successfully
retr = true;
} while(false);
//exit
fclose(file);
return retr;
}
What is strange is that it returns false even when it reads the file. I tried to debug it and here is the strangest part.
I go line by line until this one
if(file == NULL) {
Then even though file is not NULL it skips perror and moves to
return false;
But does not return at all.
I again go line by line until
retr = true;
where it seems to do something, however retr remains false.
Then it closes file and returns with false.
I have never come across something like this.
I tried cleaning project, rebuilding, even deleting files and redownloading them from subversion. Before using this function, I use similar one – I read PE headers. So I though a problem could be with reading the file but it doesn§t explain debug behavior.
After returning from function, I use perror and it writes No error.
I use mingw with QtCreator.
Thanks in advance.
Ok, this was both my and mingw’s problem. I’ve re-installed whole QtSDK with no effect. Then I installed different version of mingw and set qt creator up to use it. Now debugger works without problem. I’m not sure what happened but cerr << “TEST”; also stopped working with the old mingw and this is 100% correct.
As 111111 suggested, problem was with if break clause. I thought read returns number of bytes read and this was simply not true :).
Now it is working, thanks to 111111 for his suggestion :).