[BUG] ECPG crash with union type

Started by Jehan-Guillaume de Rorthaisabout 1 month ago3 messageshackers
Jump to latest

Hi,

One of our customer hit a sigsev with ecpg lately. Their team created a simple
scenario and point us the origin of the crash in ECPG code. The credit goes to
them (iMSA) for that work.

Here is their scenario:

EXEC SQL BEGIN DECLARE SECTION;

struct SVA1_BF5L10
{
char NFCTACS[2];
char ESUICRS[1];

union UVA1_BF5L10
{
long pointeur1;
long pointeur2;
} UVA1;
} SVA1;

EXEC SQL END DECLARE SECTION;

Trying to compile it leads to the segfault:

$ ecpg sc1.pgc
Segmentation fault (core dumped)

I have tested their scenario on all supported branches and the crash appears in
v18. We were able to identify commit 0e6060790d6533 (cc Tom Lane as author) as
the origin of the regression. Reverting the commit in a test branch doesn't
expose the crash anymore and sc1.c is generated without errors.

In ecpg code, union's type size is set to NULL in "ecpg/preproc/preproc.y":

else
{
$$.type_enum = ECPGt_union;
$$.type_sizeof = NULL;
}

But the patch change the following code in "ecpg/preproc/type.c" leading to
the segfault in "mm_strdup":

     struct ECPGtype *
    -ECPGmake_struct_type(struct ECPGstruct_member *rm, enum ECPGttype type, …
    +ECPGmake_struct_type(struct ECPGstruct_member *rm, enum ECPGttype type,
    +                                        const char *type_name, …
     {
            struct ECPGtype *ne = ECPGmake_simple_type(type, "1", 0);
            ne->type_name = mm_strdup(type_name);
            ne->u.members = ECPGstruct_member_dup(rm);
    -       ne->struct_sizeof = struct_sizeof;
    +       ne->struct_sizeof = mm_strdup(struct_sizeof);

If setting "$$.type_sizeof = NULL;" is legit for unions (I didn't try to wrap
my head around this code), maybe this change should be:

    -       ne->struct_sizeof = struct_sizeof;
    +       ne->struct_sizeof = struct_sizeof ? mm_strdup(struct_sizeof):NULL;

Regards,

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jehan-Guillaume de Rorthais (#1)
Re: [BUG] ECPG crash with union type

Jehan-Guillaume de Rorthais <jgdr@dalibo.com> writes:

One of our customer hit a sigsev with ecpg lately. Their team created a simple
scenario and point us the origin of the crash in ECPG code. The credit goes to
them (iMSA) for that work.
...
If setting "$$.type_sizeof = NULL;" is legit for unions (I didn't try to wrap
my head around this code), maybe this change should be:

-       ne->struct_sizeof = struct_sizeof;
+       ne->struct_sizeof = struct_sizeof ? mm_strdup(struct_sizeof):NULL;

Yeah, I agree. Will fix, thanks for the report!

regards, tom lane

In reply to: Tom Lane (#2)
Re: [BUG] ECPG crash with union type

On Thu, 25 Jun 2026 16:46:05 -0400
Tom Lane <tgl@sss.pgh.pa.us> wrote:

[…]

Yeah, I agree. Will fix, thanks for the report!

Thank you for your quick answer and commit!

Have a good day,