Function return error

Hello,

I've ran into a problem with my code. You see for some reason or another, when my function finally gets done executing everything I told it to, it never returns to the correct address that it was called from. Here a code snippet to demonstrate.

For demonstration purpose, I'll be making up some random addresses you would find while debugging.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int main(){
...
FILE *myFile = NULL;
myFile = fopen(szName, "rb");

if(myFile == NULL)
    return 0;

0x004110 =
if(CheckHeader(myFile, 1) != 1) //Function never returns, 
    return 0;                   //even with perfect execution

0x004140 ... //ect


BOOL CheckHeader(FILE *hFile, int check){
...
	if(check == 1){  //PACK header checking
		printf("Checking 'PACK' Header\n\n");

		fread(Pakheader, sizeof(HeaderSize), 4, hFile);
		printf("Buffer Readout %s Hex = %x\n", Pakheader, Pakheader);
		for(i = 0; i < HeaderSize; i++)
			if(Pakheader[i] != PACK_HEADER[i])
				return FALSE;  //ident corrupt

		printf("|P = TRUE\n");
		printf("|A = TRUE\n");
		printf("|C = TRUE\n");
		printf("|K = TRUE\n");
		printf("Verified!\n");
	}

... 
return TRUE; // error instead of returning back to the if statement. 
             //To see if this function was true or not. 
             //The return is set at something like 0x0000173 instead of
             //0x004110 or something like that. 


That's pretty much a vague explanation I could come up with. If you have any idea's please help me.

Thanks,
-Hex2Text
Last edited on
Sounds like you corrupted the stack.
Any ideas how I could fix this?
We can't debug vague things. Please post exact code.
What's the definition of Pakheader?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#define HeaderSize 4
BYTE PACK_HEADER[HeaderSize] = {'P', 'A', 'C', 'K'};
BYTE LZO1_HEADER[HeaderSize] = {'L', 'Z', 'O', '1'};

BOOL CheckHeader(FILE *hFile, int *check){
	int i;
	BYTE Pakheader[HeaderSize];
	BYTE Lzoheader[HeaderSize];

	if(check == 1){  //PACK header checking
		printf("Checking 'PACK' Header\n\n");

		fread(Pakheader, sizeof(HeaderSize), 4, hFile);
		printf("Buffer Readout %s Hex = %x\n", Pakheader, Pakheader);
		for(i = 0; i < HeaderSize; i++)
			if(Pakheader[i] != PACK_HEADER[i])
				return FALSE;  //ident corrupt

		printf("|P = TRUE\n");
		printf("|A = TRUE\n");
		printf("|C = TRUE\n");
		printf("|K = TRUE\n");
		printf("Verified!\n");
	}

	if(check == 2){  //LZO1 Header checking
		printf("Checking 'LZO1' Header\n");
		
		fread(Lzoheader, sizeof(HeaderSize), 1, hFile);
		for(i = 0; i < HeaderSize; i++)
			if(Lzoheader[i] != LZO1_HEADER[i])
				return FALSE;  //ident corrupt

		printf("|L = TRUE\n");
		printf("|Z = TRUE\n");
		printf("|O = TRUE\n");
		printf("|1 = TRUE\n");
		printf("-Verified!\n");
	}
	return TRUE;
}


This is the whole code that this section holds.
Last edited on
There's the problem. Pakheader is an array of 4 bytes, but you're asking fread() to write sizeof(4)*4 bytes to the buffer. 4 is an int, so it becomes sizeof(int), which is probably 4. You end up writing four times more data than you should.
Pass sizeof(BYTE) as the second parameter for fread(). You should pass HeaderSize as the third parameter.

EDIT: Oh, and I'd do it like this:
1
2
3
4
5
BYTE Pakheader[HeaderSize+1]={0};
if (/*...*/){
    fread(Pakheader, sizeof(BYTE), HeaderSize, hFile);
    if (!strcmp(Pakheader,"PACK"))
//... 
Last edited on
Your solution worked like a charm. Thanks alot helios.
Topic archived. No new replies allowed.