Skip to content

Commit

Permalink
Don't strip the delimiters
Browse files Browse the repository at this point in the history
This simplifies the code a lot, and makes it robust, as well.
  • Loading branch information
gurjeet committed Jan 10, 2023
1 parent 91d016c commit 62d11a5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 55 deletions.
8 changes: 4 additions & 4 deletions src/backend/catalog/pg_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static Oid get_other_operator(List *otherOp,
* are operator names rather than some other lexical entity.
*/
static bool
validOperatorName(const char *name, bool namedopNeedsDelimiters)
validOperatorName(const char *name)
{
size_t len = strlen(name);

Expand All @@ -81,7 +81,7 @@ validOperatorName(const char *name, bool namedopNeedsDelimiters)
return false;

/* Is this a Named Operator? */
if (validNamedOperator(name, namedopNeedsDelimiters))
if (validNamedOperator(name))
return true;

/* Can't contain any invalid characters */
Expand Down Expand Up @@ -218,7 +218,7 @@ OperatorShellMake(const char *operatorName,
/*
* validate operator name
*/
if (!validOperatorName(operatorName, true))
if (!validOperatorName(operatorName))
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("\"%s\" is not a valid operator name",
Expand Down Expand Up @@ -360,7 +360,7 @@ OperatorCreate(const char *operatorName,
/*
* Sanity checks
*/
if (!validOperatorName(operatorName, false))
if (!validOperatorName(operatorName))
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("\"%s\" is not a valid operator name",
Expand Down
5 changes: 2 additions & 3 deletions src/backend/parser/scan.l
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,10 @@ other .

{namedop} {
SET_YYLLOC();
if (yyleng >= NAMEDATALEN + 2)
if (yyleng >= NAMEDATALEN)
yyerror("operator name too long");
/* strip the delimiters/colons from the token */
/* XXX Should we support double-quoted, case sensitive names? */
yylval->str = downcase_identifier(pnstrdup(yytext + 1, yyleng-2), yyleng-2, false, false);
yylval->str = downcase_identifier(yytext, yyleng, false, false);
return Op;
}

Expand Down
20 changes: 1 addition & 19 deletions src/bin/pg_dump/pg_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -5429,29 +5429,11 @@ getOperators(Archive *fout, int *numOprs)

for (i = 0; i < ntups; i++)
{
bool is_namedop;
char *tmp;

oprinfo[i].dobj.objType = DO_OPERATOR;
oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
AssignDumpId(&oprinfo[i].dobj);

tmp = PQgetvalue(res, i, i_oprname);
is_namedop = validNamedOperator(tmp, false);

if (is_namedop)
{
char *delimited_name = palloc(strlen(tmp) + 3);

strcat(delimited_name, ":");
strcat(delimited_name, tmp);
strcat(delimited_name, ":");
oprinfo[i].dobj.name = delimited_name;
}
else
oprinfo[i].dobj.name = pg_strdup(tmp);

oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
oprinfo[i].dobj.namespace =
findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace)));
oprinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_oprowner));
Expand Down
38 changes: 12 additions & 26 deletions src/common/scansup.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,27 @@

#include "common/scansup.h"

/*
* need_delimiters == true: The name must have delimiters
* need_delimiters == false: The name must NOT have delimiters
*
*/
bool
validNamedOperator(const char *name, bool need_delimiters)
validNamedOperator(const char *name)
{
size_t len = strlen(name);
size_t min_len = (need_delimiters ? 2 : 0);
size_t max_len = (need_delimiters ? 2 : 0) + NAMEDATALEN;
bool valid_identifier;
char *tmp;

if (len <= min_len || len >= max_len)
if (len < 3 || len >= NAMEDATALEN)
return false;

if (need_delimiters
&& (name[0] != ':'
|| name[len-1] != ':'))
{
return false;
}
if (name[0] != ':' || name[len-1] != ':')
return false;

if (need_delimiters)
{
char *tmp = pstrdup(name);
tmp = pstrdup(name);

// Disregard the delimiters
tmp[len-1] = '\0';
tmp += 1;
valid_identifier = validIdentifier(tmp);
pfree(tmp);
}
else
valid_identifier = validIdentifier(name);
// Disregard the delimiters
tmp[len-1] = '\0';
tmp += 1;
valid_identifier = validIdentifier(tmp);
tmp -= 1;
pfree(tmp);

return valid_identifier;
}
Expand Down
4 changes: 2 additions & 2 deletions src/include/common/scansup.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef COMMON_SCANSUP_H
#define COMMON_SCANSUP_H

extern bool validNamedOperator(const char *name, bool need_delimiters);
extern bool validNamedOperator(const char *name);
extern bool validIdentifier(const char *name);

#endif /* COMMON_SCANSUP_H */
#endif /* COMMON_SCANSUP_H */
2 changes: 1 addition & 1 deletion test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CREATE OPERATOR :equal_2: (
LEFTARG = int,
RIGHTARG = int,
PROCEDURE = int4eq,
COMMUTATOR = equal_2 -- This should causes a failure, but it doesn't!
COMMUTATOR = equal_2 -- The absence of delimiters causes an expected failure
);


Expand Down

0 comments on commit 62d11a5

Please sign in to comment.