ECPG: "char*" instead of "const char*" in ecpglib.h - Mailing list pgsql-bugs

From Tomasz Ostrowski
Subject ECPG: "char*" instead of "const char*" in ecpglib.h
Date
Msg-id 20051109113922.GC11941@batory.org.pl
Whole thread Raw
Responses Re: ECPG: "char*" instead of "const char*" in ecpglib.h  (Qingqing Zhou <zhouqq@cs.toronto.edu>)
List pgsql-bugs
Using "char*" as an argument type instead of "const char*" in ecpglib.h
causes that for example the following sample program, basically
copied from docmentation
http://www.postgresql.org/docs/8.1/interactive/ecpg-dynamic.html ,
does not compile using "gcc-4.0.1":

test.pgc

    #include <stdio.h>

    int main()
    {
        EXEC SQL BEGIN DECLARE SECTION;
        const char *select_version_sql = "select version()";
        char version[1000];
        EXEC SQL END DECLARE SECTION;

        EXEC SQL CONNECT TO template1 USER postgres;
        EXEC SQL PREPARE select_version FROM :select_version_sql;
        EXEC SQL EXECUTE select_version INTO :version;
        printf("%s\n", version);
        return 0;
    }

$ ecpg --version
ecpg (PostgreSQL 8.1.0) 4.1.1

$ecpg test.pgc

$gcc test.c -lecpg
test.pgc: In function 'main':
test.pgc:11: warning: passing argument 3 of 'ECPGprepare' discards qualifiers from pointer target type

$g++ test.c -lecpg
test.pgc: In function 'int main()':
test.pgc:11: error: invalid conversion from 'const char*' to 'char*'
test.pgc:11: error:   initializing argument 3 of 'bool ECPGprepare(int, char*, char*)'

----------------------------------------------------------

This forces to use terrible workarounds like:

    const char *select_version_sql = "select version()";
    int select_version_sql_l = strlen(select_version_sql);
    EXEC SQL BEGIN DECLARE SECTION;
    char _select_version_sql[select_version_sql_l+1];
    EXEC SQL END DECLARE SECTION;
    strncpy(_select_version_sql, select_version_sql, select_version_sql_l+1);

    EXEC SQL PREPARE select_version FROM :_select_version_sql;

----------------------------------------------------------

This bug was present in 8.0.4 and is present in 8.1.0 - I haven't
checked other versions.


Is this just a function declaration bug or do ecpg functions really
change their arguments so they cannot be declared "const" (also a bug)?

Regards
Tometzky
--
...although Eating Honey was a very good thing to do, there was a
moment just before you began to eat it which was better than when you
were...
                                                      Winnie the Pooh

pgsql-bugs by date:

Previous
From: "Dragon"
Date:
Subject: BUG #2030: Chinese character support error
Next
From: "Dave Page"
Date:
Subject: Re: Psql odbc driver version 8.01.01.00 doesn't remember "Cache size" option if it is set to 0 (to set "unlimited").