Skip to content

Commit 92459e7

Browse files
committed
Fix translatability markings in psql, and add defenses against future bugs.
Several previous commits have added columns to various \d queries without updating their translate_columns[] arrays, leading to potentially incorrect translations in NLS-enabled builds. Offenders include commit 8936867 (added prosecdef to \df+), c9ac00e (added description to \dc+) and 3b17efd (added description to \dC+). Fix those cases back to 9.3 or 9.2 as appropriate. Since this is evidently more easily missed than one would like, in HEAD also add an Assert that the supplied array is long enough. This requires an API change for printQuery(), so it seems inappropriate for back branches, but presumably all future changes will be tested in HEAD anyway. In HEAD and 9.3, also clean up a whole lot of sloppiness in the emitted SQL for \dy (event triggers): lack of translatability due to failing to pass words-to-be-translated through gettext_noop(), inadequate schema qualification, and sloppy formatting resulting in unnecessarily ugly -E output. Peter Eisentraut and Tom Lane, per bug #8702 from Sergey Burladyan
1 parent 5858cf8 commit 92459e7

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

src/bin/psql/describe.c

+30-14
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
224224
PQExpBufferData buf;
225225
PGresult *res;
226226
printQueryOpt myopt = pset.popt;
227-
static const bool translate_columns[] = {false, false, false, false, true, true, false, false, false, false};
227+
static const bool translate_columns[] = {false, false, false, false, true, true, true, false, false, false, false};
228228

229229
if (strlen(functypes) != strspn(functypes, "antwS+"))
230230
{
@@ -457,6 +457,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
457457
myopt.title = _("List of functions");
458458
myopt.translate_header = true;
459459
myopt.translate_columns = translate_columns;
460+
myopt.n_translate_columns = lengthof(translate_columns);
460461

461462
printQuery(res, &myopt, pset.queryFout, pset.logfile);
462463

@@ -789,6 +790,7 @@ permissionsList(const char *pattern)
789790
myopt.title = buf.data;
790791
myopt.translate_header = true;
791792
myopt.translate_columns = translate_columns;
793+
myopt.n_translate_columns = lengthof(translate_columns);
792794

793795
printQuery(res, &myopt, pset.queryFout, pset.logfile);
794796

@@ -862,6 +864,7 @@ listDefaultACLs(const char *pattern)
862864
myopt.title = buf.data;
863865
myopt.translate_header = true;
864866
myopt.translate_columns = translate_columns;
867+
myopt.n_translate_columns = lengthof(translate_columns);
865868

866869
printQuery(res, &myopt, pset.queryFout, pset.logfile);
867870

@@ -1034,6 +1037,7 @@ objectDescription(const char *pattern, bool showSystem)
10341037
myopt.title = _("Object descriptions");
10351038
myopt.translate_header = true;
10361039
myopt.translate_columns = translate_columns;
1040+
myopt.n_translate_columns = lengthof(translate_columns);
10371041

10381042
printQuery(res, &myopt, pset.queryFout, pset.logfile);
10391043

@@ -2818,6 +2822,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
28182822
myopt.title = _("List of relations");
28192823
myopt.translate_header = true;
28202824
myopt.translate_columns = translate_columns;
2825+
myopt.n_translate_columns = lengthof(translate_columns);
28212826

28222827
printQuery(res, &myopt, pset.queryFout, pset.logfile);
28232828
}
@@ -2999,7 +3004,8 @@ listConversions(const char *pattern, bool verbose, bool showSystem)
29993004
PQExpBufferData buf;
30003005
PGresult *res;
30013006
printQueryOpt myopt = pset.popt;
3002-
static const bool translate_columns[] = {false, false, false, false, true};
3007+
static const bool translate_columns[] =
3008+
{false, false, false, false, true, false};
30033009

30043010
initPQExpBuffer(&buf);
30053011

@@ -3055,6 +3061,7 @@ listConversions(const char *pattern, bool verbose, bool showSystem)
30553061
myopt.title = _("List of conversions");
30563062
myopt.translate_header = true;
30573063
myopt.translate_columns = translate_columns;
3064+
myopt.n_translate_columns = lengthof(translate_columns);
30583065

30593066
printQuery(res, &myopt, pset.queryFout, pset.logfile);
30603067

@@ -3079,19 +3086,23 @@ listEventTriggers(const char *pattern, bool verbose)
30793086
initPQExpBuffer(&buf);
30803087

30813088
printfPQExpBuffer(&buf,
3082-
"select evtname as \"%s\", "
3083-
"evtevent as \"%s\", "
3084-
"pg_catalog.pg_get_userbyid(e.evtowner) AS \"%s\", "
3085-
"case evtenabled when 'O' then 'enabled' "
3086-
" when 'R' then 'replica' "
3087-
" when 'A' then 'always' "
3088-
" when 'D' then 'disabled' end as \"%s\", "
3089-
"e.evtfoid::regproc as \"%s\", "
3090-
"array_to_string(array(select x "
3091-
" from unnest(evttags) as t(x)), ', ') as \"%s\" ",
3089+
"SELECT evtname as \"%s\", "
3090+
"evtevent as \"%s\", "
3091+
"pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"
3092+
" case evtenabled when 'O' then '%s'"
3093+
" when 'R' then '%s'"
3094+
" when 'A' then '%s'"
3095+
" when 'D' then '%s' end as \"%s\",\n"
3096+
" e.evtfoid::pg_catalog.regproc as \"%s\", "
3097+
"pg_catalog.array_to_string(array(select x"
3098+
" from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
30923099
gettext_noop("Name"),
30933100
gettext_noop("Event"),
30943101
gettext_noop("Owner"),
3102+
gettext_noop("enabled"),
3103+
gettext_noop("replica"),
3104+
gettext_noop("always"),
3105+
gettext_noop("disabled"),
30953106
gettext_noop("Enabled"),
30963107
gettext_noop("Procedure"),
30973108
gettext_noop("Tags"));
@@ -3100,7 +3111,7 @@ listEventTriggers(const char *pattern, bool verbose)
31003111
",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
31013112
gettext_noop("Description"));
31023113
appendPQExpBufferStr(&buf,
3103-
"\nFROM pg_event_trigger e ");
3114+
"\nFROM pg_catalog.pg_event_trigger e ");
31043115

31053116
processSQLNamePattern(pset.db, &buf, pattern, false, false,
31063117
NULL, "evtname", NULL, NULL);
@@ -3116,6 +3127,7 @@ listEventTriggers(const char *pattern, bool verbose)
31163127
myopt.title = _("List of event triggers");
31173128
myopt.translate_header = true;
31183129
myopt.translate_columns = translate_columns;
3130+
myopt.n_translate_columns = lengthof(translate_columns);
31193131

31203132
printQuery(res, &myopt, pset.queryFout, pset.logfile);
31213133

@@ -3134,7 +3146,7 @@ listCasts(const char *pattern, bool verbose)
31343146
PQExpBufferData buf;
31353147
PGresult *res;
31363148
printQueryOpt myopt = pset.popt;
3137-
static const bool translate_columns[] = {false, false, false, true};
3149+
static const bool translate_columns[] = {false, false, false, true, false};
31383150

31393151
initPQExpBuffer(&buf);
31403152

@@ -3214,6 +3226,7 @@ listCasts(const char *pattern, bool verbose)
32143226
myopt.title = _("List of casts");
32153227
myopt.translate_header = true;
32163228
myopt.translate_columns = translate_columns;
3229+
myopt.n_translate_columns = lengthof(translate_columns);
32173230

32183231
printQuery(res, &myopt, pset.queryFout, pset.logfile);
32193232

@@ -3289,6 +3302,7 @@ listCollations(const char *pattern, bool verbose, bool showSystem)
32893302
myopt.title = _("List of collations");
32903303
myopt.translate_header = true;
32913304
myopt.translate_columns = translate_columns;
3305+
myopt.n_translate_columns = lengthof(translate_columns);
32923306

32933307
printQuery(res, &myopt, pset.queryFout, pset.logfile);
32943308

@@ -3548,6 +3562,7 @@ describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
35483562
myopt.topt.default_footer = false;
35493563
myopt.translate_header = true;
35503564
myopt.translate_columns = translate_columns;
3565+
myopt.n_translate_columns = lengthof(translate_columns);
35513566

35523567
printQuery(res, &myopt, pset.queryFout, pset.logfile);
35533568

@@ -3579,6 +3594,7 @@ describeOneTSParser(const char *oid, const char *nspname, const char *prsname)
35793594
myopt.topt.default_footer = true;
35803595
myopt.translate_header = true;
35813596
myopt.translate_columns = NULL;
3597+
myopt.n_translate_columns = 0;
35823598

35833599
printQuery(res, &myopt, pset.queryFout, pset.logfile);
35843600

src/bin/psql/print.c

+4
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,10 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *f
25962596
printTableInit(&cont, &opt->topt, opt->title,
25972597
PQnfields(result), PQntuples(result));
25982598

2599+
/* Assert caller supplied enough translate_columns[] entries */
2600+
Assert(opt->translate_columns == NULL ||
2601+
opt->n_translate_columns >= cont.ncolumns);
2602+
25992603
for (i = 0; i < cont.ncolumns; i++)
26002604
{
26012605
char align;

src/bin/psql/print.h

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ typedef struct printQueryOpt
146146
bool translate_header; /* do gettext on column headers */
147147
const bool *translate_columns; /* translate_columns[i-1] => do
148148
* gettext on col i */
149+
int n_translate_columns; /* length of translate_columns[] */
149150
} printQueryOpt;
150151

151152

src/bin/scripts/createlang.c

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ main(int argc, char *argv[])
160160
popt.title = _("Procedural Languages");
161161
popt.translate_header = true;
162162
popt.translate_columns = translate_columns;
163+
popt.n_translate_columns = lengthof(translate_columns);
164+
163165
printQuery(result, &popt, stdout, NULL);
164166

165167
PQfinish(conn);

src/bin/scripts/droplang.c

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ main(int argc, char *argv[])
159159
popt.title = _("Procedural Languages");
160160
popt.translate_header = true;
161161
popt.translate_columns = translate_columns;
162+
popt.n_translate_columns = lengthof(translate_columns);
163+
162164
printQuery(result, &popt, stdout, NULL);
163165

164166
PQfinish(conn);

0 commit comments

Comments
 (0)