]> granicus.if.org Git - postgresql/commitdiff
Fix realfailN lexer rules to not make assumptions about input format.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 13 Nov 2018 19:54:41 +0000 (14:54 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 13 Nov 2018 19:54:41 +0000 (14:54 -0500)
The realfail1 and realfail2 backup-prevention rules always returned
token type FCONST, ignoring the possibility that what we've scanned
is more appropriately described as ICONST.  I think that at the
time that code was added, it might actually have been safe to not
distinguish; but since we started allowing AS-less aliases in SELECT
target lists, it's definitely legal to have a number immediately
followed by an identifier.

In the SELECT case, it seems there's no visible consequence because
make_const() will change the type back to integer anyway.  But I'm
worried that there are other contexts, or will be in future, where
it's more important to get the constant's type right.

Hence, use process_integer_literal to correctly determine which
token type to return.

Arguably this is a bug fix, but given the lack of evidence of
user-visible problems, I'll refrain from back-patching.

Discussion: https://postgr.es/m/21364.1542136808@sss.pgh.pa.us

src/backend/parser/scan.l
src/fe_utils/psqlscan.l
src/interfaces/ecpg/preproc/pgc.l

index 6c6a6e320f08da29c6fc76d030258ce5bcb1ec77..74e34df71fab91a2a5afb2980c758fcabb6f49bc 100644 (file)
@@ -1005,22 +1005,18 @@ other                   .
                                }
 {realfail1}            {
                                        /*
-                                        * throw back the [Ee], and treat as {decimal}.  Note
-                                        * that it is possible the input is actually {integer},
-                                        * but since this case will almost certainly lead to a
-                                        * syntax error anyway, we don't bother to distinguish.
+                                        * throw back the [Ee], and figure out whether what
+                                        * remains is an {integer} or {decimal}.
                                         */
                                        yyless(yyleng - 1);
                                        SET_YYLLOC();
-                                       yylval->str = pstrdup(yytext);
-                                       return FCONST;
+                                       return process_integer_literal(yytext, yylval);
                                }
 {realfail2}            {
                                        /* throw back the [Ee][+-], and proceed as above */
                                        yyless(yyleng - 2);
                                        SET_YYLLOC();
-                                       yylval->str = pstrdup(yytext);
-                                       return FCONST;
+                                       return process_integer_literal(yytext, yylval);
                                }
 
 
@@ -1255,6 +1251,10 @@ litbufdup(core_yyscan_t yyscanner)
        return new;
 }
 
+/*
+ * Process {integer}.  Note this will also do the right thing with {decimal},
+ * ie digits and a decimal point.
+ */
 static int
 process_integer_literal(const char *token, YYSTYPE *lval)
 {
@@ -1265,7 +1265,7 @@ process_integer_literal(const char *token, YYSTYPE *lval)
        val = strtoint(token, &endptr, 10);
        if (*endptr != '\0' || errno == ERANGE)
        {
-               /* integer too large, treat it as a float */
+               /* integer too large (or contains decimal pt), treat it as a float */
                lval->str = pstrdup(token);
                return FCONST;
        }
index ae5418e7da09983102c51ea0985b0f07978f9d8b..02d95498e4162b9d3e6457e2394379119caf1c54 100644 (file)
@@ -887,10 +887,9 @@ other                      .
                                }
 {realfail1}            {
                                        /*
-                                        * throw back the [Ee], and treat as {decimal}.  Note
-                                        * that it is possible the input is actually {integer},
-                                        * but since this case will almost certainly lead to a
-                                        * syntax error anyway, we don't bother to distinguish.
+                                        * throw back the [Ee], and figure out whether what
+                                        * remains is an {integer} or {decimal}.
+                                        * (in psql, we don't actually care...)
                                         */
                                        yyless(yyleng - 1);
                                        ECHO;
index 5ccda8da6b182aae6770375794bd6b5bf07eb476..5e90e35bd2bac0384b3d7abbf20bcbf60d8293d1 100644 (file)
@@ -900,20 +900,16 @@ cppline                   {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
                                }
 {realfail1}            {
                                        /*
-                                        * throw back the [Ee], and treat as {decimal}.  Note
-                                        * that it is possible the input is actually {integer},
-                                        * but since this case will almost certainly lead to a
-                                        * syntax error anyway, we don't bother to distinguish.
+                                        * throw back the [Ee], and figure out whether what
+                                        * remains is an {integer} or {decimal}.
                                         */
                                        yyless(yyleng - 1);
-                                       base_yylval.str = mm_strdup(yytext);
-                                       return FCONST;
+                                       return process_integer_literal(yytext, &base_yylval);
                                }
 {realfail2}            {
                                        /* throw back the [Ee][+-], and proceed as above */
                                        yyless(yyleng - 2);
-                                       base_yylval.str = mm_strdup(yytext);
-                                       return FCONST;
+                                       return process_integer_literal(yytext, &base_yylval);
                                }
 } /* <C,SQL> */
 
@@ -1473,6 +1469,10 @@ addlitchar(unsigned char ychar)
        literalbuf[literallen] = '\0';
 }
 
+/*
+ * Process {integer}.  Note this will also do the right thing with {decimal},
+ * ie digits and a decimal point.
+ */
 static int
 process_integer_literal(const char *token, YYSTYPE *lval)
 {
@@ -1483,7 +1483,7 @@ process_integer_literal(const char *token, YYSTYPE *lval)
        val = strtoint(token, &endptr, 10);
        if (*endptr != '\0' || errno == ERANGE)
        {
-               /* integer too large, treat it as a float */
+               /* integer too large (or contains decimal pt), treat it as a float */
                lval->str = mm_strdup(token);
                return FCONST;
        }