Change error handling of out of scope variables in ecpg.
authorMichael Meskes <meskes@postgresql.org>
Wed, 30 Jan 2019 12:58:25 +0000 (13:58 +0100)
committerMichael Meskes <meskes@postgresql.org>
Wed, 30 Jan 2019 13:35:52 +0000 (14:35 +0100)
The function called can result in an out of memory error that subsequently was
disregarded. Instead it should set the appropriate SQL error variables and be
checked by whatever whenever statement is defined.

src/interfaces/ecpg/ecpglib/misc.c
src/interfaces/ecpg/preproc/ecpg.addons
src/interfaces/ecpg/preproc/ecpg.trailer
src/interfaces/ecpg/preproc/output.c
src/interfaces/ecpg/preproc/preproc_extern.h
src/interfaces/ecpg/test/expected/compat_informix-sqlda.c
src/interfaces/ecpg/test/expected/compat_informix-test_informix.c
src/interfaces/ecpg/test/expected/preproc-cursor.c
src/interfaces/ecpg/test/expected/preproc-outofscope.c
src/interfaces/ecpg/test/expected/sql-binary.c

index a26dfdb361f1ab697b470dd6afa65f2402be490f..ee0d3e98fb928b8bb5490ed61c92f416ad824a8d 100644 (file)
@@ -525,6 +525,17 @@ ECPGset_var(int number, void *pointer, int lineno)
 {
        struct var_list *ptr;
 
+       struct sqlca_t *sqlca = ECPGget_sqlca();
+
+       if (sqlca == NULL)
+        {
+                ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+                       ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+                return;
+        }
+
+        ecpg_init_sqlca(sqlca);
+
        for (ptr = ivlist; ptr != NULL; ptr = ptr->next)
        {
                if (ptr->number == number)
index 821d93ef0797c91bcdcf230e4bae74d5e08f5986..86be5041e9f1ca586b9f2148b436d2abd33210c8 100644 (file)
@@ -22,7 +22,7 @@ ECPG: stmtDeallocateStmt block
                output_deallocate_prepare_statement($1);
        }
 ECPG: stmtDeclareCursorStmt block
-       { output_simple_statement($1); }
+       { output_simple_statement($1, (strncmp($1, "ECPGset_var", strlen("ECPGset_var")) == 0) ? 4 : 0); }
 ECPG: stmtDiscardStmt block
 ECPG: stmtFetchStmt block
        { output_statement($1, 1, ECPGst_normal); }
@@ -65,7 +65,7 @@ ECPG: stmtViewStmt rule
        }
        | ECPGCursorStmt
        {
-               output_simple_statement($1);
+                output_simple_statement($1, (strncmp($1, "ECPGset_var", strlen("ECPGset_var")) == 0) ? 4 : 0);
        }
        | ECPGDeallocateDescr
        {
@@ -75,7 +75,7 @@ ECPG: stmtViewStmt rule
        }
        | ECPGDeclare
        {
-               output_simple_statement($1);
+               output_simple_statement($1, 0);
        }
        | ECPGDescribe
        {
@@ -178,14 +178,14 @@ ECPG: stmtViewStmt rule
                if (connection)
                        mmerror(PARSE_ERROR, ET_ERROR, "AT option not allowed in VAR statement");
 
-               output_simple_statement($1);
+               output_simple_statement($1, 0);
        }
        | ECPGWhenever
        {
                if (connection)
                        mmerror(PARSE_ERROR, ET_ERROR, "AT option not allowed in WHENEVER statement");
 
-               output_simple_statement($1);
+               output_simple_statement($1, 0);
        }
 ECPG: where_or_current_clauseWHERECURRENT_POFcursor_name block
        {
@@ -327,12 +327,7 @@ ECPG: DeclareCursorStmtDECLAREcursor_namecursor_optionsCURSORopt_holdFORSelectSt
                }
                comment = cat_str(3, mm_strdup("/*"), c1, mm_strdup("*/"));
 
-               if ((braces_open > 0) && INFORMIX_MODE) /* we're in a function */
-                       $$ = cat_str(3, adjust_outofscope_cursor_vars(this),
-                               mm_strdup("ECPG_informix_reset_sqlca();"),
-                               comment);
-               else
-                       $$ = cat2_str(adjust_outofscope_cursor_vars(this), comment);
+               $$ = cat2_str(adjust_outofscope_cursor_vars(this), comment);
        }
 ECPG: ClosePortalStmtCLOSEcursor_name block
        {
index 19dc78188598db473833c62f3abb7b276b0e8838..60e1f53e9d397e1b1b38cb7930162a6642154220 100644 (file)
@@ -339,12 +339,8 @@ ECPGCursorStmt:  DECLARE cursor_name cursor_options CURSOR opt_hold FOR prepared
 
                        comment = cat_str(3, mm_strdup("/*"), mm_strdup(this->command), mm_strdup("*/"));
 
-                       if ((braces_open > 0) && INFORMIX_MODE) /* we're in a function */
-                               $$ = cat_str(3, adjust_outofscope_cursor_vars(this),
-                                       mm_strdup("ECPG_informix_reset_sqlca();"),
+                       $$ = cat_str(2, adjust_outofscope_cursor_vars(this),
                                        comment);
-                       else
-                               $$ = cat2_str(adjust_outofscope_cursor_vars(this), comment);
                }
                ;
 
index cdfa52608c4176c55879c89f0376c70ba95f1215..f778bbe35feb62bf3d818448b715e4e6007ebcb9 100644 (file)
@@ -16,9 +16,11 @@ output_line_number(void)
 }
 
 void
-output_simple_statement(char *stmt)
+output_simple_statement(char *stmt, int whenever_mode)
 {
        output_escaped_str(stmt, false);
+       if (whenever_mode)
+               whenever_action(whenever_mode);
        output_line_number();
        free(stmt);
 }
index 97467800dc9eff9ede01d08cff43d7596ad069df..3cdbdabaa367011260548463d40d14ab73bb901a 100644 (file)
@@ -69,7 +69,7 @@ extern void output_line_number(void);
 extern void output_statement(char *, int, enum ECPG_statement_type);
 extern void output_prepare_statement(char *, char *);
 extern void output_deallocate_prepare_statement(char *);
-extern void output_simple_statement(char *);
+extern void output_simple_statement(char *, int);
 extern char *hashline_number(void);
 extern int     base_yyparse(void);
 extern int     base_yylex(void);
index ad3188d1e6369906c4fd10d6e7f15c1682763246..7e19319d27f88f40fba14e926e583b74829a130d 100644 (file)
@@ -236,7 +236,7 @@ if (sqlca.sqlcode < 0) exit (1);}
 
 
        strcpy(msg, "declare");
-       ECPG_informix_reset_sqlca(); /* declare mycur1 cursor for $1 */
+       /* declare mycur1 cursor for $1 */
 #line 98 "sqlda.pgc"
 
 
@@ -311,7 +311,7 @@ if (sqlca.sqlcode < 0) exit (1);}
 
 
        strcpy(msg, "declare");
-       ECPG_informix_reset_sqlca(); /* declare mycur2 cursor for $1 */
+       /* declare mycur2 cursor for $1 */
 #line 135 "sqlda.pgc"
 
 
index 387e2e810d3c3192f72d5db7b668c6c756373367..cc6504992d09b5bebc205716ab4341c0dc7d7d27 100644 (file)
@@ -148,7 +148,12 @@ if (sqlca.sqlcode < 0) dosqlprint ( );}
 
        sqlca.sqlcode = 100;
        ECPGset_var( 0, &( i ), __LINE__);\
- ECPG_informix_reset_sqlca(); /* declare c cursor for select * from test where i <= $1  */
+ /* declare c cursor for select * from test where i <= $1  */
+#line 49 "test_informix.pgc"
+
+if (sqlca.sqlcode < 0) dosqlprint ( );
+#line 49 "test_informix.pgc"
+
 #line 49 "test_informix.pgc"
 
        printf ("%ld\n", sqlca.sqlcode);
index 482290174210a2fa923e1c418890cc6a84576045..a4e7b12c17a119da42d999b3bea82b897f12ddc2 100644 (file)
@@ -185,6 +185,11 @@ if (sqlca.sqlcode < 0) exit (1);}
  /* declare $0 cursor for select id , t from t1 */
 #line 64 "cursor.pgc"
 
+if (sqlca.sqlcode < 0) exit (1);
+#line 64 "cursor.pgc"
+
+#line 64 "cursor.pgc"
+
 
        strcpy(msg, "open");
        { ECPGdo(__LINE__, 0, 1, "test1", 0, ECPGst_normal, "declare $0 cursor for select id , t from t1", 
@@ -321,6 +326,11 @@ if (sqlca.sqlcode < 0) exit (1);}
  /* declare $0 cursor for select id , t from t1 */
 #line 105 "cursor.pgc"
 
+if (sqlca.sqlcode < 0) exit (1);
+#line 105 "cursor.pgc"
+
+#line 105 "cursor.pgc"
+
 
        strcpy(msg, "open");
        { ECPGdo(__LINE__, 0, 1, "test1", 0, ECPGst_normal, "declare $0 cursor for select id , t from t1", 
@@ -475,12 +485,22 @@ if (sqlca.sqlcode < 0) exit (1);}
        strcpy(msg, "declare");
        ECPGset_var( 4, &( curname3 ), __LINE__);\
  /* declare $0 cursor for $1 */
+#line 149 "cursor.pgc"
+
+if (sqlca.sqlcode < 0) exit (1);
+#line 149 "cursor.pgc"
+
 #line 149 "cursor.pgc"
 
        ECPGset_var( 5, &( curname5 ), __LINE__);\
  /* declare $0 cursor for $1 */
 #line 150 "cursor.pgc"
 
+if (sqlca.sqlcode < 0) exit (1);
+#line 150 "cursor.pgc"
+
+#line 150 "cursor.pgc"
+
 
        strcpy(msg, "open");
        { ECPGdo(__LINE__, 0, 1, "test1", 0, ECPGst_normal, "declare $0 cursor for $1", 
@@ -661,6 +681,11 @@ if (sqlca.sqlcode < 0) exit (1);}
  /* declare $0 cursor for $1 */
 #line 203 "cursor.pgc"
 
+if (sqlca.sqlcode < 0) exit (1);
+#line 203 "cursor.pgc"
+
+#line 203 "cursor.pgc"
+
 
        strcpy(msg, "open");
        { ECPGdo(__LINE__, 0, 1, "test1", 0, ECPGst_normal, "declare $0 cursor for $1", 
index 1ebdc0a6ea3feebef39af70dc019b58014721401..3a27c53e174322c3c560e5d7dd99cc85af93c8c2 100644 (file)
@@ -192,6 +192,11 @@ get_var1(MYTYPE **myvar0, MYNULLTYPE **mynullvar0)
  /* declare mycur cursor for select * from a1 */
 #line 28 "outofscope.pgc"
 
+if (sqlca.sqlcode < 0) exit (1);
+#line 28 "outofscope.pgc"
+
+#line 28 "outofscope.pgc"
+
 
        if (sqlca.sqlcode != 0)
                exit(1);
index 99244bbcef8026c1dcac8cec7828433a8900624b..6d92ce344b6f4dc08e81ab3b4c0131543fb8b5a4 100644 (file)
@@ -101,6 +101,11 @@ if (sqlca.sqlcode < 0) exit (1);}
 
   ECPGset_var( 0, &( empl.idnum ), __LINE__);\
  /* declare C cursor for select name , accs , byte from empl where idnum = $1  */
+#line 36 "binary.pgc"
+
+if (sqlca.sqlcode < 0) exit (1);
+#line 36 "binary.pgc"
+
 #line 36 "binary.pgc"
 
   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare C cursor for select name , accs , byte from empl where idnum = $1 ", 
@@ -135,6 +140,11 @@ if (sqlca.sqlcode < 0) exit (1);}
   memset(empl.name, 0, 21L);
   ECPGset_var( 1, &( empl.idnum ), __LINE__);\
  /* declare B binary cursor for select name , accs , byte from empl where idnum = $1  */
+#line 44 "binary.pgc"
+
+if (sqlca.sqlcode < 0) exit (1);
+#line 44 "binary.pgc"
+
 #line 44 "binary.pgc"
 
   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare B binary cursor for select name , accs , byte from empl where idnum = $1 ", 
@@ -172,6 +182,11 @@ if (sqlca.sqlcode < 0) exit (1);}
 
   ECPGset_var( 2, &( empl.idnum ), __LINE__);\
  /* declare A binary cursor for select byte from empl where idnum = $1  */
+#line 55 "binary.pgc"
+
+if (sqlca.sqlcode < 0) exit (1);
+#line 55 "binary.pgc"
+
 #line 55 "binary.pgc"
 
   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare A binary cursor for select byte from empl where idnum = $1 ",