Pelles C forum

Pelles C => Bug reports => Topic started by: Franzki on June 07, 2008, 12:42:12 PM

Title: non-optimized compiling leads to access violation in Win32 console application
Post by: Franzki on June 07, 2008, 12:42:12 PM
For debugging purposes I set optimizations to 'none' while compiling my Win32 console program in Pelles C Version 5.00

The generated program then crashes with an "Access Violation" on the next line:

Code: [Select]
k_ptr->jaarkosten[jaar] = rentevoet * k_ptr-> bedrag + 0.5;

The generated exe file also crashes on other systems.

This is the structure I'm using:

Code: [Select]

typedef struct K_REGEL
{
struct K_REGEL *Next;
char type[16];
char omschrijving[100];
int jaarkosten[50];
int bedrag;
int investeringsjaar;
int looptijd;
int ncw;
} K_REGEL;

k_ptr is a pointer to a stucture K_REGEL

Code: [Select]
k_ptr=malloc(sizeof(K_REGEL));

In this case 'jaar' = 0

jaarkosten[jaar] has been initialised to 0 for all items in the array. Its also not the first time that I access it.

Does anyone have a clue what I'm doing wrong?

The generated program also crashes on other systems. No problems however when optimation is turned on.

Title: Re: non-optimized compiling leads to access violation in Win32 console applicati
Post by: JohnF on June 07, 2008, 01:01:40 PM
For debugging purposes I set optimizations to 'none' while compiling my Win32 console program in Pelles C Version 5.00

The generated program then crashes with an "Access Violation" on the next line:

Code: [Select]
k_ptr->jaarkosten[jaar] = rentevoet * k_ptr-> bedrag + 0.5;

The generated exe file also crashes on other systems.

This is the structure I'm using:

Code: [Select]

typedef struct K_REGEL
{
struct K_REGEL *Next;
char type[16];
char omschrijving[100];
int jaarkosten[50];
int bedrag;
int investeringsjaar;
int looptijd;
int ncw;
} K_REGEL;

k_ptr is a pointer to a stucture K_REGEL

Code: [Select]
k_ptr=malloc(sizeof(K_REGEL));

In this case 'jaar' = 0

jaarkosten[jaar] has been initialised to 0 for all items in the array. Its also not the first time that I access it.

Does anyone have a clue what I'm doing wrong?

The generated program also crashes on other systems. No problems however when optimation is turned on.

With optimization tuned on the generated code will often use a register instead of a memory location which can hide subtle bugs.

Did you check that malloc actually succeeds?

You should post a small app which manifests the problem, otherwise one cannot give good advice.

John
Title: Re: non-optimized compiling leads to access violation in Win32 console application
Post by: Franzki on June 07, 2008, 01:56:20 PM
I will try to isolate the problem... though it may be hard.

In the meantime:

If I replace:
Code: [Select]
k_ptr->jaarkosten[jaar] = rentevoet * k_ptr-> bedrag + 0.5;

with

Code: [Select]

int test;
test = rentevoet*k_ptr->bedrag+0.5;
k_ptr->jaarkosten[jaar]=test;

The problem is gone (in that part of the program).
But it returns somewhere else in the program at a similar line of code.

I also tried Pelles C 3.0 but can't reproduce the Access Violation error over there. everything seems to be fine there...
Title: Re: non-optimized compiling leads to access violation in Win32 console application
Post by: Franzki on June 07, 2008, 02:17:59 PM
Code: [Select]
#include<stdlib.h>

typedef struct K_REGEL
{
int jaarkosten[30];
int bedrag;
} K_REGEL;



int main()
{
K_REGEL *k_ptr;
float rentevoet;

if(    (k_ptr=malloc(sizeof(K_REGEL)))  !=NULL);

{
rentevoet=0.065;
k_ptr->bedrag=100;

k_ptr->jaarkosten[0]=rentevoet*k_ptr->bedrag+0.5;
}

return(0);

}


This should reproduce the Access Violation... easier than I thought...

Operation system used: WinXP home edition SP3 Dutch
Title: Re: non-optimized compiling leads to access violation in Win32 console applicati
Post by: JohnF on June 07, 2008, 03:35:57 PM
I tried your listing - no access violation here.

Edit: with optimizations off.

John
Title: Re: non-optimized compiling leads to access violation in Win32 console applicati
Post by: Franzki on June 07, 2008, 03:48:31 PM
I tried your listing - no access violation here.

Edit: with optimizations off.

John


I was about to ask that.. ;-)

Anyway... I ran the generated executable on several systems... Even on a 'clean' SP3 install on a Virtual Box and it gave the same error everytime.

Im not sure whether it's a WinXP SP3 issue, a Pelles C 5.0 issue, or just bad coding by me.

What version of Windows are you using John?

And should this discussion be moved to the BUG section?
Title: Re: non-optimized compiling leads to access violation in Win32 console applicati
Post by: JohnF on June 07, 2008, 04:25:51 PM
I tried your listing - no access violation here.

Edit: with optimizations off.

John


I was about to ask that.. ;-)

Anyway... I ran the generated executable on several systems... Even on a 'clean' SP3 install on a Virtual Box and it gave the same error everytime.

Im not sure whether it's a WinXP SP3 issue, a Pelles C 5.0 issue, or just bad coding by me.

What version of Windows are you using John?

And should this discussion be moved to the BUG section?

We don't know it's a bug yet, and personally I doubt it is.

I'm using XP SP3

I've found that problems like these are invariably caused by a variable being corrupted by a buffer overrun, or putting something somewhere it does not belong and so causing corruption.

John


Title: Re: non-optimized compiling leads to access violation in Win32 console application
Post by: TimoVJL on June 07, 2008, 04:44:59 PM
Here is that point in assembler:
Code: [Select]
CPU Disasm
Address   Hex dump          Command                                  Comments
k_ptr->jaarkosten[0]=rentevoet*k_ptr->bedrag+0.5;
0040102B  |.  8B45 FC       MOV EAX,DWORD PTR SS:[k_ptr]
0040102E  |.  D945 F8       FLD DWORD PTR SS:[rentevoet]
00401031  |.  DA48 78       FIMUL DWORD PTR DS:[EAX+78]
00401034  |.  DC05 00304000 FADD QWORD PTR DS:[403000]               ; FLOAT 0.5000000000000000
0040103A  |.  E8 21060000   CALL 00401660
0040103F  |.  89C2          MOV EDX,EAX
00401041  |.  8910          MOV DWORD PTR DS:[EAX],EDX      <- here is that problem, same value in registers

This way it won't crash:
Code: [Select]
k_ptr->jaarkosten[0]=(int)rentevoet*k_ptr->bedrag+0.5;
Code: [Select]
CPU Disasm
Address   Hex dump          Command                                  Comments
0040102B  |.  D945 F8       FLD DWORD PTR SS:[LOCAL.3]
0040102E  |.  E8 3D060000   CALL 00401670                            ; [Regel.00401670
00401033  |.  89C0          MOV EAX,EAX
00401035  |.  8B55 FC       MOV EDX,DWORD PTR SS:[LOCAL.2]
00401038  |.  0FAF42 78     IMUL EAX,DWORD PTR DS:[EDX+78]
0040103C  |.  50            PUSH EAX
0040103D  |.  DB0424        FILD DWORD PTR SS:[ESP]
00401040  |.  58            POP EAX
00401041  |.  DC05 00304000 FADD QWORD PTR DS:[403000]               ; FLOAT 0.5000000000000000
00401047  |.  E8 24060000   CALL 00401670                            ; [Regel.00401670
0040104C  |.  89C0          MOV EAX,EAX
0040104E  |.  8902          MOV DWORD PTR DS:[EDX],EAX
Title: Re: non-optimized compiling leads to access violation in Win32 console application
Post by: Franzki on June 07, 2008, 05:13:51 PM
Here's some simplified code...

I have also been able to reproduce it on a WinXP SP2 system with a clean Pelles 5.0 install so far.


Code: [Select]
#include<stdlib.h>
typedef struct TEST { int i[2]; } TEST;

int main()
{
TEST *ptr;
int t;
if(    (ptr=malloc(sizeof(TEST)))  !=NULL);
{
t=100;
ptr->i[0]=100;
ptr->i[1]=0.25* t;     //this works
ptr->i[1]=ptr->i[0];    //this works
ptr->i[1]=0.25*ptr->i[0];    // this causes an Access Violation
}
return(0);
}

<EDIT>


Adding an (int) to the calculation seems to avoid the problem as float 0.25 is converted to int 0.
However the aim of my calculation is to have a float calculation of which the result is converted to int.

<EDIT2>

Still no problems in Pelles C 3.0
Title: Re: non-optimized compiling leads to access violation in Win32 console applicati
Post by: JohnF on June 07, 2008, 05:58:51 PM
Weird,

I guess Pelle should look at this. I still don't get an access violation.

John
Title: Re: non-optimized compiling leads to access violation in Win32 console application
Post by: Pelle on June 07, 2008, 08:38:48 PM
I couldn't reproduce it either at first, but then I removed the /Ze option... Now I can...

OK, I will try and fix this one way or the other...
Title: Re: non-optimized compiling leads to access violation in Win32 console applicati
Post by: JohnF on June 07, 2008, 11:11:53 PM
Right - same here.

John
Title: Re: non-optimized compiling leads to access violation in Win32 console application
Post by: Pelle on June 08, 2008, 01:15:04 PM
I have this fixed now, will upload a new version later.
Title: Re: non-optimized compiling leads to access violation in Win32 console applicati
Post by: JohnF on June 08, 2008, 01:19:35 PM
Thanks.

John
Title: Re: non-optimized compiling leads to access violation in Win32 console application
Post by: Franzki on June 09, 2008, 02:07:57 PM
The fix in version 5.00.1 seems to help, no problems anymore over here.

Thank you Pelle for fixing it so quick!


Frans
Title: Re: non-optimized compiling leads to access violation in Win32 console application
Post by: Pelle on June 09, 2008, 03:03:56 PM
Very good - thanks!