Ticket #9 (new defect)

Opened 5 years ago

Last modified 2 years ago

sizeof computation fails for nested derived types (pointer to array or array of pointers)

Reported by: guest Owned by:
Priority: normal Milestone:
Component: general Version: 0.15.1
Keywords: Cc:

Description

c2hs reports the wrong size of nested derived types.

struct pointer_to_array {
  int (*y)[4  ];
} PTA;
struct array_of_pointers {
  int* y[4];
} AOP;
diff fail2_via_c.out fail2_via_c2hs.out
1,2c1,2
< __tydef__array_of_pointers__: 16
< __tydef__pointer_to_array__: 4
---
> __tydef__array_of_pointers__: 4
> __tydef__pointer_to_array__: 16

The reason for this is that the analysis code assumes that pointer to T is represented as CPointerDeclr TDeclr, but it is actually parsed as TDeclr (...(CPointerDeclr)...).

Change History

Changed 2 years ago by guest

I think this may be related: bug.h

typedef char inner_t[32];
typedef struct
{
        inner_t first;
        inner_t second;
} outer_t;

bug.chs

module Bug where
#include "bug.h"
main :: IO ()
main = print ({# sizeof inner_t #}, {# sizeof outer_t #})

Running c2hs bug.chs (on a 64-bit machine) gives me…

module Bug where
main :: IO ()
main = print (32, 16)

Shurely schome mischtake?

On the other hand, okay.h

typedef struct
{
        char first[32];
        char second[32];
} outer_t;

okay.chs

module Okay where
#include "okay.h"
main :: IO ()
main = print {# sizeof outer_t #}

gives…

module Okay where
main :: IO ()
main = print 64
Note: See TracTickets for help on using tickets.