aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg/preproc/variable.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2022-07-12 17:05:46 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2022-07-12 17:05:46 -0400
commit83f1c7b742e80d5aa15e6710ecb324e388d007b3 (patch)
tree3c85900d94348391eedd802b5aeb6d796bdc1b46 /src/interfaces/ecpg/preproc/variable.c
parente64cdab003027acef29e713087fb667e2319f679 (diff)
downloadpostgresql-83f1c7b742e80d5aa15e6710ecb324e388d007b3.tar.gz
postgresql-83f1c7b742e80d5aa15e6710ecb324e388d007b3.zip
Fix ECPG's handling of type names that match SQL keywords.
Previously, ECPG could only cope with variable declarations whose type names either weren't any SQL keyword, or were at least partially reserved. If you tried to use something in the unreserved_keyword category, you got a syntax error. This is pretty awful, not only because it says right on the tin that those words are not reserved, but because the set of such keywords tends to grow over time. Thus, an ECPG program that was just fine last year could fail when recompiled with a newer SQL grammar. We had to work around this recently when STRING became a keyword, but it's time for an actual fix instead of a band-aid. To fix, borrow a trick from C parsers and make the lexer's behavior change when it sees a word that is known as a typedef. This is not free of downsides: if you try to use such a name as a SQL keyword in EXEC SQL later in the program, it won't be recognized as a SQL keyword, leading to a syntax error there instead. So in a real sense this is just trading one hazard for another. But there is an important difference: with this, whether your ECPG program works depends only on what typedef names and SQL commands are used in the program text. If it compiles today it'll still compile next year, even if more words have become SQL keywords. Discussion: https://postgr.es/m/3661437.1653855582@sss.pgh.pa.us
Diffstat (limited to 'src/interfaces/ecpg/preproc/variable.c')
-rw-r--r--src/interfaces/ecpg/preproc/variable.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/interfaces/ecpg/preproc/variable.c b/src/interfaces/ecpg/preproc/variable.c
index 887d479e735..2a2b9531187 100644
--- a/src/interfaces/ecpg/preproc/variable.c
+++ b/src/interfaces/ecpg/preproc/variable.c
@@ -497,15 +497,20 @@ check_indicator(struct ECPGtype *var)
}
struct typedefs *
-get_typedef(char *name)
+get_typedef(const char *name, bool noerror)
{
struct typedefs *this;
- for (this = types; this && strcmp(this->name, name) != 0; this = this->next);
- if (!this)
+ for (this = types; this != NULL; this = this->next)
+ {
+ if (strcmp(this->name, name) == 0)
+ return this;
+ }
+
+ if (!noerror)
mmfatal(PARSE_ERROR, "unrecognized data type name \"%s\"", name);
- return this;
+ return NULL;
}
void