| From: | Andres Freund <andres(at)anarazel(dot)de> |
|---|---|
| To: | Peter Eisentraut <peter(at)eisentraut(dot)org> |
| Cc: | pgsql-hackers <pgsql-hackers(at)postgresql(dot)org> |
| Subject: | Re: -Wformat-nonliteral |
| Date: | 2026-11-16 23:46:50 |
| Message-ID: | pd3ctvtroyxe7tcidxtq73grlhuapybs4ao6buqguvlavwqtfb@jym63w3soglz |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
On 2026-11-13 11:37:40 +0100, Peter Eisentraut wrote:
> That issue was found by -Wformat-nonliteral. That warning option flags a
> bunch of other places, which were fortunately not security issues. I think
> this is quite a large surface area to leave unchecked. I propose to fix
> these and turn this warning option on by default. While this does require
> introducing a bit more duplication, the patch (see patch 0003) overall does
> not blow up the code much at all (911 insertions(+), 896 deletions(-)).
I find the degree of duplication troubling tbh. Stuff like:
> @@ -1023,7 +1022,12 @@ heap_vacuum_rel(Relation rel, const VacuumParams *params,
> * VACUUM VERBOSE ereport
> */
> Assert(!params->is_wraparound);
> - msgfmt = _("finished vacuuming \"%s.%s.%s\": index scans: %d\n");
> + appendStringInfo(&buf,
> + _("finished vacuuming \"%s.%s.%s\": index scans: %d\n"),
> + vacrel->dbname,
> + vacrel->relnamespace,
> + vacrel->relname,
> + vacrel->num_index_scans);
> }
> else if (params->is_wraparound)
> {
> @@ -1034,22 +1038,37 @@ heap_vacuum_rel(Relation rel, const VacuumParams *params,
> * case all the same, just in case.
> */
> if (vacrel->aggressive)
> - msgfmt = _("automatic aggressive vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n");
> + appendStringInfo(&buf,
> + _("automatic aggressive vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n"),
> + vacrel->dbname,
> + vacrel->relnamespace,
> + vacrel->relname,
> + vacrel->num_index_scans);
> else
> - msgfmt = _("automatic vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n");
> + appendStringInfo(&buf,
> + _("automatic vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n"),
> + vacrel->dbname,
> + vacrel->relnamespace,
> + vacrel->relname,
> + vacrel->num_index_scans);
> }
> else
> {
> if (vacrel->aggressive)
> - msgfmt = _("automatic aggressive vacuum of table \"%s.%s.%s\": index scans: %d\n");
> + appendStringInfo(&buf,
> + _("automatic aggressive vacuum of table \"%s.%s.%s\": index scans: %d\n"),
> + vacrel->dbname,
> + vacrel->relnamespace,
> + vacrel->relname,
> + vacrel->num_index_scans);
> else
> - msgfmt = _("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n");
> + appendStringInfo(&buf,
> + _("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n"),
> + vacrel->dbname,
> + vacrel->relnamespace,
> + vacrel->relname,
> + vacrel->num_index_scans);
> }
> - appendStringInfo(&buf, msgfmt,
> - vacrel->dbname,
> - vacrel->relnamespace,
> - vacrel->relname,
> - vacrel->num_index_scans);
> appendStringInfo(&buf, _("pages: %u removed, %u remain, %u scanned (%.2f%% of total), %u eagerly scanned\n"),
> vacrel->removed_pages,
> new_rel_pages,
is IMO vastly worse maintenance wise.
> @@ -1109,7 +1128,12 @@ heap_vacuum_rel(Relation rel, const VacuumParams *params,
> else
> appendStringInfoString(&buf, _("index scan needed: "));
>
> - msgfmt = _("%u pages from table (%.2f%% of total) had %" PRId64 " dead item identifiers removed\n");
> + appendStringInfo(&buf,
> + _("%u pages from table (%.2f%% of total) had %" PRId64 " dead item identifiers removed\n"),
> + vacrel->lpdead_item_pages,
> + orig_rel_pages == 0 ? 100.0 :
> + 100.0 * vacrel->lpdead_item_pages / orig_rel_pages,
> + vacrel->lpdead_items);
> }
> else
> {
> @@ -1118,13 +1142,13 @@ heap_vacuum_rel(Relation rel, const VacuumParams *params,
> else
> appendStringInfoString(&buf, _("index scan bypassed by failsafe: "));
>
> - msgfmt = _("%u pages from table (%.2f%% of total) have %" PRId64 " dead item identifiers\n");
> + appendStringInfo(&buf,
> + _("%u pages from table (%.2f%% of total) have %" PRId64 " dead item identifiers\n"),
> + vacrel->lpdead_item_pages,
> + orig_rel_pages == 0 ? 100.0 :
> + 100.0 * vacrel->lpdead_item_pages / orig_rel_pages,
> + vacrel->lpdead_items);
> }
> - appendStringInfo(&buf, msgfmt,
> - vacrel->lpdead_item_pages,
> - orig_rel_pages == 0 ? 100.0 :
> - 100.0 * vacrel->lpdead_item_pages / orig_rel_pages,
> - vacrel->lpdead_items);
>
> if (vacrel->worker_usage.vacuum.nplanned > 0)
> appendStringInfo(&buf,
And this one even repeats decidedly nontrivial code multiple times.
I do not agree that -Wformat-nonliteral is worth this kind of nontrivial
duplication.
That said, maybe we can just get rid of the duplication in the first place?
It's pretty silly to burden the translators with having to separately
translate
automatic aggressive vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n
automatic vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\nv
automatic aggressive vacuum of table \"%s.%s.%s\": index scans: %d\n"
automatic vacuum of table \"%s.%s.%s\": index scans: %d\n
and
%u pages from table (%.2f%% of total) had %" PRId64 " dead item identifiers removed\n
%u pages from table (%.2f%% of total) have %" PRId64 " dead item identifiers\n
And it's not awesome that we have separate lines for vacuum and autovacuum
either.
Can we perhaps reformulate this to something like
vacuum (%s) of table \"%s.%s.%s\": index scans: %d\n
with the first %s containing being the concatenation of {automatic, manual},
aggressive, to prevent wraparound, as appropriate?
I know separately translating arguments like "automatic" not awesome for
translators either, but it seems like it might still be better than what we
have now or what you are proposing?
> diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
> index fd37ea4f607..27ef4c02bea 100644
> --- a/src/backend/access/transam/xlog.c
> +++ b/src/backend/access/transam/xlog.c
> @@ -9890,8 +9890,9 @@ assign_wal_sync_method(int new_wal_sync_method, void *extra)
> void
> issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
> {
> - char *msg = NULL;
> instr_time start;
> + char xlogfname[MAXFNAMELEN];
> + int save_errno;
>
> Assert(tli != 0);
>
> @@ -9914,17 +9915,38 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
> {
> case WAL_SYNC_METHOD_FSYNC:
> if (pg_fsync_no_writethrough(fd) != 0)
> - msg = _("could not fsync file \"%s\": %m");
> + {
> + save_errno = errno;
> + XLogFileName(xlogfname, tli, segno, wal_segment_size);
> + errno = save_errno;
> + ereport(PANIC,
> + (errcode_for_file_access(),
> + errmsg("could not fsync file \"%s\": %m", xlogfname)));
> + }
> break;
> #ifdef HAVE_FSYNC_WRITETHROUGH
> case WAL_SYNC_METHOD_FSYNC_WRITETHROUGH:
> if (pg_fsync_writethrough(fd) != 0)
> - msg = _("could not fsync write-through file \"%s\": %m");
> + {
> + save_errno = errno;
> + XLogFileName(xlogfname, tli, segno, wal_segment_size);
> + errno = save_errno;
> + ereport(PANIC,
> + (errcode_for_file_access(),
> + errmsg("could not fsync write-through file \"%s\": %m", xlogfname)));
> + }
> break;
> #endif
> case WAL_SYNC_METHOD_FDATASYNC:
> if (pg_fdatasync(fd) != 0)
> - msg = _("could not fdatasync file \"%s\": %m");
> + {
> + save_errno = errno;
> + XLogFileName(xlogfname, tli, segno, wal_segment_size);
> + errno = save_errno;
> + ereport(PANIC,
> + (errcode_for_file_access(),
> + errmsg("could not fdatasync file \"%s\": %m", xlogfname)));
> + }
> break;
> case WAL_SYNC_METHOD_OPEN:
> case WAL_SYNC_METHOD_OPEN_DSYNC:
> @@ -9938,19 +9960,6 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
> break;
> }
>
> - /* PANIC if failed to fsync */
> - if (msg)
> - {
> - char xlogfname[MAXFNAMELEN];
> - int save_errno = errno;
> -
> - XLogFileName(xlogfname, tli, segno, wal_segment_size);
> - errno = save_errno;
> - ereport(PANIC,
> - (errcode_for_file_access(),
> - errmsg(msg, xlogfname)));
> - }
> -
> pgstat_report_wait_end();
I also dislike this. For one it blows up the size of the code by a fair
bit. And the repetition is bothersome too.
Here the fix seems easier - the only difference between the error messages is
the name of the GUC value. Given it's a GUC value, which are not translated,
the appropriate handling here seems to just pass the GUC value as a
nontranslated argument and rephrase as something like
"could not sync file \"%s\" using %s"
or such?
> pgstat_count_io_op_time(IOOBJECT_WAL, IOCONTEXT_NORMAL, IOOP_FSYNC,
> diff --git a/src/backend/catalog/aclchk.c b/src/backend/catalog/aclchk.c
> index 2bbcc3e824b..7b82255dbe6 100644
> --- a/src/backend/catalog/aclchk.c
> +++ b/src/backend/catalog/aclchk.c
> @@ -385,6 +385,50 @@ restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs,
> return this_privileges;
> }
>
> +static int
> +errmsg_invalid_privilege_type(ObjectType objtype, AclMode priv)
> +{
> + const char *ps = privilege_to_string(priv);
> +
> + switch (objtype)
> + {
> + case OBJECT_TABLE:
> + return errmsg("invalid privilege type %s for relation", ps);
> [many object types]
> + case OBJECT_PARAMETER_ACL:
> + return errmsg("invalid privilege type %s for parameter", ps);
> + default:
> + elog(ERROR, "unrecognized GrantStmt.objtype: %d", (int) objtype);
> + }
It seems insane to me to translate all of these separately, even if we already
did that.
Leaving that aside, I don't think it's a great idea to move the errmsg()
invocation outside of the ereport_domain() definition. That prevents things
like pg_prevent_errno_in_scope() from working.
> @@ -1049,7 +1066,7 @@ ExecAlterDefaultPrivilegesStmt(ParseState *pstate, AlterDefaultPrivilegesStmt *s
> if (priv & ~all_privileges)
> ereport(ERROR,
> (errcode(ERRCODE_INVALID_GRANT_OPERATION),
> - errmsg(errormsg, privilege_to_string(priv))));
> + errmsg_invalid_privilege_type(action->objtype, priv)));
>
> iacls.privileges |= priv;
> }
> @@ -2639,6 +2656,209 @@ privilege_to_string(AclMode privilege)
> * Note: we do not double-quote the %s's below, because many callers
> * supply strings that might be already quoted.
> */
> +
> +static int
> +errmsg_permission_denied(ObjectType objtype, const char *objectname)
> +{
> + switch (objtype)
> + {
> + case OBJECT_AGGREGATE:
> + return errmsg("permission denied for aggregate %s", objectname);
> [many object types]
> + case OBJECT_VIEW:
> + return errmsg("permission denied for view %s", objectname);
> + /* these currently aren't used */
> [many object types]
> + case OBJECT_USER_MAPPING:
> + elog(ERROR, "unsupported object type: %d", objtype);
> + }
> +
> + return 0;
> +}
The fact that we have multiple such "one translation per object type" cases
makes this approach even more insane.
Do translators actually want to translate stuff like "type", given that the
DDL object is named "type", regardless of what it's translated to? I see
there are translations for those not spelling them the grammar way, but I
really doubt that's helping anyone.
But even if that is important, would it really be that bad to have the
to-be-translated object type passed in as a separately translated string?
> +static int
> +errmsg_not_owner(ObjectType objtype, const char *objectname)
> +{
> + switch (objtype)
> + {
> + case OBJECT_AGGREGATE:
> + return errmsg("must be owner of aggregate %s", objectname);
> [many object types]
> + case OBJECT_USER_MAPPING:
> + elog(ERROR, "unsupported object type: %d", objtype);
> + }
> +
> + return 0;
> +}
And because it's so much fun, let's have more
O(#object_types * #error_messages * #languages)
type translator work. I'm sure that's sustainable.
And we do that differently in other places already!
C.f. getObjectDescription().
We probably shouldn't use that in the above cases, due to the additiional
catalog lookups it does, but it still feels like a relevant precedent.
> @@ -242,239 +236,217 @@ type_in_list_does_not_exist_skipping(List *typenames, const char **msg,
> static void
> does_not_exist_skipping(ObjectType objtype, Node *object)
> {
> - const char *msg = NULL;
> - char *name = NULL;
> - char *args = NULL;
> -
> switch (objtype)
> {
> case OBJECT_ACCESS_METHOD:
> - msg = gettext_noop("access method \"%s\" does not exist, skipping");
> - name = strVal(object);
> + ereport(NOTICE,
> + errmsg("access method \"%s\" does not exist, skipping",
> + strVal(object)));
> break;
> case OBJECT_TYPE:
> case OBJECT_DOMAIN:
> {
> TypeName *typ = castNode(TypeName, object);
>
> - if (!schema_does_not_exist_skipping(typ->names, &msg, &name))
> - {
> - msg = gettext_noop("type \"%s\" does not exist, skipping");
> - name = TypeNameToString(typ);
> - }
> + if (!schema_does_not_exist_skipping(typ->names))
> + ereport(NOTICE,
> + errmsg("type \"%s\" does not exist, skipping",
> + TypeNameToString(typ)));
> }
> break;
This all seems just insane. Before and after.
This all might have been ok when we had far fewer object types, but nobody can
tell me this kind of thing is good for translators.
To maintain my sanity, I'm very quickly skiming over the rest.
> @@ -8847,10 +8847,6 @@ buffer_readv_report(PgAioResult result, const PgAioTargetData *td,
> checkfail_count,
> first_off;
> uint8 affected_count;
> - const char *msg_one,
> - *msg_mult,
> - *det_mult,
> - *hint_mult;
>
> buffer_readv_decode_error(result, &zeroed_any, &ignored_any,
> &zeroed_or_error_count,
> @@ -8892,37 +8888,56 @@ buffer_readv_report(PgAioResult result, const PgAioTargetData *td,
> {
> Assert(!zeroed_any); /* can't have invalid pages when zeroing them */
> affected_count = zeroed_or_error_count;
> - msg_one = _("invalid page in block %u of relation \"%s\"");
> - msg_mult = _("%u invalid pages among blocks %u..%u of relation \"%s\"");
> - det_mult = _("Block %u held the first invalid page.");
> - hint_mult = _("See server log for the other %u invalid block(s).");
> + ereport(elevel,
> + errcode(ERRCODE_DATA_CORRUPTED),
> + affected_count == 1 ?
> + errmsg("invalid page in block %u of relation \"%s\"", first + first_off, rpath.str) :
> + errmsg_plural("%u invalid page among blocks %u..%u of relation \"%s\"",
> + "%u invalid pages among blocks %u..%u of relation \"%s\"",
> + affected_count,
> + affected_count, first, last, rpath.str),
> + affected_count > 1 ? errdetail("Block %u held the first invalid page.", first + first_off) : 0,
> + affected_count > 1 ? errhint_plural("See server log for the other %u invalid block.",
> + "See server log for the other %u invalid blocks.",
> + affected_count - 1,
> + affected_count - 1) : 0);
> }
> else if (zeroed_any && !ignored_any)
> {
> affected_count = zeroed_or_error_count;
> - msg_one = _("invalid page in block %u of relation \"%s\"; zeroing out page");
> - msg_mult = _("zeroing out %u invalid pages among blocks %u..%u of relation \"%s\"");
> - det_mult = _("Block %u held the first zeroed page.");
> - hint_mult = _("See server log for the other %u zeroed block(s).");
> + ereport(elevel,
> + errcode(ERRCODE_DATA_CORRUPTED),
> + affected_count == 1 ?
> + errmsg("invalid page in block %u of relation \"%s\"; zeroing out page", first + first_off, rpath.str) :
> + errmsg_plural("zeroing out %u invalid page among blocks %u..%u of relation \"%s\"",
> + "zeroing out %u invalid pages among blocks %u..%u of relation \"%s\"",
> + affected_count,
> + affected_count, first, last, rpath.str),
> + affected_count > 1 ? errdetail("Block %u held the first zeroed page.", first + first_off) : 0,
> + affected_count > 1 ? errhint_plural("See server log for the other %u zeroed block.",
> + "See server log for the other %u zeroed blocks.",
> + affected_count - 1,
> + affected_count - 1) : 0);
> }
> else if (!zeroed_any && ignored_any)
> {
> affected_count = checkfail_count;
> - msg_one = _("ignoring checksum failure in block %u of relation \"%s\"");
> - msg_mult = _("ignoring %u checksum failures among blocks %u..%u of relation \"%s\"");
> - det_mult = _("Block %u held the first ignored page.");
> - hint_mult = _("See server log for the other %u ignored block(s).");
> + ereport(elevel,
> + errcode(ERRCODE_DATA_CORRUPTED),
> + affected_count == 1 ?
> + errmsg("ignoring checksum failure in block %u of relation \"%s\"", first + first_off, rpath.str) :
> + errmsg_plural("ignoring %u checksum failure among blocks %u..%u of relation \"%s\"",
> + "ignoring %u checksum failures among blocks %u..%u of relation \"%s\"",
> + affected_count,
> + affected_count, first, last, rpath.str),
> + affected_count > 1 ? errdetail("Block %u held the first ignored page.", first + first_off) : 0,
> + affected_count > 1 ? errhint_plural("See server log for the other %u ignored block.",
> + "See server log for the other %u ignored blocks.",
> + affected_count - 1,
> + affected_count - 1) : 0);
> }
> else
> pg_unreachable();
> -
> - ereport(elevel,
> - errcode(ERRCODE_DATA_CORRUPTED),
> - affected_count == 1 ?
> - errmsg_internal(msg_one, first + first_off, rpath.str) :
> - errmsg_internal(msg_mult, affected_count, first, last, rpath.str),
> - affected_count > 1 ? errdetail_internal(det_mult, first + first_off) : 0,
> - affected_count > 1 ? errhint_internal(hint_mult, affected_count - 1) : 0);
> }
I am strongly against this. I am expecting to evolve some of this stuff, and
this makes it harder.
Greetings,
Andres Freund
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Zsolt Parragi | 2026-11-16 22:41:54 | Re: Dropping a composite attribute causes data integrity violations |