ECPG for Postgres 6 and 16: coredump including an unexpected memory free when PREPARE'ing a faulty query

60 Views Asked by At

The following coredump occurs (here it is using ecpg for postgresql 6, but still happens in later versions like 16):

==31300== Invalid free() / delete / delete[] / realloc() ==31300== at 0x4C3210C: free (vg_replace_malloc.c:538) ==31300== by 0x5071C13: ECPGfree_auto_mem (in /bdd/usr/pgsql/lib/libecpg.so.6.6) ==31300== by 0x5070CF5: ??? (in /bdd/usr/pgsql/lib/libecpg.so.6.6) ==31300== by 0x507124B: ??? (in /bdd/usr/pgsql/lib/libecpg.so.6.6) ==31300== by 0x50716D4: ECPGprepare (in /bdd/usr/pgsql/lib/libecpg.so.6.6)

This 100% of the time happens when PREPARE'ing a faulty query (with a syntax error, for example a truncated query "order by bigint").

For the coredump to occur, there must be a successful previous query with a "SELECT column into :hostVariable from ...", hostVariable being a "char *" and being correctly freed after.

Here is a sample code for reproducing this coredump:

    EXEC SQL BEGIN DECLARE SECTION;
    char * hostVariable = NULL;
    EXEC SQL END DECLARE SECTION;
 
    EXEC SQL SELECT my_column
    INTO :hostVariable
    FROM my_table
    LIMIT 1;

    free(hostVariable);

    EXEC SQL BEGIN DECLARE SECTION;
    const char * cRequest = "order by bigint";
    EXEC SQL END DECLARE SECTION;
 
    // this is where the coredump happens (ECPGprepare) :
    EXEC SQL PREPARE prepare_crash FROM :cRequest ;

It seems like replacing the "free(hostVariable);" instruction by the following instruction:

ECPGfree_auto_mem();

Does the trick at least in this example, there isn't any leak or coredump after the faulty PREPARE. So it seems like ECPG frees the host variables this way, and no longer frees them an other time with the faulty PREPARE.

It seems too good to be true as it's not documented in ECPG, are there more tricky cases where it won't be enough or will cause performance issues ?

0

There are 0 best solutions below