Pelles C forum

Pelles C => Bug reports => Topic started by: jullien on July 07, 2014, 06:52:12 AM

Title: Can't take sizeof of a const struct defined with typedef
Post by: jullien on July 07, 2014, 06:52:12 AM
C allows typedef to define a type before type it refers to is actually defined. Pelles C supports this but has a strange behavior when typedef is for a const object.

I found this issue porting OpenLisp with upcoming version. It used to work since 2008 with all previous Pelles C versions.

here is a snippet to reproduce the bug:

Code: [Select]
#include <stdlib.h>
#include <stdint.h>

/* OK if the next two typedef are moved after struct definition */
typedef struct OBJECT * POINTER;
typedef const struct OBJECT * CPOINTER;

struct  OBJECT  {
int x;
int y;
};


#define nextenv( x ) (x + 1)

/* error #2153: Unknown size for type 'const struct OBJECT'. */
CPOINTER
foo( CPOINTER env )
{
CPOINTER a1 = env;

return nextenv(a1);
}

/* Ok if typedef is expanded */
const struct OBJECT *
foo1( const struct OBJECT * env )
{
const struct OBJECT * a1 = env;

return nextenv(a1);
}

/* Ok if POINTER is used (i.e. non-const) */
POINTER
foo2( POINTER env )
{
POINTER a1 = env;

return nextenv(a1);
}

Title: Re: Can't take sizeof of a const struct defined with typedef
Post by: Pelle on July 13, 2014, 07:32:34 PM
OK, I will look at it.
Title: Re: Can't take sizeof of a const struct defined with typedef
Post by: Pelle on July 15, 2014, 09:35:30 PM
In the hope of not having to upload yet another release candidate, could you please replace your existing copy of pocc.exe with the attached version (8.00.22) and see if it works better? Thanks.
Title: Re: Can't take sizeof of a const struct defined with typedef
Post by: jullien on July 19, 2014, 10:41:42 PM
It compiles fine but OpenLisp crashes when loading (64bit version).

c:\usr\jullien\openlisp>psalisp-AMD64.exe
CRT: unhandled exception (main) -- terminating

As it is the first build I'm able to do with new 8.0 I can't tell you if it is the new (fixed) pocc or 8.0 which fails.
I'll investigate and try to give you more inputs.

Regards
Title: Re: Can't take sizeof of a const struct defined with typedef
Post by: jullien on July 19, 2014, 11:24:45 PM
FYI it occurs both with 32 and 64bit version.
Title: Re: Can't take sizeof of a const struct defined with typedef
Post by: neo313 on July 20, 2014, 08:38:37 AM
I tried the exe in the attachment and the error went away.
Title: Re: Can't take sizeof of a const struct defined with typedef
Post by: jullien on July 20, 2014, 11:06:21 AM
You can close this thread as new pocc.exe works now correctly.

The bug I have (new with 8.0) seems to be related to a wrong optimization with macro expansion. It occurs with any of -O1, -O2 and -Ox I pass. With no -O* passed, binary works well.
I'm investigation. I'll open a specific thread as soon as I have more info.

Christian